rakesh marsonio
rakesh marsonio

Reputation: 37

Confused in XSLT scripting

hi i want to create an HTML table that shows the numbers(Years) in one column and the data in the second column. below is my xslt. i'm really confused as they have the same tags.

<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>

Thanks

Upvotes: 0

Views: 57

Answers (2)

Pablo Pozo
Pablo Pozo

Reputation: 1920

I am assuming that the first two elements of the XML that you posted are wrapped by a <row> element and all the rows are grouped under a parent element called rows.

If some of those assumptions are wrong, tell me and I will correct the code.

<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>

UPDATE : If you just want to match the first entry/para element for each row, then you should use a template like the following one:

<xsl:template match="entry[1]/para">
    <!-- Put your code here -->
</xsl:template>

Upvotes: 1

Fishcake
Fishcake

Reputation: 10754

Here's an example of creating a HTML table using XSLT. The dataset used in the example is very similar to your XML, just substitute the examples <book> tags with your <row> tags and so on.

Upvotes: 0

Related Questions