Xslt 从输出xml中删除空的xmlns命名空间
我有一个输入xml,在xsl中我称之为模板。模板中的第一个标记显示为空xmlns属性,如下所示Xslt 从输出xml中删除空的xmlns命名空间,xslt,Xslt,我有一个输入xml,在xsl中我称之为模板。模板中的第一个标记显示为空xmlns属性,如下所示 <Section xmlns=""> 这个属性可以在xslt中消除吗 请帮我做这个 我只是添加了一个代码示例 Input.xml: <?xml version="1.0" encoding="utf-8"?> <List> <Sections> <Section> <Column>a</Column> &
<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>
A.
B
C
D
E
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>
我需要
标记中的xmlns属性,但不需要
标记中的xmlns属性。这个xmlns属性之所以出现,是因为我调用了一个模板,并且该模板的第一个标记添加了这个属性。在XSLT中向内容添加名称空间:
<xsl:template name="Page">
<Content xmlns="http://developer.com/">
您需要将第二个模板更改为:
<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>
否则,您将把
元素及其所有子元素放在没有名称空间的位置,结果文档必须反映这一点。请提供足够的代码(XML、XSLT),使我们能够重现您的问题。xmlns=“”
不是属性,而是名称空间声明。它们看起来相同,但用途不同,您不需要简单地添加或删除xmlns“attribtues”,相反,您首先要确保在正确的名称空间中创建元素,并且序列化程序将负责插入任何必要的名称空间声明,以使输出XML反映您创建的节点树。