[英]Using arrays in XSLT 1.0 to build a table with uneven number of rows
我有一個如下的XML結構:
<people>
<attending>
<person><firstname>John</firstname><lastname>Doe</lastname></person>
<person><firstname>George</firstname><lastname>Washington</lastname></person>
<person><firstname>Henry</firstname><lastname>Dodson</lastname></person>
</attending>
<maybe>
<person><firstname>Jackie</firstname><lastname>Gleason</lastname></person>
<person><firstname>Jill</firstname><lastname>Hill</lastname></person>
</maybe>
</people>
我想使用XSLT 1.0構建單個HTML表,其中包含attending
信息以及maybe
包含元素的信息,但是永遠不能保證它們具有相同數量的元素。 我希望表格看起來像這樣(或類似形式):
<table>
<tr>
<th>Attending</th><th>Maybe</th>
</tr>
<tr>
<td>John Doe</td><td>Jackie Gleason</td>
</tr>
<tr>
<td>George Washington</td><td>Jill Hill</td>
</tr>
<tr>
<td>Henry Dodson</td><td> </td>
</tr>
</table>
因此,由於我一次只能對一個元素執行xsl:for-each
,因此我可以構建兩個單獨的表(一次一個列),並將每個表並排放置在一個更大的,包含表。 但是, 我需要一張桌子 。 (如果您想知道為什么,這是出於跨瀏覽器樣式的原因,並且表中的表變得難以控制跨瀏覽器。一個表可以緩解很多這種情況。)
下一個顯而易見的事情是構建兩個數組,一個具有attending
節點集,一個具有maybe
節點集,然后執行基於索引的xsl:for-each
,因為我當然會在每個數組中查找索引HTML表需要一次建立一行,但是不幸的是我的數據存儲為列。 另外,XSLT事先不知道每次attending
, maybe
不知道會attending
多少次,因此它必須能夠動態處理。
xsl:for-each
迭代? (與$attending[index]
,其中index
是我的“ for each”計數器) 我希望獲得XSLT 1.0的答案,因為這是我受約束的框架,但是我很樂意聽到有關如何在更高版本的XSLT中完成此操作的信息。
您可以通過以下一種方式查看它:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/people">
<table>
<tr>
<th>Attending</th>
<th>Maybe</th>
</tr>
<xsl:call-template name="rows"/>
</table>
</xsl:template>
<xsl:template name="rows">
<xsl:param name="i" select="1"/>
<xsl:if test="*/person[$i]">
<tr>
<td>
<xsl:apply-templates select="attending/person[$i]"/>
</td>
<td>
<xsl:apply-templates select="maybe/person[$i]"/>
</td>
</tr>
<xsl:call-template name="rows">
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="firstname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastname"/>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.