2011-02-06 66 views
1

利弊, 我需要用「X」下面的文檔中標記「B」標籤轉換:XSLT - 取代reoccuring XML文檔的部分(任何級別)

<a> 
<B marker="true"> 
    <c> 
    <B marker="true"> 
     <d> 
     <B marker="true"> 
     </B> 
     <d> 
    </B> 
    </c> 
</B> 
</a> 

注重現的「B ',它可以出現在動態XML的任何深度。 這裏就是我所做的:

<xsl:template match="//*[@marker='true']"> 
    <X> 
     <xsl:copy-of select="./node()"/> 
    </X> 
</xsl:template> 

它爲最上面的「B」的標籤,但忽略了所有的嵌套的。

我想我知道問題是什麼 - 'copy-of'只是刷新最頂部'B'標籤的內容,而沒有對其進行評估。我能做些什麼來「複製」重新評估我的模板?

謝謝! Baruch。

+0

看到我的回答,利用基本的 「恆等轉換」 的格局。 – Flack 2011-02-06 12:47:38

回答

7

我會去身份轉換。

此代碼:

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

    <xsl:template match="B[@marker = 'true']"> 
     <X> 
      <xsl:apply-templates/> 
     </X> 
    </xsl:template> 
</xsl:stylesheet> 

在此XML輸入:

<a> 
    <B marker="true"> 
     <c test="test"> 
      testText 
      <B marker="true"> 
       <d> 
        testText2 
        <B marker="true"> 
         testText3 
        </B> 
       </d> 
      </B> 
      testText4 
     </c> 
     testText5 
    </B> 
</a> 

將提供這一正確的結果:

<a> 
    <X> 
     <c test="test"> 
      testText 
      <X> 
       <d> 
        testText2 
        <X> 
         testText3 
        </X> 
       </d></X> 
      testText4 
     </c> 
     testText5 
    </X> 
</a> 
+0

謝謝!我正在用自己的解決方案自己回答。不知道它有一個名字,雖然:) – JBaruch 2011-02-06 13:01:41