0

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.

3
  • 2
    Are you sure you want to do this? It's not a good structure to have, as it makes it very difficult to select all phoneContacts for processing. Normally, you would use an attribute to distinguish an element from its siblings of same name. Commented Mar 1, 2016 at 18:10
  • Agreed. Your requested output XML form is a much poorer design than your input XML. What's your bigger picture goal? Commented Mar 1, 2016 at 18:17
  • Thank you for your response. Agree, that is a better structure. But I want to do this transform for another implementation downstream, where a DB table will have one record per person with the possibility of having multiple phone numbers. Commented Mar 1, 2016 at 18:44

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

And that worked perfectly, kind Sir. THANK YOU. Here is what I implemented finally - <xsl:for-each select="phoneContact"> <xsl:element name="phoneContact{position()}"> <firstName><xsl:value-of select="firstName"/></firstName> <middleName><xsl:value-of select="middleName"/></middleName> <lastName><xsl:value-of select="lastName"/></lastName> <phone><xsl:value-of select="phone"/></phone> </xsl:element> </xsl:for-each>
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.