I have a input xml,in my xsl I call a template. The first tag inside the template gets displayed with empty xmlns attribute as shown below
<Section xmlns="">
Can this attribute be eliminated in xslt?
Please help me with this..
I'm just adding a sample of my code,
Input.xml:
<?xml version="1.0" encoding="utf-8"?>
<List>
<Sections>
<Section>
<Column>a</Column>
<Column>b</Column>
<Column>c</Column>
<Column>d</Column>
<Column>e</Column>
</Section>
</Sections>
</List>
Stylesheet.xsl
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="List">
<report xmlns="http://developer.com/">
<Views>
<xsl:call-template name="Page"/>
</Views>
</report>
</xsl:template>
<xsl:template name="Page">
<Content>
<xsl:for-each select="./Sections/Section">
<Columns>
<xsl:for-each select="./Column">
<Column>
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</Column>
</xsl:for-each>
</Columns>
</xsl:for-each>
</Content>
</xsl:template>
The output.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://developer.com/">
<Views>
<Content xmlns="">
<Columns>
<Column value="a"/>
<Column value="b"/>
<Column value="c"/>
<Column value="d"/>
<Column value="e"/>
</Columns>
</Content>
</Views>
I need the xmlns attribute in the <report> tag but not in the <Content> tag. This xmlns attribute comes because I have called a template and the first tag of that template is added with this attribute.
xmlns=""isn't an attribute, it's a namespace declaration. They look the same but they have different purposes, and you don't simply add or remove xmlns "attribtues", rather you make sure that you create elements in the correct namespaces in the first place and the serializer will take care of inserting whatever namespace declarations are necessary to make the output XML reflect the node tree you've created.