Generating html out of xml using xslt

B

Brent

Hi All,

I'm trying to transform an xml document into an html doc, with an xsl
doc. It's a simple document, shown below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>


The xsl stylesheet looks like this:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>



I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
them produce the right html output. The output looks like this:


<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
</table>
</body>
</html>


I've tried it without the for-each loop, but it doesn't seem to be
grabbing the data.
Is there something I'm missing?


Thanks for any hints!

Brent
 
G

grouch

Brent said:
I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
them produce the right html output. The output looks like this:


<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
</table>
</body>
</html>


Hmm,

I've tried it using XmlStarlet (http://xmlstar.sourceforge.net/)
with your stylesheet and XML and it worked fine.

$ xml tr -E catalog.xml
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
</table>
</body>
</html>

Since you are referencing to your stylesheet from XML,
as a test you could just open your XML in a browser and see.

--MG
 
M

Malte

Brent said:
I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
them produce the right html output. The output looks like this:

I ran it though oxygenXml and it looks fine. Oxygen uses Xalan, I believe.
 
P

Peter Flynn

Brent said:
Hi All,

I'm trying to transform an xml document into an html doc, with an xsl
doc. It's a simple document, shown below: [snip]
The xsl stylesheet looks like this: [snip]
I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
them produce the right html output. The output looks like this: [snip]
I've tried it without the for-each loop, but it doesn't seem to be
grabbing the data.
Is there something I'm missing?

It works fine using Saxon b8-0 here.

<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
</table>
</body>
</html>

However...you may find it easier to use templates rather than for-each:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="cd">
<tr>
<td>
<xsl:apply-templates select="title"/>
</td>
<td>
<xsl:apply-templates select="artist"/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top