Hi all:
I have the following in my XSL which adds a xmlns to my XML.
<xsl:template match="root">
<xsl:element name="root" namespace="myXslLoc">
<xsl:attribute name="Name">Default</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
<xsl:template>
The above does add a xmlns attribute to the root element (the top level element). However it also added a xmlns to the subsequent element. This is how it turned out:
<root Name="Default" xmlns="myXslLoc">
<steps xmlns=""> <-- where did this attribute come from?
.
.
.
</steps>
</root>
I have no ideas where that xmlns in the steps element come from. I have no code that specifies the adding of the xmlns to the steps element. Below is a snipet of my xsd:
<xs:complexType name="root">
<xs:sequence>
<xs:element name="steps" type="steps" maxOccurs="1" MinOccurs="1"/>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="steps">
<xs:sequence>
<xs:element name="step" type="step" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
Is there something wrong in my xsl or xsd? I can not seem to figure out where the problem came from.
Thanks.
EDIT: Following Dimitre's transformation code, I have managed to insert the namespace attribute into the root element. However, more instances of namespace attribute appeared further down in the transformed xml document.
The below is what happened:
<root Name="Default" xmlns="myXslLoc">
<steps> <-- where did this attribute come from?
<step name="A">
.
</step>
.
.
<!-- the below is the final steps element -->
<step name="Z" xmlns=""> <-- this xmlns was unexpectedly added.
<steps xmlns="myXslLoc"> <-- this one as well.
.
.
.
</steps>
</step>
<step Name="Step manually added by identity transformation (addLastNode stuff)">
.
.
.
</step>
</steps>
</root>
The xsl looks something like this:
<xsl:template match="root">
<xsl:element name="root namespace="myXslLoc">
<xsl:attribute name="Name">Default</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
<xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{$addMyXslLoc}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
<xsl:template>
<xsl:param name="addMyXslLoc" select="'myXslLoc'"/>
<xsl:template match="/*/steps/step[position()=last()]">
<xsl:call-template name="identity"/>
<xsl:copy-of select="$addLastNodes"/>
</xsl:template>
<xsl:param name="addLastNodes">
<step Name="Total Cost">
<items>
<item name="A">
</item>
<item name="b">
</item>
</items>
</step>
</xsl:param>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
I liked how the namespace now appears on the root element (along with the Name attribute). But I now face the problem of unable to get rid of the namespaces which was inserted into the last element of the xml document excluding the one that is added via the transformation.
EDIT: Updated the addLastNodes xsl.