Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract value in xslt?

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.

like image 413
user944513 Avatar asked Feb 05 '23 01:02

user944513


2 Answers

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"/>
like image 187
Tim C Avatar answered Feb 07 '23 15:02

Tim C


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"/>
like image 26
RT72 Avatar answered Feb 07 '23 13:02

RT72