How to subtract value in xslt?

南笙酒味 提交于 2019-12-23 13:27:32

问题


Could you please tell me how to subtract value in xslt using a variable?

Here is my code:

<xsl:variable name="currentCurpg" select="1"/>
<xsl:variable name="tCurpg" select="($currentCurpg-1)"/>

The variable tCurpg should be zero or 0.

Why I am getting error?

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
         <xsl:variable name="currentCurpg" select="1"/>
          <xsl:variable name="tCurpg" select="($currentCurpg-1)"/>
     <xsl:value-of select="$tCurpg"/>

      </hmtl>
    </xsl:template>


</xsl:transform>

I am expecting output zero.


回答1:


The problem is that hyphens are valid in variable names, so when you do this...

<xsl:variable name="tCurpg" select="($currentCurpg-1)"/>

It is literally looking for a variable named currentCurpg-1.

Instead change it to this...

<xsl:variable name="tCurpg" select="$currentCurpg - 1"/>



回答2:


Your select attribute value ul/li[position() &gt;= last()-{$currentCurpg} and position() &lt;= last()-1] is invalid. In XSLT attributes, you use XSLT variables directly, so the curly brackets shouldn't be present. In other words, use ul/li[position() &gt;= last()-$currentCurpg and position() &lt;= last()-1].

Also, in <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />, the correct value for doctype-public is about:legacy-compat; see HTML §12.1.1: The DOCTYPE.

And declaring omit-xml-declaration="yes" is pointless for HTML since HTML generated with method="html" isn't XML and will therefore never have an XML declaration.

Edit: The OP apparently initially linked to the wrong code which also had errors.




回答3:


Looking at your code, the curly {} braces around your variable are not need in an xslt statement only in html

eg div title="{$currentCurpg}">

so in your code you need

<xsl:for-each select="ul/li[position() &gt;= (last()-$currentCurpg) and position() &lt;= last()-1]">

Updated Based on your updated code you need to drop the () and put spaces between the variable and the - 1 like this

 <xsl:variable name="tCurpg" select="$currentCurpg - 1"/>


来源:https://stackoverflow.com/questions/42411314/how-to-subtract-value-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!