[英]Using arrays in XSLT 1.0 to build a table with uneven number of rows
I have an XML structure such as the following: 我有一个如下的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>
I want to build a single HTML table using XSLT 1.0 with the information in both attending
and maybe
elements, but they are never guaranteed to have the same amount of elements. 我想使用XSLT 1.0构建单个HTML表,其中包含
attending
信息以及maybe
包含元素的信息,但是永远不能保证它们具有相同数量的元素。 I'd like the table to look like this (or similar): 我希望表格看起来像这样(或类似形式):
<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>
So since I can only do an xsl:for-each
over one element at a time, I could build two separate tables (one column at a time), and put each table in two cells side-by-side of a larger, encompassing table. 因此,由于我一次只能对一个元素执行
xsl:for-each
,因此我可以构建两个单独的表(一次一个列),并将每个表并排放置在一个更大的,包含表。 But, I need one table . 但是, 我需要一张桌子 。 (If you'd like to know why, it's for cross-browser styling reasons, and tables within tables become difficult to control cross browser. One table would alleviate a lot of this.)
(如果您想知道为什么,这是出于跨浏览器样式的原因,并且表中的表变得难以控制跨浏览器。一个表可以缓解很多这种情况。)
The next obvious thing would be to build two arrays, one with the attending
node-set and one with the maybe
node-set, then do an index based xsl:for-each
where I look up the index in each array, because of course HTML tables need to be built one row at a time, but unfortunately my data is stored as columns. 下一个显而易见的事情是构建两个数组,一个具有
attending
节点集,一个具有maybe
节点集,然后执行基于索引的xsl:for-each
,因为我当然会在每个数组中查找索引HTML表需要一次建立一行,但是不幸的是我的数据存储为列。 Also, the XSLT doesn't know beforehand how many of each attending
or maybe
there will be, so it has to be able to dynamically handle it. 另外,XSLT事先不知道每次
attending
, maybe
不知道会attending
多少次,因此它必须能够动态处理。
xsl:for-each
iteration over such arrays? xsl:for-each
迭代? (As in $attending[index]
where index
is my "for each" counter) $attending[index]
,其中index
是我的“ for each”计数器) I would prefer answers for XSLT 1.0 since that is the framework I am constrained to, but I am open to hearing about how this could be done in later versions of XSLT. 我希望获得XSLT 1.0的答案,因为这是我受约束的框架,但是我很乐意听到有关如何在更高版本的XSLT中完成此操作的信息。
Here's one way you could look at it: 您可以通过以下一种方式查看它:
XSLT 1.0 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.