I have an xml document that is structured somewhat like this :-
<catalog xmlns="format_old" xmlns:final="format_new">
<final:book>
<final:title>blah</final:title>
<final:author>more blah</final:author>
</final:book>
<book>
<description title="blah2"/>
<writer name="more blah2"/>
</book>
</catalog>
Obviously, this is a simplified version of the problem. What I want to do is to convert this into something like :-
<catalog xmlns="format_new">
<book>
<title>blah</title>
<author>more blah</author>
</book>
<book>
<title>blah2</title>
<author>more blah2</author>
</book>
</catalog>
The stylesheet that I have right now is something like this :-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:orig="format_old"
xmlns="format_new"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//orig:book">
<xsl:element name="title">
<xsl:value-of select="./orig:description/@title" />
</xsl:element>
<xsl:element name="author">
<xsl:value-of select="./orig:writer/@name" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This gives me an output like :-
<catalog xmlns="format_old">
<book xmlns="format_new">
<title>blah</title>
<author>more blah</author>
</book>
<book xmlns:orig="format_old" xmlns="format_new">
<title>blah2</title>
</author>more blah2</author>
</book>
</catalog>
There are two problems with this stylesheet :-
1.) (major issue) The root element gets copied over rather than changing the default namespace of the root element. So basically the catalog element would still be in the namespace format_old.
2.) (minor issue) This will convert the elements as :-
<book xmlns:orig="format_old" xmlns="format_new">
...
</book>
instead of picking up the namespace from the root element as keeping it as
<book>
...
</book>
What am I missing here? I’m using Xalan-C.
I think the following should do:
Saxon 6.5.5 transforms your input into