繁体   English   中英

在XSLT 1.0中使用数组来构建行数不均匀的表

[英]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>&nbsp;</td>
    </tr>
</table>

因此,由于我一次只能对一个元素执行xsl:for-each ,因此我可以构建两个单独的表(一次一个列),并将每个表并排放置在一个更大的,包含表。 但是, 我需要一张桌子 (如果您想知道为什么,这是出于跨浏览器样式的原因,并且表中的表变得难以控制跨浏览器。一个表可以缓解很多这种情况。)

下一个显而易见的事情是构建两个数组,一个具有attending节点集,一个具有maybe节点集,然后执行基于索引的xsl:for-each ,因为我当然会在每个数组中查找索引HTML表需要一次建立一行,但是不幸的是我的数据存储为列。 另外,XSLT事先不知道每次attendingmaybe不知道会attending多少次,因此它必须能够动态处理。

  • XSLT 1.0是否支持此类数组?
  • 如何在此类数组上进行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM