Page 1 of 1

generic stylesheet for display xml values in html

Posted: Thu Sep 07, 2006 7:52 pm
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

Posted: Fri Sep 08, 2006 12:03 pm
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