Reputation: 23
I have a scenario where I have to change the namespaces from an XML document from one set to another set. I could change the namespaces successfully but the properties of the fields are not getting copied, like attributes. I basically want to copy the whole XML, just change the namespaces.
For example, the XML
<ns1:Response xmlns:ns2="http://www.w3.org/TR/html4/2" xmlns:ns3="http://www.w3.org/TR/html4/3" xmlns:ns4="http://www.w3.org/TR/html4/4" xmlns:ns5="http://www.w3schools.com/furniture" xmlns:myns1="http://www.w3.org/TR/html4/1" xmlns:myns2="http://www.w3.org/TR/html4/2" xmlns:myns3="http://www.w3.org/TR/html4/3" xmlns:myns4="http://www.w3.org/TR/html4/4" xmlns:myns5="http://www.w3schools.com/furniture" xmlns:ns1="http://www.w3.org/TR/html4/1">
<ns2:task attribute="one">..</ns2:task>
<ns2:address>..</ns2:address>
<ns2:pin>..</ns2:pin>
<ns3:address>
<ns4:add1 attribute="2">..</ns4:add1>
<ns4:add2>..</ns4:add2>
<ns4:add3>
<ns5:asdf>..</ns5:asdf>
<ns5:qwe>..</ns5:qwe>
</ns4:add3>
<ns4:add4>..</ns4:add4>
</ns3:address>
<ns2:query>..</ns2:query>
</ns1:Response>
to be converted to
<?xml version="1.0" encoding="UTF-8"?>
<myns1:taskListResponse xmlns:ns2="http://www.w3.org/TR/html4/2" xmlns:ns3="http://www.w3.org/TR/html4/3" xmlns:ns4="http://www.w3.org/TR/html4/4" xmlns:ns5="http://www.w3schools.com/furniture" xmlns:myns1="http://www.w3.org/TR/html4/1" xmlns:myns2="http://www.w3.org/TR/html4/2" xmlns:myns3="http://www.w3.org/TR/html4/3" xmlns:myns4="http://www.w3.org/TR/html4/4" xmlns:myns5="http://www.w3schools.com/furniture" xmlns:ns1="http://www.w3.org/TR/html4/1">
<myns2:task attribute="one">..</myns2:task>
<myns2:address>..</myns2:address>
<myns2:pin>..</myns2:pin>
<myns3:address>
<myns4:add1 attribute="2">..</myns4:add1>
<myns4:add2>..</myns4:add2>
<myns4:add3>
<myns5:asdf>..</myns5:asdf>
<myns5:qwe>..</myns5:qwe>
</myns4:add3>
<myns4:add4>..</myns4:add4>
</myns3:address>
<myns2:query>..</myns2:query>
</myns1:taskListResponse>
I used the following XSLT to do it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns1="http://www.w3.org/TR/html4/1"
xmlns:myns2="http://www.w3.org/TR/html4/2"
xmlns:myns3="http://www.w3.org/TR/html4/3"
xmlns:myns4="http://www.w3.org/TR/html4/4"
xmlns:myns5="http://www.w3schools.com/furniture"
xmlns:ns1="http://www.w3.org/TR/html4/1"
xmlns:ns2="http://www.w3.org/TR/html4/2"
xmlns:ns3="http://www.w3.org/TR/html4/3"
xmlns:ns4="http://www.w3.org/TR/html4/4"
xmlns:ns5="http://www.w3schools.com/furniture">
<xsl:template match="*">
<myns1:taskListResponse>
<xsl:apply-templates/>
</myns1:taskListResponse>
</xsl:template>
<xsl:template match="ns2:*">
<xsl:element name="myns2:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns3:*">
<xsl:element name="myns3:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns4:*">
<xsl:element name="myns4:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns5:*">
<xsl:element name="myns5:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
All the namespaces are properly converted to another set as required, but the attributes within the fields are not copied. In this example, I've defined only two attributes, but there are many more attributes. Basically, I want to copy the whole document, just the namespaces to be replaced. Would you please help me with this?
Upvotes: 1
Views: 124
Reputation: 122364
<xsl:apply-templates/>
is equivalent to
<xsl:apply-templates select="child::node()"/>
and the child::
axis does not include attributes. If you add an extra template
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
then you can change all the apply-templates
calls into
<xsl:apply-templates select="@*|node()" />
to copy attributes as well as applying the element templates.
Upvotes: 1