2011-07-20 40 views
0

在XSLT上很綠,我正在使用的系統之一是使用它在前端生成一些表。基本上執行鍼對DB2實例的查詢結果集被解析成XML和輸出類似於...XSLT在不使用for-each的情況下推進節點

<ResultSet> 
    <Row> 
     <node1></node1> 
     <node2></node2> 
     <etc> 
    </Row> 
    <Row> 
    </Row> 
</ResultSet> 

我不知道如何來推進的節點,而不需要使用的for-each循環。這是從我對XSLT內部變量的理解(這是有限的)。

在頁面末尾,我必須使用上面創建的變量創建一個表格。假設結果集是它將返回三行而不多/少。從我的xslt的一些代碼如下...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html"/> 
    <xsl:template match="/"> 
    ....after some html population... 
    <tbody> 
     <tr> 
      <td>Column</td> 
       <td class="rightAligned"> 
        <xsl:call-template name="formatAsCurrencyNoDecimals"> 
         <xsl:with-param name="numberToFormat" 
          select="summarizedLoads/summary/total" /> 
        </xsl:call-template> 
       </td> 
       .....xsl continues for this row.... 

一旦完成這一步需要做什麼來推進到下一行?我的是,我將不得不修改根模板匹配到<xsl:template match="/summarizedLoads/">,然後在每行後調用。

在每行的內部,我需要創建幾個變量供最終使用。

所有行也包含相同數量的數據。希望這是明確的,我想要做什麼,如果需要從我這裏任何其他事情,請讓我知道。

回答

0

在使用嵌套模板時,XSLT的甜蜜點在於。你已經有一個模板了;讓我們在當前匹配的地方之下再做一個匹配=「行」。在該模板中,執行所有特定行的內容。然後,你的主模板內調用它(匹配=「/」),您希望您的最終行像這樣:

<xsl:apply-templates select = "./Row[0]"/> 
<xsl:apply-templates select = "./Row[1]"/> 
<xsl:apply-templates select = "./Row[2]"/> 

如果你想所有的行,而不只是前3,你可以這樣做相反:

<xsl:apply-templates select = "./Row"/> 

點代表當前元素。由於我們在主模板中,所以這是根元素ResultSet。/Row意味着我們將與Row匹配的第一個模板應用於所有後代Row元素。

1

假設你有以下的XML:

<root> 
    <row>1</row> 
    <row>2</row> 
    <row>3</row> 
    <row>4</row> 
    <row>5</row> 
</root> 

只選擇3行,你可以使用XPath://row[position() &lt; 4],例如XSLT:

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

    <xsl:template match="/"> 
    <xsl:apply-templates select="//row[position() &lt; 4]"/> 
    </xsl:template> 

    <xsl:template match="row"> 
    <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出應該是:

<row>1</row> 
<row>2</row> 
<row>3</row> 
+0

很好的回答。 +1 – hoodaticus

相關問題