Hi,
I have an XML:

<A>
  <B>
    <C>a</C>
  </B>
  <B>
     <C>b</C>
  </B>
  <B>
     <C>c</C>
  </B>
</A>

and I need the output as

<X name="a">
  <Y name="b">
    <Z name ="c"/>
  </Y>
</X>

i don't know how to write the XSL for it, tried few things but couldn't get the solution.

Tried XSL-

<xsl:variable name="i">
<xsl:value-of select="A/B/C"/>
</xsl:variable>
<X name={$i}>
  <xsl:variable name="j">
   <xsl:value-of select="A/B/C"/>
  </xsl:variable>
  <Y name={$j}>
      <xsl:variable name="k">
        <xsl:value-of select="A/B/C"/>
      </xsl:variable>
      <Z name={$k}/>
   </Y>
</X>

it gives output as

<X name="a">
  <Y name="a">
    <Z name ="a"/>
  </Y>
</X>

Please advise

Here is a non-recursion based solution:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:output method="xml" />

  <xsl:template match="/">
     <xsl:element name="X">
        <xsl:attribute name="name">
           <xsl:value-of select="//B[1]/C" />
        </xsl:attribute>
        <xsl:element name="Y">
           <xsl:attribute name="name">
              <xsl:value-of select="//B[2]/C" />
           </xsl:attribute>
           <xsl:element name="Z">
              <xsl:attribute name="name">
                 <xsl:value-of select="//B[3]/C" />
              </xsl:attribute>
           </xsl:element>
        </xsl:element>
     </xsl:element>
  </xsl:template>

</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.