0

嗨,我想创建一个 HTML 表格,在一列中显示数字(年),在第二列中显示数据。下面是我的xslt。我真的很困惑,因为他们有相同的标签。

<chapter>
<row>
        <entry>
            <para>1984</para>
        </entry>
        <entry>
            <para>International Business Companies Act passed into
            law.</para>
        </entry>
    </row>
    <row>
        <entry>
            <para>2004</para>
        </entry>
        <entry>
            <para>BVI Business Companies Act passed into law, coming into
            force on 1 January 2005.</para>
        </entry>
    </row>
    <row>
        <entry>
            <para>2005</para>
        </entry>
        <entry>
            <para>All three corporate statutes exist in parallel and it is
            possible to incorporate companies under any of them.</para>
        </entry>
    </row>
    <row>
        <entry>
            <para>2006</para>
        </entry>
        <entry>
            <para>Incorporation provisions in the International Business
            Companies Act and the Companies Act are repealed on 31 December
            2005; the Acts remain in force but new companies may only be
            incorporated under the BVI Business Companies Act.</para>
        </entry>
    </row>
</chapter>

谢谢

4

2 回答 2

1

我假设您发布的 XML 的前两个元素由 <row> 元素包装,并且所有行都分组在称为行的父元素下。

如果其中一些假设是错误的,请告诉我,我会更正代码。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output mode="html" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="text()" />

    <!-- I am assuming that the parent element for the set of row elements is
         named rows. You can change this to match your XML -->
    <xsl:template match="chapter">
        <table>
            <tr>
                <th>Year</th>
                <th>Data</th>
            </tr>
            <xsl:apply-templates select="row" />
        </table>
    </xsl:template>

    <xsl:template match="row">
        <tr>
            <xsl:apply-templates select="*" />
        </tr>
    </xsl:template>

    <xsl:template match="para">
        <td><xsl:value-of select="." /></td>
    </xsl:template>

</xsl:stylesheet>

更新:如果您只想匹配每行的第一个 entry/para 元素,那么您应该使用如下模板:

<xsl:template match="entry[1]/para">
    <!-- Put your code here -->
</xsl:template>
于 2013-02-19T09:30:59.963 回答
0

下面是使用 XSLT创建 HTML 表的示例。示例中使用的数据集与您的 XML 非常相似,只需将示例<book>标签替换为您的<row>标签等等。

于 2013-02-19T09:17:48.040 回答