XSL newbie question

Discussion in 'XML' started by Christoph, Mar 29, 2005.

  1. Christoph

    Christoph Guest

    Given the following sample xml document:

    <resultdata>
    <row>
    <field1>Field 1 Value</field1>
    <field2>Field 2 Value</field2>
    <field3>Field 3 Value</field3>
    </row>
    <row>
    <field1>Field 1 Value</field1>
    <field2>Field 2 Value</field2>
    <field3>Field 3 Value</field3>
    </row>
    </resultdata>

    what would the XSL look like such that it produces the
    following HTML

    <table>
    <tr><th>field1</th><th>field2</th><th>field3</th></tr>
    <tr><td>Field 1 Value</td><td>Field 2 Value</td><td>Field 3
    Value</td></tr>
    <tr><td>Field 1 Value</td><td>Field 2 Value</td><td>Field 3
    Value</td></tr>
    </table>

    without (and here's what I can't figure out) hardcoding
    any of the node names? I'd like to come up with a generic
    XSLT that can handle any xml document representing
    db table rows, regardless of the table. I just can't figure
    out how to make the first HTML table row (consisting
    of the TH's) to contain just the unique node names and
    then each subsequent HTML table row list out the values
    of each <row> child node. I have no problem coming
    up with the requisite XSLT if I hard code everything.
    But I just can't figure out how to do it dynamically.

    Any pointers would be greatly appreciated!

    thnx,
    Christoph
    Christoph, Mar 29, 2005
    #1
    1. Advertising

  2. Christoph wrote:

    > Given the following sample xml document:
    >
    > <resultdata>
    > <row>
    > <field1>Field 1 Value</field1>
    > <field2>Field 2 Value</field2>
    > <field3>Field 3 Value</field3>
    > </row>
    > <row>
    > <field1>Field 1 Value</field1>
    > <field2>Field 2 Value</field2>
    > <field3>Field 3 Value</field3>
    > </row>
    > </resultdata>
    >
    > what would the XSL look like such that it produces the
    > following HTML
    >
    > <table>
    > <tr><th>field1</th><th>field2</th><th>field3</th></tr>
    > <tr><td>Field 1 Value</td><td>Field 2 Value</td><td>Field 3
    > Value</td></tr>
    > <tr><td>Field 1 Value</td><td>Field 2 Value</td><td>Field 3
    > Value</td></tr>
    > </table>
    >
    > without (and here's what I can't figure out) hardcoding
    > any of the node names? I'd like to come up with a generic
    > XSLT that can handle any xml document representing
    > db table rows, regardless of the table.


    Here is a solution, you can use the XPath function local-name() to get
    the name of an element to put that in the header and you can use modes
    to elegantly process the "field" elements twice, once for making the
    thead, once for filling the data in the tbody.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="html" indent="yes" />

    <xsl:template match="resultdata">
    <table>
    <thead>
    <tr>
    <xsl:apply-templates select="row[1]/*" mode="head-cells" />
    </tr>
    </thead>
    <tbody>
    <xsl:apply-templates select="row" />
    </tbody>
    </table>
    </xsl:template>

    <xsl:template match="*" mode="head-cells">
    <th><xsl:value-of select="local-name()" /></th>
    </xsl:template>

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

    <xsl:template match="*" mode="body-cells">
    <td><xsl:value-of select="." /></td>
    </xsl:template>

    </xsl:stylesheet>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 29, 2005
    #2
    1. Advertising

  3. something like:

    <xsl:template match="/*">
    <table>
    <tr>
    <xsl:for-each select="*[1]/*"><th><xsl:value-of-select="name()"/></th></xsl:for-each>
    </tr>
    <xsl:apply-templates/>
    </table>
    </xsl:template>

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

    <xsl:template match="/*/*/*">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>


    untested...

    David
    David Carlisle, Mar 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Flood
    Replies:
    0
    Views:
    1,017
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,729
    Kevin Flood
    Sep 13, 2004
  3. Vijay singh
    Replies:
    1
    Views:
    434
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  5. Rob Smegma
    Replies:
    1
    Views:
    1,808
    shaun
    Sep 26, 2005
Loading...

Share This Page