473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Render XML structure using XSLT

Hi,
I'm trying to render a XML structure to HTML using XSLT.
My XML describe the header of a table with a complex and not linear
structure.
The first row of the header table always contains the ServiceName
(attribute name), under I have to show all the other fields with
correct structure.
Can you help me to write XSLT document?

My XML doc:
************************************************** ****************************
<?xml version="1.0" encoding="UTF-8"?>
<data>
<Service depth="6" name="service1" totalDepth="2">
<Field depth="0" name="1.1"/>
<Field depth="0" name="1.2"/>
<Field depth="0" name="1.3"/>
<Field depth="0" name="1.4"/>
<Field depth="0" name="1.5"/>
<Field depth="0" name="1.6"/>
</Service>
<Service depth="8" name="service2" totalDepth="4">
<Field depth="4" name="2.1">
<Field depth="4" name="2.1.1">
<Field depth="0" name="2.1.1.1"/>
<Field depth="0" name="2.1.1.2"/>
<Field depth="0" name="2.1.1.3"/>
<Field depth="0" name="2.1.1.4"/>
</Field>
<Field depth="4" name="2.1.2">
<Field depth="0" name="2.1.2.1"/>
<Field depth="0" name="2.1.2.2"/>
<Field depth="0" name="2.1.2.3."/>
<Field depth="0" name="2.1.2.4"/>
</Field>
<Field depth="4" name="2.1.3.">
<Field depth="0" name="2.1.3.1"/>
<Field depth="0" name="2.1.3.2"/>
<Field depth="0" name="2.1.3.3"/>
<Field depth="0" name="2.1.2.4"/>
</Field>
<Field depth="4" name="2.1.4">
<Field depth="0" name="2.1.4.1"/>
<Field depth="0" name="2.1.4.2"/>
<Field depth="0" name="2.1.4.3"/>
<Field depth="0" name="2.1.4.4"/>
</Field>
</Field>
<Field depth="4" name="2.2">
<Field depth="0" name="2.2.1"/>
<Field depth="0" name="2.2.2"/>
<Field depth="0" name="2.2.3"/>
<Field depth="0" name="2.2.4"/>
</Field>
</Service>
</data>

This is what I want to obtain:
************************************************** ****************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My Doc</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr align="center">
<td colspan="6">service1</td>
<td colspan="20">service2</td>
</tr>
<tr>
<td rowspan="3">1.1</td>
<td rowspan="3">1.2</td>
<td rowspan="3">1.3</td>
<td rowspan="3">1.4</td>
<td rowspan="3">1.5</td>
<td rowspan="3">1.6</td>
<td colspan="16">2.1</td>
<td colspan="4" rowspan="2">2.2</td>
</tr>
<tr>
<td colspan="4">2.1.1</td>
<td colspan="4">2.1.2</td>
<td colspan="4">2.1.3</td>
<td colspan="4">2.1.4</td>
</tr>
<tr>
<td>2.1.1.1</td>
<td>2.1.1.2</td>
<td>2.1.1.3</td>
<td>2.1.1.4</td>
<td>2.1.2.1</td>
<td>2.1.2.2</td>
<td>2.1.2.3.</td>
<td>2.1.2.4</td>
<td>2.1.3.1</td>
<td>2.1.3.2</td>
<td>2.1.3.3</td>
<td>2.1.2.4</td>
<td>2.1.4.1</td>
<td>2.1.4.2</td>
<td>2.1.4.3</td>
<td>2.1.4.4</td>
<td>2.2.1</td>
<td>2.2.2</td>
<td>2.2.3</td>
<td>2.2.4</td>
</tr>
</table>
</body>
</html>

Thanks.

Jul 20 '05 #1
2 2171
Sylvia wrote:
I'm trying to render a XML structure to HTML using XSLT.
My XML describe the header of a table with a complex and not linear
structure.
The first row of the header table always contains the ServiceName
(attribute name), under I have to show all the other fields with
correct structure.
Can you help me to write XSLT document?


The following might be helpful:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
method="html" indent="yes" />

<xsl:template match="data">
<html>
<head>
<title>My Doc</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr align="center">
<xsl:apply-templates select="Service" />
</tr>
<tr>
<xsl:apply-templates select="Service/Field" />
</tr>
<tr>
<xsl:apply-templates select="Service/Field/Field[child::*]" />
</tr>
<tr>
<xsl:apply-templates select="child::Service/Field/Field/Field" />
<xsl:apply-templates select="Service/Field/Field[not(child::*)]" />
</tr>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="Service">
<td colspan="{count(descendant-or-self::Field[@depth='0'])}">
<xsl:value-of select="@name" />
</td>
</xsl:template>

<xsl:template match="Service/Field">
<td>
<xsl:call-template name="colspan" />
<xsl:call-template name="rowspan" />
<xsl:value-of select="@name" />
</td>
</xsl:template>

<xsl:template match="Service/Field/Field[child::*]">
<td>
<xsl:call-template name="colspan" />
<xsl:value-of select="@name" />
</td>
</xsl:template>

<xsl:template match="child::Service/Field/Field/Field">
<td>
<xsl:value-of select="@name" />
</td>
</xsl:template>

<xsl:template match="Service/Field/Field[not(child::*)]">
<td>
<xsl:value-of select="@name" />
</td>
</xsl:template>

<xsl:template name="colspan">
<xsl:if test="count(descendant::Field/Field) &gt; 1
or count(descendant::Field) &gt; 1">
<xsl:attribute name="colspan">
<xsl:choose>
<xsl:when test="count(descendant::Field/Field) &gt; 1">
<xsl:value-of select="count(descendant::Field/Field)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(descendant::Field)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
</xsl:template>

<xsl:template name="rowspan">
<xsl:if test="not(descendant::Field) or (. = //Service[2]/Field[2])">
<xsl:attribute name="rowspan">
<xsl:choose>
<xsl:when test="not(descendant::Field)">
<xsl:value-of select="count(ancestor-or-self::*)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(ancestor::*)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

JW

Jul 20 '05 #2
Great, but I have other a problem: if I add this following service, the
xslt doesn't work well
----------------------------------------------------------------------------------------------------------------------
<Service name="service3" depth="2" totalDepth="3">
<Field name="3.1" depth="2">
<Field name="3.1.1" depth="0"/>
<Field name="3.1.2" depth="0"/>
</Field>
<Field name="3.2" depth="2">
<Field name="3.2.1" depth="0"/>
<Field name="3.2.2" depth="0"/>
</Field>
</Service>

My XML document has a dynamic structure with unlimited number of
Service node, Field node and Field subnode.
The xslt document work only on a fix structure with maximum 3 level of
Field node.
XML dcoument has 2 different attribute:
- depth: is the number of child (without subnode) for the selected node
(i.e. "service3" has 2 child: "3.1" and "3.2")
- totalDepth: is the maximum deep of Service node. "service3" as
totalDepth="3": Service node + Field node + Field subnode

I think that these attributes can help you.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
5
by: Miguel J. Jiménez | last post by:
Hi, I need to make a FOR structure using XSL... ie. I want to make a structure for moving between 0 and x, being x a variable name... How can I do this? In PHP/Java would be something like this: ...
5
by: aboycalled3 | last post by:
I'm interested in using the fascinating CSS available at http://www.moronicbajebus.com/playground/cssplay/reformat-table/ which allows one to present tabular data in a way that's more appealing...
6
by: dave | last post by:
I really have 2 questions regarding the following xml snippet. The xml is a directory representation. <?xml version="1.0" standalone="yes"?> <FileSystem> <Row> <ID>1</ID> <Name>Root</Name>...
4
by: n.phelge | last post by:
I need to perform an XSLT to set the namespace on some XML and I need to preserve the original document line formatting to assist with error handling (so the line number from any schema validation...
5
by: Jeremy | last post by:
Does anyone have a clever algorithm for generating an outline of the current document from (client-side) javascript using DOM methods? For example, let's say I predictably have a document...
4
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report>...
6
by: andy | last post by:
say i currently have xml like so... <node> <Date>20080507</Date> <ChildNode /> <ChildNode /> </node> <node> <Date>20080507</Date> <ChildNode />
2
by: AliR \(VC++ MVP\) | last post by:
I'm trying to write a RTF render code in C#. I have the code in C++/MFC and it works fine, but I have run into a problem with the C# code. I think the C# code is from Microsoft, and I added...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.