generic stylesheet for display xml values in html

Here should go questions about transforming XML with XSLT and FOP.
vijay
Posts: 1
Joined: Thu Sep 07, 2006 7:19 pm

generic stylesheet for display xml values in html

Post by vijay »

Hi ALL ,

My system is generating the Xml file
example :-- <root>
<object>
<first>1</first>
<second>2</second>
<third/>
<four>4</four>
</object>
<object>
<first>1</first>
<second>2</second>
<third/>
<four>4</four>
</object>
</root>

i want to do print the values in html using xslt. but the thing is i donot know how many childs inside object and that to with values and without values .I wnat to print only cahild values in table


my output is just like

in html table
first second four
1 2 4
1 2 4
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

For an input XML file with your structure (root/object/other elements) you can find below a stylesheet that generates the HTML table that you want. If you want to apply different styles to the elements of the HTML result or to add new elements just modify this stylesheet.

Code: Select all

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

<xsl:template match="root">
<html>
<head>
<title>A table with only child elements with text content</title>
</head>
<body>
<table border="1">
<tr>
<xsl:apply-templates select="object[1]/*[string-length() > 0]" mode="header"/>
</tr>
<xsl:apply-templates select="*" mode="rows"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="*" mode="header">
<td>
<xsl:value-of select="local-name()"/>
</td>
</xsl:template>

<xsl:template match="object" mode="rows">
<tr>
<xsl:apply-templates select="*[string-length() > 0]" mode="rows"/>
</tr>
</xsl:template>

<xsl:template match="*" mode="rows">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
Regards,
Sorin
Post Reply