Below is my example xml:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:something-well-formed">
<child1 attr1="a" attr2="b">
<child1 attr1="a" attr2="b"/>
</child1>
<child3/>
<child1 attr1="a" attr2="b">
<child2 attr1="a" attr2="b"/>
</child1>
</root>
Question 1: How do you count the number of child1 nodes? Below I pass the string "child1" to my "element" parameter but what's the correct xslt syntax to achieve this?
Here is my example XSLT
<xsl:param name="element"/>
<xsl:template match="@*|node()">
<root>
<name><xsl:value-of select="count(/root/*[local-name()='$element'])"/>
</name>
</root>
Question 2: Given the same example xml, how do you access the Nth child1 element? I want to set its attributes to a specified value, but again, tripping up on syntax. Here's my example attempt:
<xsl:param name="element" />
<xsl:param name="attributes" />
<xsl:param name="nodeNumber" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name(.)=$element][count(preceding::*[local-name()='$element'] | ancestor::*[local-name()='$element'])=number($nodeNumber)]">
<xsl:copy>
<xsl:apply-templates select="@*" />
<!-- Splits into separate key/value pairs elements -->
<xsl:variable name="attributesSeq" select="tokenize($attributes, ';')" />
<xsl:for-each select="$attributesSeq">
<xsl:variable name="attributesSeq" select="tokenize(., ',')" />
<xsl:variable name="key"
select="replace($attributesSeq[1], '"', '')" />
<xsl:variable name="value"
select="replace($attributesSeq[2], '"', '')" />
<xsl:attribute name="{$key}">
<xsl:value-of select="$value" />
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="node()[$nodeNumber]" />
</xsl:copy>
</xsl:template>
Thanks for the help!