I have some questions about namespace inheritance. I'm trying to set the default namespace of an output document and have all the child elements of the document node belogn to that namespace. But I seem to be having some trouble. I cannot think of a better way to illustrate this than with a example. I apologize for the semi-large amount of code, but I think it best illustrates my point.
Input Source Document
Code:
<Top>
<A>Adata</A>
<B>Bdata</B>
<C>Cdata</C>
<D>
<D1>1data</D1>
<D2>2data</D2>
</D>
</Top>
Transformation
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Transaction" inherit-namespaces="yes" namespace="http://myuri.com">
<xsl:apply-templates select="Top" />
</xsl:element>
</xsl:template>
<xsl:template match="Top">
<xsl:element name="Category">
<xsl:attribute name="Source" select="A" />
<xsl:element name="Description">
<xsl:value-of select="B" />
</xsl:element>
<xsl:element name="Shortname">
<xsl:value-of select="C" />
</xsl:element>
<xsl:apply-templates select="D" />
</xsl:element>
</xsl:template>
<xsl:template match="D">
<!--More Elements & Attributes-->
</xsl:template>
</xsl:stylesheet>
Resulting document
Code:
<Transaction xmlns="http://myuri.com">
<Category Source="Adata" xmlns="">
<Description>Bdata</Description>
<Shortname>Cdata</Shortname>
</Category>
</Transaction>
Now when I check to see which namespaces that the elements belong to, I don't get what
I expect. Only the 'Transaction' node belongs to the '
http://myuri.com' namepsace. I used this transformation to check.
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:value-of select="local-name(.),namespace-uri(.)"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
</xsl:template>
</xsl:stylesheet>
The results say the following:
Transaction
http://myuri.com
Category {null}
Description {null}
Shortname {null}
I'm looking for a method to define a default namespace and force that namespace onto all the generated child elements of that document node. Also keep in mind that the real transformation that this will be applied to spans multiple <xsl:include>s and mutiple files. So the scope of the transformation is across many templates, call templates and global variables.
I really want the results that every element belogns to the default namespace defined at the document node.
Transaction
http://myuri.com
Category
http://myuri.com
Description
http://myuri.com
Shortname
http://myuri.com
Can to provide some insight as to how this might be done?
I really appreciate the help!
Thanks.