Reputation: 15
Suppose I have an xml file like this
<a>
<b>
<c>
<n xmlns="http://www.abcd.com/play/soccer"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.abcd.com/bgft">
<document>
<docbody>
......
......
......
</docbody>
</document>
</n>
</c>
</b>
</a>
i want to render that xml and copy that part using xslt under a new element . but the problem is i can not render that xml with those name spaces in element. so i have to remove those namespaces through xslt but i need those name spaces in my output xml. My output xml should be like that.
<m>
<n>
<o>
<n xmlns="http://www.abcd.com/play/soccer"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.abcd.com/bgft">
<abc>
<document>
<docbody>
......
......
......
</docbody>
</document>
</abc>
</n>
</o>
</n>
</m>
here is a new element
how can i pass through element removing namespaces and copy amd retain namespaces of in final output? Please help.
Upvotes: 0
Views: 994
Reputation: 122364
There's no "adding" or "removing" of any namespaces here, you're simply translating element names a
, b
, and c
(in no namespace) to m
, n
and o
(also in no namespace) and adding an abc
element in the http://www.abcd.com/play/soccer
namespace between the original {http://www.abcd.com/play/soccer}n
element and its children.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:play="http://www.abcd.com/play/soccer"
exclude-result-prefixes="play">
<!-- identity template - copy everything as-is unless we say otherwise -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="a">
<m><xsl:apply-templates select="@*|node()" /></m>
</xsl:template>
<xsl:template match="b">
<n><xsl:apply-templates select="@*|node()" /></n>
</xsl:template>
<xsl:template match="c">
<o><xsl:apply-templates select="@*|node()" /></o>
</xsl:template>
<!-- Match the original <n xmlns="http://www.abcd.com/play/soccer"> element.
We have to use a prefix for this because no prefix always means no
namespace in XPath -->
<xsl:template match="play:n">
<xsl:copy>
<!-- preserve the xsi:schemaLocation attribute -->
<xsl:apply-templates select="@*" />
<!-- insert an abc element in the right namespace -->
<abc xmlns="http://www.abcd.com/play/soccer">
<xsl:apply-templates />
</abc>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 167401
I don't understand why you want to remove and then restore the namespaces. If you simply copy the child nodes of the c
element while transforming it to a o
element you are done:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="c">
<o>
<xsl:apply-templates/>
</o>
</xsl:template>
<xsl:template match="a">
<m>
<xsl:apply-templates/>
</m>
</xsl:template>
<xsl:template match="b">
<n>
<xsl:apply-templates/>
</n>
</xsl:template>
Upvotes: 1