从输出xml中删除空xmlns命名空间

时间:2014-05-15 09:14:21

标签: xslt

我有一个输入xml,在我的xsl中我调用了一个模板。模板内的第一个标签显示为空的xmlns属性,如下所示

    <Section xmlns="">

可以在xslt中消除此属性吗?

请帮帮我..

我只是添加了我的代码示例

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>

output.xml看起来像

<?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>

我需要<report>标记中的xmlns属性,但不需要<Content>标记中的xmlns属性。这个xmlns属性的出现是因为我调用了一个模板,并且该模板的第一个标签添加了这个属性。

2 个答案:

答案 0 :(得分:5)

在XSLT中将命名空间添加到Content

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

答案 1 :(得分:3)

您需要将第二个模板更改为:

<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>

否则,您将把<Content>元素及其所有子元素放在没有命名空间中 - 结果文档必须反映出来。