2014-08-27 145 views
0

這裏是我的XML源刪除屬性各個節點在XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/"> 
    <GrandParent> 
    <Parent> 
     <Child xmlns:tns="http://www.domain.com/"> 
      <Age>3</Age> 
      <Gender>Male</Gender> 
      <Name>Todd</Name> 
     </Child> 
     <Other>1234</Other> 
    </Parent> 
    </GrandParent> 
</tns:Grand_Parent_XML> 

這裏就是我使用XSLT的身體......

<xsl:template match="node()|@*"> 
      <xsl:copy> 
        <xsl:apply-templates select="node()|@*"/> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template match="Grand_Parent_XML"> 
      <xsl:element name="tns:{name()}" namespace="http://www.domain.com/"> 
        <xsl:copy-of select="namespace::*"/> 
        <xsl:apply-templates select="node()|@*"/> 
      </xsl:element> 
     </xsl:template> 

     <xsl:template match="node()|@*"> 
     <xsl:if test="normalize-space(string(.)) != ''"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
     </xsl:if> 
     </xsl:template> 

<xsl:template match="Parent"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <Child> 
      <xsl:apply-templates select="Child/*"/> 
     </Child> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Child"/> 

我想刪除的xmlns:TNS =「」,並將其保留在Grand_Parent_XML中。我嘗試了其他建議,例如在XSLT底部創建以下代碼,但它不起作用。任何幫助將不勝感激。

<xsl:template match="Child"> 
    <xsl:apply-templates /> 
</xsl:template> 
+3

'xmlns:tns =「」'無效。前綴名稱空間綁定可能不爲空。 – 2014-08-27 12:22:39

+0

您的輸入XML格式不正確。如果您不更改名稱空間定義,則樣式表將不接受它作爲輸入。或者將XML版本設置爲「1.1」。 – 2014-08-27 12:23:50

+0

對不起,我省略了原來的內容。我現在就更新它。 – edpalomo 2014-08-27 12:26:10

回答

0

試試這個:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

我已經使用了撒克遜6.5.5 – 2014-08-27 12:29:45

1

一個簡單的身份變換去除冗餘空間聲明,僅此而已。用撒克遜6.5和9.5測試。

你的編輯之前,你的XML輸入了一個命名空間聲明一樣

xmlns:tns="" 

注意undeclaring命名空間是XML 1.1 XML 1.0違法,但有可能。

<?xml version="1.1" encoding="UTF-8"?> 

樣式

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

XML輸入

<?xml version="1.0" encoding="UTF-8"?> 
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/"> 
    <GrandParent> 
    <Parent> 
     <Child xmlns:tns="http://www.domain.com/"> 
      <Age>3</Age> 
      <Gender>Male</Gender> 
      <Name>Todd</Name> 
     </Child> 
     <Other>1234</Other> 
    </Parent> 
    </GrandParent> 
</tns:Grand_Parent_XML> 

:如果你改變了序言文件的下面的XML解析器不會引發錯誤XML輸出

<?xml version="1.0" encoding="utf-8"?> 
<tns:Grand_Parent_XML xmlns:tns="http://www.domain.com/"> 
    <GrandParent> 
     <Parent> 
     <Child> 
      <Age>3</Age> 
      <Gender>Male</Gender> 
      <Name>Todd</Name> 
     </Child> 
     <Other>1234</Other> 
     </Parent> 
    </GrandParent> 
</tns:Grand_Parent_XML>