2013-04-29 24 views
1

我正在努力與一些基本的xslt分組,但未能明白事情出錯的地方。採取下面的XML快照:小組結束 - 我的文字在哪裏去了?

<ul> 
<li>This is a text only node</li> 
<li>This one contains some different ones, like an <img src="#" alt="dummy" />image and a break.<br />Also a link <a href="#"> for testing purposes</a>.</li> 
<li>This one contains some different ones, like an <img src="#" alt="dummy" />image but no break. Also a link <a href="#"> for testing purposes</a>.</li> 
</ul> 

我喜歡什麼實現的是,對於每一個[李]包含休息前的內容突破獲取的包裹,如果它不包含破壞整個內容都被包裹着。因此,希望的結果是如下:

<ul> 
<li> 
    <string>This is a text only node</string> 
</li> 
<li> 
    <string>This one contains some different ones, like an <img src="#" alt="dummy" />image and a break.</string> 
    <br/> 
    <string>Also a link <a href="#"> for testing purposes</a>. 
</li> 
<li> 
    <string>This one contains some different ones, like an <img src="#" alt="dummy"/>image but no break. Also a link <a href="#"> for testing purposes</a>.</string> 
</li> 

這是XSLT我在地方

<xsl:template match="li"> 
<xsl:copy> 
    <xsl:choose> 
     <xsl:when test="br"> 
     <!-- node contains a break so let's group --> 
      <xsl:for-each-group select="*" group-ending-with="br"> 
       <!-- copy all but the break --> 
       <string><xsl:copy-of select="current-group()[not(self::br)]"/></string> 
       <!-- and place break unless it's the last element, then we don't need it... --> 
       <xsl:if test="not(position() = last())"><br/></xsl:if> 
      </xsl:for-each-group> 
     </xsl:when> 
     <xsl:otherwise> 
      <string><xsl:apply-templates select="@* | node()"/></string> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

但這似乎也剝奪我的文字,這是不是我想要的......輸出我得到的是如下:

<ul> 
<li> 
    <string>This is a text only node</string> 
</li> 
<li> 
    <string> 
     <img src="#" alt="dummy"/> 
    </string> 
    <br/> 
    <string> 
     <a href="#"> for testing purposes</a> 
    </string> 
</li> 
<li> 
    <string>This one contains some different ones, like an <img src="#" alt="dummy"/>image but no break. Also a link <a href="#"> for testing purposes</a>.</string> 
</li> 

所以它從我的文本從當前組()。我在這裏俯瞰什麼?

回答

1

那麼你需要用<xsl:for-each-group select="node()" group-ending-with="br">來處理所有的子節點,而不是像你現在那樣簡單地處理元素子節點。