2014-05-15 98 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>元素及其所有的孩子在沒有命名空間 - 由此產生的文件必須反映這一點。

相關問題