我有一個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() < 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() > 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
這是可能的,但不建議由於內的兩個複製模板。更好:使用一個''和''/'',分別。 –
Tomalak
@Tomalak,修改後的模板好嗎? –
不,這是一樣的事情:你會*需要第二個模板來輸出位置5之後的''元素。這是一個重複性和不清晰的方法。 - 使用一個可輸出''元素的模板。它一定不在乎他們的立場。然後使用''來只輸出你想看到的元素。 –
Tomalak