Reputation: 105
Input:
<root>
<trip>
<ID>3295</ID>
<ordini>
<NR>821321</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>234</NR>
<!-- some info -->
</ordini>
</trip>
<trip>
<ID>23</ID>
<ordini>
<NR>2321</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>999</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>232132131</NR>
<!-- some info -->
</ordini>
</trip>
</root>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template name="set-row">
<xsl:param name="runningtot"/>
<xsl:param name="node"/>
<xsl:value-of select="$runningtot + 1"/>
<xsl:if test="$node/following-sibling::ordini">
<xsl:call-template name="set-row">
<xsl:with-param name="runningtot" select="$runningtot + 1"/>
<xsl:with-param name="node" select="$node/following-sibling::ordini[1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="ordini">
<xsl:for-each select="//ordini">
<ordini>
<NR>
<xsl:call-template name="set-row">
<xsl:with-param name="runningtot" select="0"/>
<xsl:with-param name="node" select="ordini"/>
</xsl:call-template>
</NR>
<xsl:apply-templates/>
</ordini>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Desired output:
<root>
<trip>
<ID>3295</ID>
<ordini>
<NR>1</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>2</NR>
<!-- some info -->
</ordini>
</trip>
<trip>
<ID>23</ID>
<ordini>
<NR>1</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>2</NR>
<!-- some info -->
</ordini>
<ordini>
<NR>3</NR>
<!-- some info -->
</ordini>
</trip>
</root>
Basically, I want for each 'ordini' tag to REPLACE the 'NR' tag, where I count, starting from 1 and incrementing, each 'ordini' tag in the parent 'trip' tag. Saw this template with parameter answer on here, that is used for recurring increment counts, but I can't make it work for me.
Thank you for your time.
Upvotes: 0
Views: 195
Reputation: 70648
You have a template matching ordini
but then you go and select all ordini
elements in the document (as that is what //ordini
) selects.
There is a much simpler solution in this case anyway, rather than using that set-row
template. Simply use xsl:number
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ordini">
<ordini>
<NR>
<xsl:number />
</NR>
<xsl:apply-templates/>
</ordini>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1