I have an XML sample that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<location id="1">
<address>1600 Pennsylvania Avenue</address>
<address>211B Baker Street</address>
</location>
<location id="1">
<address>17 Cherry Tree Lane</address>
</location>
<location id="2">
<address>350 5th Avenue</address>
</location>
</root>
And I would like to generate output that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<result>
<location id="1">
<address addressId="1">1600 Pennsylvania Avenue</address>
<address addressId="2">211B Baker Street</address>
</location>
<location id="1">
<address addressId="3">17 Cherry Tree Lane</address>
</location>
<location id="2">
<address addressId="1">350 5th Avenue</address>
</location>
</result>
Thus, it addressIdreflects the sequence addressfor all instances locationwith the same attribute id.
I thought that <xsl:number>would be my answer, but my attempts failed:
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
<xsl:template match="/root">
<result>
<xsl:for-each select="location">
<location>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:for-each select="address">
<address>
<xsl:attribute name="addressId">
<xsl:number count="//location[@id = ../@id]/address" level="any" />
</xsl:attribute>
<xsl:value-of select="." />
</address>
</xsl:for-each>
</location>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
source
share