Sorry if this has been discussed before, but could nt find a suitable answer for my problem..... I have this xml:
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>
What I want is if any extra attribute comes with this xml at RootList\Root\Extn node apart from what is present, that attribute will be removed.
So if the input comes like:
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1" ExtnAtt2="2">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>
then the output will be like:
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>
For this I have written an xsl as:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
<RootList>
<xsl:for-each select="/RootList">
<xsl:element name="Root">
<xsl:element name="Extn">
<xsl:attribute name="ExtnAtt1">
<xsl:value-of select="/RootList/Root/Extn/@ExtnAtt1"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:for-each>
</RootList>
</xsl:template>
</xsl:stylesheet>
But this gives me the output as:
<?xml version="1.0" encoding="UTF-16"?>
<RootList>
<Root>
<Extn ExtnAtt1="1" />
</Root>
</RootList>
I want to preserve the existing xml and only want remove attributes that are extra.
Can anybody please help me with this.
Thanks in advance guys.
Thanks guys for all the replies...you guys rock.... I figured another method....let me know what do u think about this....
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
<RootList>
<xsl:for-each select="/RootList">
<xsl:element name="Root">
<xsl:copy-of select="/RootList/Root/@RootAtt1"/>
<xsl:copy-of select="/RootList/Root/@RootAtt2"/>
<xsl:copy-of select="/RootList/Root/@RootAtt3"/>
<xsl:element name="Extn">
<xsl:copy-of select="/RootList/Root/Extn/@ExtnAtt1"/>
<xsl:element name="Child">
<xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt1"/>
<xsl:copy-of select="/RootList/Root/Extn/Child/@ChildAtt2"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:for-each>
</RootList>
</xsl:template>
</xsl:stylesheet>
When this is applied on :
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1" ExtnAtt2="2">
<Child ChildAtt1="1" ChildAtt2="2"/>
</Extn>
</Root>
</RootList>
It gives an output as:
<?xml version="1.0" encoding="UTF-16"?>
<RootList>
<Root RootAtt1="1" RootAtt2="2" RootAtt3="3">
<Extn ExtnAtt1="1">
<Child ChildAtt1="1" ChildAtt2="2" />
</Extn>
</Root>
</RootList>
assuming that RootList\Root\Extn\@ExtnAtt2 is the unwanted attribute.
Thanks Awaiting your reply....:)
Cheers