7

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.

2
  • Please provide enough code (XML, XSLT) to enable us to reproduce your problem. Commented May 15, 2014 at 10:59
  • 1
    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. Commented May 15, 2014 at 12:34

2 Answers 2

8

Add namespace to Content in your XSLT:

<xsl:template name="Page">
    <Content xmlns="http://developer.com/">
Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.