Question

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.

Was it helpful?

Solution

Add namespace to Content in your XSLT:

<xsl:template name="Page">
    <Content xmlns="http://developer.com/">

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top