473,605 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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">ser vice1</td>
<td colspan="20">se rvice2</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 2193
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:styleshe et 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::S ervice/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(des cendant::Field/Field) &gt; 1
or count(descendan t::Field) &gt; 1">
<xsl:attribut e name="colspan">
<xsl:choose>
<xsl:when test="count(des cendant::Field/Field) &gt; 1">
<xsl:value-of select="count(d escendant::Fiel d/Field)" />
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="count(d escendant::Fiel d)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
</xsl:template>

<xsl:template name="rowspan">
<xsl:if test="not(desce ndant::Field) or (. = //Service[2]/Field[2])">
<xsl:attribut e name="rowspan">
<xsl:choose>
<xsl:when test="not(desce ndant::Field)">
<xsl:value-of select="count(a ncestor-or-self::*)" />
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="count(a ncestor::*)" />
</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
2903
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 this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
5
1799
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: for($i=0;$i<LIMIT;$i++) { // Something using $i }
5
1505
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 to the eye (at least to my eye it's more appealing :^) To my mind this is very much what CSS is all about: allowing content providers to present properly coded data in a way they and, ideally, their readers, find inviting and accessible....
6
1727
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> <Directory>Root</Directory> <Dir>true</Dir>
4
3122
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 error matches the original line number). The original XML looks like this: <Head> <Body Val1="10" Val2="11" /> </Head>
5
1572
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 structured hierarchically with <h1>...<h6tags. I want to generate an outline of the document wherein I have nested lists of the contents of the headers. Take for example the following snippet of a fictional legal document: ------------ <h1>Main...
4
11918
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> </myXml> I there a way that the XSLT transformation can render the content as html rather than text?
6
1354
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
5553
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 scaling to it. The C# code is working fine on my desktop PC but it cuts off some of the text when I run it on my laptop. The MFC code works fine both on the laptop and the PC. I have looked at all the numbers used in the MFC and the C# code and they...
0
8004
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.