How to use xsl number to count elements

2020-06-16 03:03发布

问题:

Below I am counting elements using xsl:number. I would like to count the chapters in order, see below:

XML:

<map>
  <part>
   <chapter/>
  </part>
  <chapter/>
  <part>
   <chapter/>
   <chapter/>
  </part>
</map>

XSLT:

<xsl:template match="chapter">
  <xsl:variable name="chapNum">
    <xsl:number count="chapter" format="1"/>
  </xsl:variable>
  <xsl:value-of select="$chapNum"/>
</xsl:template>

OUTPUT:

1
1
1
2

Desired OUTPUT:

1
2
3
4

I believe I need to use the from attribute but I am not sure how to implement it.

Thanks for any help in advance!

回答1:

Use <xsl:number level="any"/>



标签: xslt