jQuery in Action, 2nd edition*
The moose likes XML and Related Technologies and the fly likes Using XSL to display XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Using XSL to display XML" Watch "Using XSL to display XML" New topic
Author

Using XSL to display XML

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
I am very new to xml. I am trying to display xml using xsl but with no success.
Here's my XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<doc>
<title>
<string lang="en">A</string>
<string lang="fr">B</string>
</title>
<body>
<val>
<string lang="en">C</string>
<string lang="fr">D</string>
</val>
<val>
<string lang="en">E</string>
<string lang="fr">F</string>
</val>

</body>
</doc>

and this is my xsl

<?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><xsl:value-of select="doc/title"/></h2>
<table border="1">
<xsl:for-each select="doc/body/val">
<tr>
<td><xsl:value-of select="string"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

But this xsl only displays the first string element value but not the second one. Please help
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi,
Try this...


Spritle Software Blogs
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Or this... (kinda hardcoded)
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
thank u so much
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Angela,
I found (after the inital learning curve) is much more clear cut and easier to maintain when you separate the template elements. Instead of using a loop let the XLST processor figure out how to go. It is more typing in the beginning... AND give you a good headstart in pattern matching. I haven't taken care of the header (your title element) which should be inside the table?
So this is my take:


So have fun!
:-) stw
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using XSL to display XML
 
Similar Threads
use Locale to display information from XML
parameter to xsl from java
Help Required in using If loop in XSL
xmlns and xsl
Hyper links Using XSL?