Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty xmlns namespace from output xml

Tags:

xslt

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.

like image 907
user1482807 Avatar asked May 15 '14 09:05

user1482807


2 Answers

Add namespace to Content in your XSLT:

<xsl:template name="Page">
    <Content xmlns="http://developer.com/">
like image 175
kjhughes Avatar answered Sep 20 '22 17:09

kjhughes


You need to change your second template to :

<xsl:template name="Page">
    <Content xmlns="http://developer.com/">
        <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>

Otherwise you will be putting the <Content> element and all its children in no namespace - and the resulting document must reflect that.

like image 21
michael.hor257k Avatar answered Sep 19 '22 17:09

michael.hor257k