XSL newbie question

C

Christoph

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
 
M

Martin Honnen

Christoph said:
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>
 
D

David Carlisle

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top