Home > other >  Descend variable counter using xsl:number
Descend variable counter using xsl:number

Time:09-16

I am looking to use variable to have a date that needs to be counted in descendant order, e.g. (2021-01-01, 2020-01-01). I have simplified the code and it just treats the year.

The reason I am using a variable is because in the larger code I need to call data from JSON.

I have found this SO question that talks about "descending counter", but the answers are perfomed without variable.


Problem: The result comes in ascending order.

Data source (XML/JSON):

Not used in this example.

You find the same code in this xsltfiddle.

Code:

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="data">
      
      <xsl:for-each select="1 to 2">
         
        <xsl:variable name="counter">
            <xsl:number start-at="2016" value="position()"/>
        </xsl:variable>
        
        <xsl:value-of select="$counter"/>
         
      </xsl:for-each>
    
  </xsl:template>
  
</xsl:stylesheet>

Result:

<!DOCTYPE HTML>20162017

Wanted Result:

<!DOCTYPE HTML>20172016

CodePudding user response:

The example seems somewhat contrived. Perhaps this can help you:

<xsl:for-each select="reverse(1 to 2)">
    <xsl:variable name="counter">
        <xsl:number start-at="2016" value="."/>
    </xsl:variable>
    <xsl:value-of select="$counter"/>
</xsl:for-each>

There is probably a simpler way to accomplish whatever this is supposed to accomplish.

  • Related