2011-09-07 42 views
3

對不起,但我對xsl的瞭解並不是太好。
我有一些代碼從不同數量的節點構造3列的表格:XSL找到一個字符串,並從該點開始

<xsl:variable name="t-size" select="count(NewDataSet/VehicleDetail/Options/Option)"/> 
    <xsl:variable name="myCount" select="ceiling($t-size div 3)"/> 
    <table callpadding="4" cellspacing="0" border="0" id="specificationTbl"> 
    <xsl:for-each select="NewDataSet/VehicleDetail/Options/Option[position() &lt;= $myCount]"> 
     <xsl:variable name="here" select="position()"/> 
     <tr> 
      <td class="stiDetail"><xsl:value-of select="."/></td> 
      <td class="stiDetail"><xsl:choose> 
       <xsl:when test="../Option[$here+$myCount]"> 
        <xsl:value-of select="../Option[$here+$myCount]/."/> 
       </xsl:when> 
       <xsl:otherwise> 
       </xsl:otherwise> 
       </xsl:choose> 
      </td> 
      <td class="stiDetail"><xsl:choose> 
       <xsl:when test="../Option[$here+$myCount+$myCount]"> 
        <xsl:value-of select="../Option[$here+$myCount+$myCount]/."/> 
       </xsl:when> 
       <xsl:otherwise> 
       </xsl:otherwise> 
       </xsl:choose></td> 
     </tr> 
     </xsl:for-each> 
    </table></td> 

這工作得很好。但是我所追求的是循環掃描節點的字符串值,然後從包含字符串的節點之後的節點開始。

EG。下面的示例XML

<options> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>hello world</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
</options> 

所以在上面的XML,我想掃描包含單詞「世界」的節點,然後從以下節點IE啓動創建表。從位置6開始。

希望是有道理的。

任何幫助將受到最感激的接收。

非常感謝,

Andy。

+0

問得好,+1。查看我的答案,獲取選擇所需元素的較短的XPath表達式。 –

回答

0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="/options"> 
     <table> 
      <xsl:apply-templates select="option[contains(., 'world')] | option[contains(., 'world')]/following-sibling::option"/> 
     </table> 
    </xsl:template> 

    <xsl:template match="option"> 
     <tr> 
      <td> 
       <xsl:value-of select="."/> 
      </td> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 

輸出:

<table> 
    <tr> 
    <td>hello world</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>something</td> 
    </tr> 
</table> 
+0

不錯的一個。主要的大小感謝和大量的好業力來到你的路上。 – Andy

+0

@安迪,謝謝:-) –

0

使用

/*/*[(.|preceding-sibling::*)[contains(text(),'world')]] 

這個XPath表達式是可能是準確選擇想要的設置元素的最短。

基於XSLT的驗證

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:copy-of select= 
     "/*/*[(.|preceding-sibling::*)[contains(.,'world')]]"/> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<options> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>hello world</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
    <option>something</option> 
</options> 

有用,正確的結果產生

<option>hello world</option> 
<option>something</option> 
<option>something</option> 
<option>something</option> 
<option>something</option> 
<option>something</option> 
<option>something</option> 

或者,你可以有這種轉變

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="*[(.|preceding-sibling::*)[contains(text(),'world')]]"> 
    <xsl:copy-of select="."/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 
相關問題