In my input.xml
I have both <note>
and <bignote>
tags, so in the output I try to make each occurrence of both of this two tags follow a same numbering chain.
<body>
<p>
dumy text dumy text dumy text dumy text
<div>
dumy text dumy text dumy text dumy text
<note>LOREM</note>
dumy text dumy text dumy text dumy text
</div>
<gibnote>IPSUM</gibnote>
</p>
<p>
dumy text dumy text dumy text dumy text
<note>DOLOR</note>
dumy text dumy text dumy text dumy text
</p>
dumy text dumy text dumy text dumy text
<bignote>SIT</bignote>
<div>
<note>AMET</note>
</div>
dumy text dumy text dumy text dumy text
</body>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="note">
<xsl:copy>
<xsl:number level="any" format="1. "/>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:template match="bignote">
<xsl:copy>
<xsl:number level="any" format="1. "/>
<xsl:value-of select="."/>
</xsl:copy>
BIG
</xsl:template>
</xsl:stylesheet>
<body>
<p>
dumy text dumy text dumy text dumy text
<div>
dumy text dumy text dumy text dumy text
<note rank="note1">1. LOREM</note>
dumy text dumy text dumy text dumy text
</div>
<note rank="note2">2. BIGIPSUM</note>
</p>
<p>
dumy text dumy text dumy text dumy text
<note rank="note3">3. DOLOR</note>
dumy text dumy text dumy text dumy text
</p>
dumy text dumy text dumy text dumy text
<note rank="note4">4. BIGSIT</note>
<div>
<note rank="note5">5. AMET</note>
</div>
dumy text dumy text dumy text dumy text
</body>
Unfortunatly, the numbering follow a different chain between <note>
s and <bignote>
s.
I get this kind of numbering:
1. note Lorem
1. bignote Ipsum
2. note Dolor
2. bignote sit
3. note amet
So, how to make both of <note>
s and <bignote>
s follow the same numbering suite?
Check out the count
attribute of xsl:number
. This is a pattern which defines which nodes are counted to calculate the number.
The 'Numbering' section of the XSLT 1.0 spec says:
The
count
attribute is a pattern that specifies what nodes should be counted at those levels. If count attribute is not specified, then it defaults to the pattern that matches any node with the same node type as the current node and, if the current node has an expanded-name, with the same expanded-name as the current node.
Here I modified your stylesheet so that it counts either note
or bignote
elements.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="note">
<xsl:copy>
<xsl:number count="bignote|note" level="any" format="1. "/>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:template match="bignote">
<xsl:copy>
<xsl:number count="bignote|note" level="any" format="1. "/>
<xsl:value-of select="."/>
</xsl:copy>
BIG
</xsl:template>
</xsl:stylesheet>