-
xslt question re transforming XML to HTML
How do you get the list of elements in an xml
document using xslt?
I am trying to find a generic way to create an html table that also
contains column captions (element name) without knowing the element
names in the xml file.
for example:
THIS XML file, using XSLT
<?xml version="1.0" ?>
<products>
<tool>
<name>Hammer</name>
<price>9.99</price>
</tool>
<tool>
<name>Hammer Deluxe</name>
<price>14.99</price>
</tool>
<tool>
<name>Wrench</name>
<price>11.49</price>
</tool>
</products>
TO PRODUCE HTML file that looks like this
tool name price
Hammer 9.99
Hammer 14.99
Wrench 11.49
xslt like this
<xsl:apply-templates select="tool/*" />
gives back
9.99
14.99
11.49
genericly gets values, but no element names
Thanks,
Ari
-
Re: xslt question re transforming XML to HTML
This template produces exactly the output you requested, but you should
understand that it's not exactly generic, because of the blank column. I
should think a generic version would use the second-level tag name as the
table caption rather than a header for a blank column. Better yet, you could
pass a parameter for the table caption. But this should get you started.
Note that it works only with XML documents that hold two-dimensional data in
this form--like your example document.
<root>
<col1name>
<col2name>col2 data</col2name>
<col3name>col3 data</col3name>
</col1name>
</root>
Russell Jones
Sr. Web Development Editor,
DevX.com
"Ari " <amenase@fhcrc.org> wrote in message news:3b6f3fce$1@news.devx.com...
>
> How do you get the list of elements in an xml
> document using xslt?
>
> I am trying to find a generic way to create an html table that also
> contains column captions (element name) without knowing the element
> names in the xml file.
>
> for example:
>
> THIS XML file, using XSLT
>
> <?xml version="1.0" ?>
> <products>
> <tool>
> <name>Hammer</name>
> <price>9.99</price>
> </tool>
> <tool>
> <name>Hammer Deluxe</name>
> <price>14.99</price>
> </tool>
> <tool>
> <name>Wrench</name>
> <price>11.49</price>
> </tool>
> </products>
>
> TO PRODUCE HTML file that looks like this
>
> tool name price
> Hammer 9.99
> Hammer 14.99
> Wrench 11.49
>
>
>
> xslt like this
> <xsl:apply-templates select="tool/*" />
>
> gives back
>
> 9.99
> 14.99
> 11.49
>
> genericly gets values, but no element names
>
>
> Thanks,
>
> Ari
>
-
Re: xslt question re transforming XML to HTML
Oops--here's the template that was supposed to accompany my last post.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl utput method="html"/>
<xsl:template match="/">
<table border="1">
<!-- write the headers -->
<xsl:apply-templates select="*[1]"/>
<!-- write the data -->
<xsl:for-each select="child::*/child::*">
<tr><td> </td>
<xsl:for-each select="child::*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="child::*[1]">
<tr>
<xsl:for-each select="child::*[1]">
<th><xsl:value-of select="name()"/></th>
<xsl:for-each select="child::*">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
"Russell Jones" <arj1@northstate.net> wrote in message
news:3b6f605c$1@news.devx.com...
> This template produces exactly the output you requested, but you should
> understand that it's not exactly generic, because of the blank column. I
> should think a generic version would use the second-level tag name as the
> table caption rather than a header for a blank column. Better yet, you
could
> pass a parameter for the table caption. But this should get you started.
>
> Note that it works only with XML documents that hold two-dimensional data
in
> this form--like your example document.
>
> <root>
> <col1name>
> <col2name>col2 data</col2name>
> <col3name>col3 data</col3name>
> </col1name>
> </root>
>
> Russell Jones
> Sr. Web Development Editor,
> DevX.com
>
> "Ari " <amenase@fhcrc.org> wrote in message
news:3b6f3fce$1@news.devx.com...
> >
> > How do you get the list of elements in an xml
> > document using xslt?
> >
> > I am trying to find a generic way to create an html table that also
> > contains column captions (element name) without knowing the element
> > names in the xml file.
> >
> > for example:
> >
> > THIS XML file, using XSLT
> >
> > <?xml version="1.0" ?>
> > <products>
> > <tool>
> > <name>Hammer</name>
> > <price>9.99</price>
> > </tool>
> > <tool>
> > <name>Hammer Deluxe</name>
> > <price>14.99</price>
> > </tool>
> > <tool>
> > <name>Wrench</name>
> > <price>11.49</price>
> > </tool>
> > </products>
> >
> > TO PRODUCE HTML file that looks like this
> >
> > tool name price
> > Hammer 9.99
> > Hammer 14.99
> > Wrench 11.49
> >
> >
> >
> > xslt like this
> > <xsl:apply-templates select="tool/*" />
> >
> > gives back
> >
> > 9.99
> > 14.99
> > 11.49
> >
> > genericly gets values, but no element names
> >
> >
> > Thanks,
> >
> > Ari
> >
>
>
-
Re: xslt question re transforming XML to HTML
Thank you!,
Ari
"Russell Jones" <arj1@northstate.net> wrote:
>Oops--here's the template that was supposed to accompany my last post.
>
><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>version="1.0">
><xsl utput method="html"/>
><xsl:template match="/">
> <table border="1">
> <!-- write the headers -->
> <xsl:apply-templates select="*[1]"/>
>
> <!-- write the data -->
> <xsl:for-each select="child::*/child::*">
> <tr><td>*</td>
> <xsl:for-each select="child::*">
> <td><xsl:value-of select="."/></td>
> </xsl:for-each>
> </tr>
> </xsl:for-each>
> </table>
></xsl:template>
><xsl:template match="child::*[1]">
> <tr>
> <xsl:for-each select="child::*[1]">
> <th><xsl:value-of select="name()"/></th>
> <xsl:for-each select="child::*">
> <th><xsl:value-of select="name()"/></th>
> </xsl:for-each>
> </xsl:for-each>
> </tr>
></xsl:template>
></xsl:stylesheet>
>
>"Russell Jones" <arj1@northstate.net> wrote in message
>news:3b6f605c$1@news.devx.com...
>> This template produces exactly the output you requested, but you should
>> understand that it's not exactly generic, because of the blank column.
I
>> should think a generic version would use the second-level tag name as
the
>> table caption rather than a header for a blank column. Better yet, you
>could
>> pass a parameter for the table caption. But this should get you started.
>>
>> Note that it works only with XML documents that hold two-dimensional data
>in
>> this form--like your example document.
>>
>> <root>
>> <col1name>
>> <col2name>col2 data</col2name>
>> <col3name>col3 data</col3name>
>> </col1name>
>> </root>
>>
>> Russell Jones
>> Sr. Web Development Editor,
>> DevX.com
>>
>> "Ari " <amenase@fhcrc.org> wrote in message
>news:3b6f3fce$1@news.devx.com...
>> >
>> > How do you get the list of elements in an xml
>> > document using xslt?
>> >
>> > I am trying to find a generic way to create an html table that also
>> > contains column captions (element name) without knowing the element
>> > names in the xml file.
>> >
>> > for example:
>> >
>> > THIS XML file, using XSLT
>> >
>> > <?xml version="1.0" ?>
>> > <products>
>> > <tool>
>> > <name>Hammer</name>
>> > <price>9.99</price>
>> > </tool>
>> > <tool>
>> > <name>Hammer Deluxe</name>
>> > <price>14.99</price>
>> > </tool>
>> > <tool>
>> > <name>Wrench</name>
>> > <price>11.49</price>
>> > </tool>
>> > </products>
>> >
>> > TO PRODUCE HTML file that looks like this
>> >
>> > tool name price
>> > Hammer 9.99
>> > Hammer 14.99
>> > Wrench 11.49
>> >
>> >
>> >
>> > xslt like this
>> > <xsl:apply-templates select="tool/*" />
>> >
>> > gives back
>> >
>> > 9.99
>> > 14.99
>> > 11.49
>> >
>> > genericly gets values, but no element names
>> >
>> >
>> > Thanks,
>> >
>> > Ari
>> >
>>
>>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks