I’m trying to put correct value in the attribute @id based on the @rid attribute value.
Logic: If element a more then 1 and element cu equal to 1 in the ‘au’ element than how to add position after the attribute value e.g (<a id="a">...</a>, <a id="a">...</a>) s/b (<a id="a1">...</a>, <a id="a2">...</a>)
How to solve this issue template match only a element.
Please check Expecting Output:
Input:
<root>
<t>Title</t>
<au>
<cu>
<x rid="a1 a2"/>
</cu>
</au>
<a id="a">Centre</a>
<a id="a">Westmead</a>
<n>notes</n>
</root>
XSLT Code:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<xsl:if test="count(../a) > 1 and count(../au/cu[x]) = 1 and @id">
<xsl:for-each-group select="self::node()" group-adjacent="self::a">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="a">
<xsl:attribute name="id">
<xsl:value-of select="concat('a',$pos)"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
<!--</xsl:for-each>-->
</xsl:for-each-group>
</xsl:if>
</xsl:template>
Expecting Output:
<root>
<t>Title</t>
<au>
<cu>
<x rid="a1 a2"/>
</cu>
</au>
<a id="a1">Centre</a>
<a id="a2">Westmead</a>
<n>notes</n>
>Solution :
If you want to do it from the template for a use
<xsl:template match="root[a[2] and not(au/cu[x][2])]/a[@id]">
<xsl:copy>
<xsl:attribute name="id">a<xsl:number/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>