473,395 Members | 1,941 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,395 software developers and data experts.

xml/xslt help

Bob
I am having problems turning my xml into html for display. A sample
xml file is given below, followed by my pathetic xslt attempt to
transform it into html. A textual representation of what I was hoping
to see is last. I am using saxon to do the transformation.

<?xml version="1.0"?>
<Whatever>
<Company>Company One
<DateRange>21-27Mar2004</DateRange>
<SubOrg>Division A
<Data>163
<Description>A description of 163, Div A, Comp A</Description>
</Data>
<Data>201
<Description>Description of 201, Div. A, Comp A</Description>
</Data>
</SubOrg>
<SubOrg>Division B
<Data>213
<Description>More descriptive text.</Description>
</Data>
<Data>222
<Description>Again, more stuff.</Description>
</Data>
</SubOrg>
</Company>
</Whatever>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head><title>Title Information</title></head>
<body bgcolor="white">
<p align="center"><font size="+3">Report</font></p>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Company">
<br/>
<p><font size="+2" color="green"><xsl:value-of
select="."/></font></p>
<xsl:apply-templates select="DateRange"/>
<xsl:apply-templates select="SubOrg"/>
<xsl:apply-templates select="Data"/>
</xsl:template>
<xsl:template match="DateRange">
<p><font color="blue"><xsl:value-of select="."/></font></p>
</xsl:template>
<xsl:template match="SubOrg">
<br/><font size="+1" color="red"><xsl:value-of
select="."/></font>
</xsl:template>
<xsl:template match="Data">
<br/><font color="black"><xsl:value-of select="."/></font>
</xsl:template>
</xsl:stylesheet>
What I would like to see is some html that would appear somewhat like
this:
Report
Company One
21-27Mar2004
Division A
163 - A description of 163, Div A, Comp A
201 - Description of 201, Div. A, Comp A
Division B
213 -
222 -
Company Two
07-14Apr2004
Division A
128 -
Division B
221 -
234 -

etc...

I am using saxon to turn the xml/xslt into html. It's probably
obvious that I'm not getting the html I wanted.

Can any provide some help? Thanks in advance!

Bob.
Jul 20 '05 #1
2 1724
gr**********@yahoo.com (Bob) writes:
I am having problems turning my xml into html for display. A sample
xml file is given below, followed by my pathetic xslt attempt to
transform it into html. A textual representation of what I was hoping
to see is last. I am using saxon to do the transformation.

<?xml version="1.0"?>
<Whatever>
<Company>Company One
<DateRange>21-27Mar2004</DateRange>
<SubOrg>Division A
<Data>163
<Description>A description of 163, Div A, Comp A</Description>
</Data>
<Data>201
<Description>Description of 201, Div. A, Comp A</Description>
</Data>
</SubOrg>
<SubOrg>Division B
<Data>213
<Description>More descriptive text.</Description>
</Data>
<Data>222
<Description>Again, more stuff.</Description>
</Data>
</SubOrg>
</Company>
</Whatever>


You've got the right idea, but a couple of comments:

- the difficulty with your XML is that not every item is wrapped up
individually as an element. So in

<Data>163
<Description>A description of 163, Div A, Comp A</Description>
</Data>

the Data element contains two nodes: a text node and a Description
node. Your <xsl:value-of select="."/> extracts the text of both.
Extracting just the text node is ugly: you can use text(), but you
get all the newlines and junk, so I used normalize-space(text()).

It would be much neater and flexible if you could rewrite this as

<Data>
<Title>163</Title>
<Description>A description of 163, Div A, Comp A</Description>
</Data>

for example.

- You should read up on CSS. <p align="center"><font size="+3">
Report</font></p> is truly horrible. The modern way is to mark it
up as <h1> or whatever and use CSS to style it.
This transformation with your data

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
<html>
<head><title>Title Information</title></head>
<body>
<h1>Report</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="Company">
<h2><xsl:value-of select="normalize-space(text())"/></h2>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="DateRange">
<h3><xsl:value-of select="normalize-space(text())"/></h3>
</xsl:template>

<xsl:template match="SubOrg">
<h4><xsl:value-of select="normalize-space(text())"/></h4>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="Data">
<p>
<xsl:value-of select="normalize-space(text())"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="Description"/>
</p>
</xsl:template>

<!-- Omit text not explicitly dealt with above -->
<xsl:template match="text()"/>

</xsl:stylesheet>
gives
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title Information</title>
</head>
<body>
<h1>Report</h1>
<h2>Company One</h2>
<h3>21-27Mar2004</h3>
<h4>Division A</h4>
<p>163 - A description of 163, Div A, Comp A</p>
<p>201 - Description of 201, Div. A, Comp A</p>
<h4>Division B</h4>
<p>213 - More descriptive text.</p>
<p>222 - Again, more stuff.</p>
</body>
</html>
Which is approximately what you want.

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #2
Bob
Ben,

Thanks for the help - it is greatly appreciated. I'm going to give it
a try this morning.

Bob.
Jul 20 '05 #3

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

Similar topics

6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
1
by: Victor | last post by:
Hi, this might sound silly but I cannot figure out how to incorporate some XML of employees into some XML of a company to give new XML containing them all. For example, this is the company now ...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
15
by: Jeff Uchtman | last post by:
Can I draw from 2 XML sources, the structure is exactly the same execpt for data contained into 1 xslt using math to add some structrure, and displaying others as node 1 and node 2? This data is...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.