Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL newbie question

Reply
Thread Tools

XSL newbie question

 
 
Christoph
Guest
Posts: n/a
 
      03-29-2005
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


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-29-2005


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput 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/
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      03-29-2005

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl to group elements? [xsl newbie] Rob Smegma XML 1 09-26-2005 10:59 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
XSLT question related to <xsl:template> and <xsl:apply-templates> Vijay singh XML 1 11-04-2004 03:42 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 0 09-08-2004 02:11 PM
Three question which is not yet answered clearly and correct so far !! challenging question in xsl and also in xsl fo Philip Meyer XML 0 11-30-2003 04:42 PM



Advertisments