2014-02-18 134 views
0

我有一個XML,其中有n個GRTSource下的GRTSource節點。我只需要GRTSource的前5個節點,並且我想訪問表的詳細信息,而不管表是否是immidiate子表。XSL將所選節點的子節點與模板相關聯

這裏是演示文件:

<GRTReport> 
<GRTSource> 
     <title>Unified CM Cluster Name</title> 
     <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment> 
     <title></title> 
     <comment></comment> 
     <table summary='Cluster Information'> 
      <row> 
       <cell style='header'>Cluster Name</cell> 
       <cell style='header'>Publisher Name/IP</cell> 
      </row> 
      <row> 
       <cell>xyzw</cell> 
       <cell>xyz1234</cell> 
      </row> 
     </table> 
    </GRTSource> 
    <GRTSource> 
     <div> 
     <title>Unified CM Cluster Name</title> 
     <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment> 
     <title></title> 
     <comment></comment> 
     <table summary='Cluster Information'> 
      <row> 
       <cell style='header'>Cluster Name</cell> 
       <cell style='header'>Publisher Name/IP</cell> 
      </row> 
      <row> 
       <cell>xyzw</cell> 
       <cell>xyz1234</cell> 
      </row> 
     </table> 
    </div> 
    </GRTSource> 
    </GRTReport> 

這裏是我的xsl:

<xsl:template match="/"> 
     <html> 
      <body> 
       <xsl:apply-templates/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="GRTReport/GRTSource[position() &lt; 6]"> 

     <fieldset class="reportSourceFieldset"> 
      <legend> 
       <xsl:value-of select="title[1]"/> 
      </legend> 
      <xsl:apply-templates select="descendant::table"/> 
     </fieldset> 
     <br /> 

    </xsl:template> 

    <xsl:template match="table"> 
     <table class="reportData"> 
      <tr class="cuesTableBg">  
       <xsl:for-each select="row[position() = 1]/cell"> 
        <th> 
         <pre> 
          <xsl:value-of select="current()"/> 
         </pre> 
        </th> 
       </xsl:for-each> 
      </tr> 

      <xsl:for-each select="row[position() &gt; 1]"> 
       <tr> 
        <xsl:for-each select="cell"> 
         <xsl:choose> 
         <xsl:when test="current()[contains(@style,'fixedFormat')]"> 
         <td> 
          <pre><xsl:value-of select="current()"/></pre> 
         </td> 
         </xsl:when> 
         <xsl:otherwise> 
         <td> 
         <span> 
          <xsl:value-of select="current()"/></span> 
         </td> 
         </xsl:otherwise> 
         </xsl:choose> 
        </xsl:for-each> 
       </tr> 
      </xsl:for-each> 
     </table> 
     <br /> 
    </xsl:template> 

不知怎的,它正在計數GRTSources高達5,但該查詢接受外界的5 GRSource算作表好。據逸岸考慮網頁上的所有表,而不僅僅是前五名內GRTSource

回答

2

當你這樣做:

<xsl:template match="/"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 

你申請模板不分青紅於全部當前節點的子節點(本例中爲根節點)。 built-in template rules「允許遞歸處理在沒有通過樣式表中的顯式模板規則進行的成功模式匹配的情況下繼續」。這意味着模板從這裏被應用到<GRTReport>,從那裏到<GRTSource>元素,這些元素之前沒有被更明確的規則匹配,並且從那裏到他們的子女 - 突然間,您有一個匹配的expicit模板規則。

嘗試改變上面:

<xsl:template match="/"> 
     <html> 
      <body> 
       <xsl:apply-templates select="GRTReport/GRTSource[position() &lt; 6]"/> 
      </body> 
     </html> 
</xsl:template> 

,然後你可以改變這一點:

<xsl:template match="GRTReport/GRTSource[position() &lt; 6]"> 

到:

<xsl:template match="GRTReport/GRTSource"> 
0

也許你要考慮:

<xsl:template match="GRTReport/GRTSource"> 
     <xsl:choose> 
      <xsl:when test="count(preceding-sibling::GRTSource) + 1 &lt; 6"> 
       <fieldset class="reportSourceFieldset"> 
        <legend> 
         <xsl:value-of select="descendant::title[1]"/> 
        </legend> 
        <xsl:apply-templates select="descendant::table"/> 
       </fieldset> 
       <br /> 
      </xsl:when> 
      <xsl:otherwise/> 
     </xsl:choose> 
</xsl:template> 
+0

這是可能的,但不建議由於內的兩個複製模板。更好:使用一個''和''/'',分別。 – Tomalak

+0

@Tomalak,修改後的模板好嗎? –

+0

不,這是一樣的事情:你會*需要第二個模板來輸出位置5之後的''元素。這是一個重複性和不清晰的方法。 - 使用一個可輸出''元素的模板。它一定不在乎他們的立場。然後使用''來只輸出你想看到的元素。 – Tomalak