itsMe
itsMe

Reputation: 785

XSLT conversion in two XML format

this is the source code

    <data t1="a" t2="b" t3="c">
        <name ans="d">xyz</name>
    </data>

I want to get

<info name="a">
    <info name="b">
        <info name="c">
            <answer name="d">xyz</answer>
        </info>
    </info>
</info>

but I have having serious problem regarding this .

the following XSL I have tried

<xsl:template match="*/*">
   <info>
        <xsl:element name="{node-name(.)}">
           <info>
            <xsl:for-each select="*/item">
                <xsl:element name="{node-name(..)}">
                  <xsl:copy-of select="./*" />
                </xsl:element>
            </xsl:for-each>
           </info> 
        </xsl:element>
    </info>
    </xsl:template>

Upvotes: 0

Views: 37

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

Your single example does not provide the rules. The following stylesheet will provide the expected result, but it could be just a coincidence:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/data">
    <info name="{@t1}">
        <info name="{@t2}">
            <info name="{@t3}">
                <xsl:apply-templates/>
            </info>
        </info>
    </info>
</xsl:template>

<xsl:template match="name">
    <answer name="{@ans}">
        <xsl:value-of select="."/>
    </answer>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions