2014-05-15 99 views
4

我有一个输入xml,在我的xsl中我调用了一个模板。模板中的第一个标签被用空的xmlns属性显示如下图所示从输出xml中删除空的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> 

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

+0

请提供足够的代码(XML,XSLT)以使我们能够重现您的问题。 –

+1

'xmlns =“”'不是一个属性,它是一个名称空间声明。它们看起来是一样的,但它们有不同的目的,而且不是简单地添加或删除xmlns“attribtues”,而是确保首先在正确的名称空间中创建元素,并且序列化程序将负责插入任何名称空间声明对于使输出XML反映您创建的节点树是必需的。 –

回答

5

添加命名空间在您的XSLT Content

<xsl:template name="Page"> 
    <Content xmlns="http://developer.com/"> 
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>元素及其所有的孩子在没有命名空间 - 由此产生的文件必须反映这一点。

相关问题