2013-06-22 135 views
0

我試圖刪除文本節點,而不從XML文檔的成功,這是我使用XSLT:XSLT刪除所有文本節點

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

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

    <xsl:template match="/*/*"> 
     <xsl:element name="x"> 
      <xsl:attribute name="attr"> 
       <xsl:value-of select="name()"/> 
      </xsl:attribute> 
      <xsl:apply-templates select="node()" /> 
     </xsl:element> 
    </xsl:template> 

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

    <xsl:template match="/*/*/a/*"> 
     <xsl:copy-of select="node()"/> 
    </xsl:template> 

    <xsl:template match="/*/*/*"> 
     <xsl:copy-of select="node()"/> 
    </xsl:template> 

    <xsl:template match="/*/*/*[not(self::a)]" /> 
    <xsl:template match="text()" /> 

</xsl:stylesheet> 

<xsl:template match="text()">可能它不工作,因爲其他線更具體(我認爲),我該如何去除所有文本節點?

回答

3

您的用於抑制文本節點的模板將抑制爲其尋找匹配模板的所有文本節點。但是它不會抑制所有文本節點,因爲並非所有文本節點都使用apply-templates進行處理。當您遇到一些節點時(匹配模式爲/*/*/a/*/*/*/*的節點),您正在複製其所有子節點而不向其應用模板;如果其中一些子節點是文本節點,或者其他子節點具有文​​本節點子節點,那些文本節點正在逃避你的鐮刀,然後擺脫複製的電話,然後堅持用複製模板複製

+0

非常感謝。 – MoreOver