Reputation: 29
I have a piece of XML data with information for multiple persons represented as -
<phoneContact>
<firstName>XXXXX</firstName>
<middleName>Y</middleName>
<lastName>ZZZZZ</lastName>
<phone>1234567890</phone>
</phoneContact>
<phoneContact>
<firstName>AAAA</firstName>
<middleName>B</middleName>
<lastName>CCCCC</lastName>
<phone>9876543210</phone>
</phoneContact>
There may be any number of persons available. I want to transform this into -
<phoneContact1>
<firstName>XXXXX</firstName>
<middleName>Y</middleName>
<lastName>ZZZZZ</lastName>
<phone>1234567890</phone>
</phoneContact1>
<phoneContact2>
<firstName>AAAA</firstName>
<middleName>B</middleName>
<lastName>CCCCC</lastName>
<phone>9876543210</phone>
</phoneContact2>
.. and so forth. How do I construct an XSL for-each code that creates multiple such different element names ?
Thank you for any help with this.
Upvotes: 0
Views: 195
Reputation: 73
we can implement using identity function as below
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/a/phoneContact">
<xsl:element name="phoneContact{position()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
Upvotes: 0
Reputation: 33000
Inside the loop over the phoneContact
elements you can use <xsl:element>
and the the position function to create your numbered phoneContacts
:
<xsl:for-each select="phoneContact">
<xsl:element name="phoneContact{position()}">
...
</xsl:element>
</xsl:for-each>
Upvotes: 1