I have an xsl that copies a xml file and renames the root tag.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:abc="http://example.com">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<test>
<xsl:apply-templates select="node()|@*"/>
</test>
</xsl:template>
<!--xsl:template match="abc:set">
-<xsl:apply-templates select="node()|@*"/>-
</xsl:template-->
</xsl:stylesheet>
That works fine but when I uncomment the last block to handle some namespaced tags I got an error that the says that something is wrong with the copy statement. How can I match and transform namespaced tags?
You are likely getting an error because the
abc:setelement has attributes, and your template matching onabc:setis producing “naked” attributes that are not attached to an element.Since you are not copying the
abc:setelement (or creating an element) in the template match forabc:set, when theapply-templatesinside of that template applies the templates to the selectedabc:set/@*andabc:set/node(), then the attributes match the identity template and will be copied forward.You can verify whether that is the issue by taking the
@*out of the select statement for theapply-templates, like this:The template above will only process the child nodes of
abc:set.If your intent was to simply copy the
abc:set, then you don’t need a specific template matching on that element. The identity template will match on it and handle that for you.