User 4.5.5
User 4.5.5

Reputation: 1321

how to get a child tag along with different parent tag from an xml using xslt

Consider the below input xml file

 <Content>
      <content1>
         <first> Hi  <dynVar name="abc" /> All </first>
         <second>this is</second>
      <content1>
      <third>input <dynVar name="def" /> xml content</third>
      <fourth> <dynVar name="ghi" /> </fourth>
      <fifth> <dynVar name="jkl" /> <dynVar name="mno" /></fifth>
 <Content>

using the above xml file i want to write an xslt so that my output xml file after transformation will look like below Target xml file :

 <aaa>
     <bbb>
         <ccc> Hi  <dynVar name="abc" /> All </ccc>
         <ddd>this is</ddd>
     <bbb>
     <eee>input <dynVar name="def" /> xml content</eee>
     <fff> <dynVar name="ghi" /> </fff>
     <ggg> <dynVar name="jkl" /> <dynVar name="mno" /></ggg>
 <aaa>

and the output file should not contain any of the namespaces associated with the input xml file Can anyone give a solution for this?

Upvotes: 0

Views: 473

Answers (1)

Maestro13
Maestro13

Reputation: 3696

Try this (also see shorter version at the end):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Content">
        <aaa>
            <xsl:apply-templates/>
        </aaa>
    </xsl:template>

    <xsl:template match="content1">
        <bbb>
            <xsl:apply-templates/>
        </bbb>
    </xsl:template>

    <xsl:template match="first">
        <ccc>
            <xsl:apply-templates/>
        </ccc>
    </xsl:template>

    <xsl:template match="second">
        <ddd>
            <xsl:apply-templates/>
        </ddd>
    </xsl:template>

    <xsl:template match="third">
        <eee>
            <xsl:apply-templates/>
        </eee>
    </xsl:template>

    <xsl:template match="fourth">
        <fff>
            <xsl:apply-templates/>
        </fff>
    </xsl:template>

    <xsl:template match="fifth">
        <ggg>
            <xsl:apply-templates/>
        </ggg>
    </xsl:template>

</xsl:stylesheet>

Applied to your input, this gives

<?xml version="1.0" encoding="UTF-8"?>
<aaa>
    <bbb>
        <ccc> Hi  <dynVar name="abc"/> All </ccc>
        <ddd>this is</ddd>
    </bbb>
    <eee>input <dynVar name="def"/> xml content</eee>
    <fff>
        <dynVar name="ghi"/>
    </fff>
    <ggg>
        <dynVar name="jkl"/>
        <dynVar name="mno"/>
    </ggg>
</aaa>

For excluding namespaces, add attribute exclude-result-prefixes="x y z" to the stylesheet element, where x, y and z are the namespaces declared additionally.

A shorter version that achieves exactly the same, but does not have a template for each node for which the node name has to be replaced:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[name() != 'dynVar']">
        <xsl:variable name="eltName">
            <xsl:choose>
                <xsl:when test="name()='Content'">aaa</xsl:when>
                <xsl:when test="name()='content1'">bbb</xsl:when>
                <xsl:when test="name()='first'">ccc</xsl:when>
                <xsl:when test="name()='second'">ddd</xsl:when>
                <xsl:when test="name()='third'">eee</xsl:when>
                <xsl:when test="name()='fourth'">fff</xsl:when>
                <xsl:when test="name()='fifth'">ggg</xsl:when>
                <xsl:otherwise>error</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:element name="{$eltName}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions