• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Using XSL to display XML

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try this...
 
Balaji Loganathan
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or this... (kinda hardcoded)
 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u so much
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'm still in control here. LOOK at this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic