Jump to content

xslt value-of select stops at first value


tnapolitano

Recommended Posts

XSLT noob here, and I would appreciate any help, hints or suggestions.I have a XML structured like this:<doc> <list> <host>hostname1</host> <ip>192.168.1.100</ip> <os>windows xp</os> <host>hostname2</host> <ip>192.168.1.101</ip> <os>mac os x</os> </list></doc>I have a XSL structured like this:<xsl:template match="/" > <html> <body> <h2>System Summary</h2> <table border="1"> <tr> <th align="left">Host</th> <th align="left">IP Address</th> <th align="left">Operating System</th> </tr> <xsl:for-each select="/Doc" > <tr> <td><xsl:value-of select="list/hostname" /></td> <td><xsl:value-of select="list/ip" /></td> <td><xsl:value-of select="list/os" /></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template>My output stops at the first value per element (i.e., "hostname1, 192.168.1.100, Windows XP"). What do I do to make the XSLT continue through all elements of the XML? Thanks in advance.

Link to comment
Share on other sites

If you look closely at your XPath expressions, you'll see the error. Doc is the root element of the document, so It's pretty obvious why this cycle repeats only once. I don't see much point of the list element though.If you are the creator of this XML (it's not generated by some application) I would suggest that you group your things to make them more easily accesable to the XSLT. Example:

<list><host><name>hostname1</name><ip>192.168.1.100</ip><os>windows xp</os></host><host><name>hostname2</name><ip>192.168.1.101</ip><os>mac os x</os></host></list>

With an XML like that, it's quite easier to set the XSLT to see what information belongs to which host so it could display each on a new row.Anyway, if this XML is generated by some application, I suppose I'll try to think of something else, but it would be quite messy, I know it.

Link to comment
Share on other sites

If you look closely at your XPath expressions, you'll see the error. Doc is the root element of the document, so It's pretty obvious why this cycle repeats only once. I don't see much point of the list element though.If you are the creator of this XML (it's not generated by some application) I would suggest that you group your things to make them more easily accesable to the XSLT. Example:
<list><host><name>hostname1</name><ip>192.168.1.100</ip><os>windows xp</os></host><host><name>hostname2</name><ip>192.168.1.101</ip><os>mac os x</os></host></list>

With an XML like that, it's quite easier to set the XSLT to see what information belongs to which host so it could display each on a new row.Anyway, if this XML is generated by some application, I suppose I'll try to think of something else, but it would be quite messy, I know it.

Link to comment
Share on other sites

  • 1 month later...

Sorry that I haven't answered earlier, but I think I have the solution now. Scince recetly there were solutions for showing an XML linearly in table discovered, I think that's what could help you here. Here's the XSLT adopted for your XML:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xsl:stylesheet [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/><xsl:param name="group-size" select="3" /><xsl:template match="/doc"><html xmlns="http://www.w3.org/1999/xhtml">	<head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  <title>Untitled Document</title>  <style type="text/css">  	table * {border: 1px solid #000000;}  </style>  </head>  <body>  	<table>  	<thead>    <th>Host</th>    <th>IP</th>    <th>OS</th>  	</thead>  	<tbody>    <xsl:apply-templates select="list/*[(position() mod $group-size) = 1]" />  	</tbody>  	</table>  </body>	</html></xsl:template><xsl:template match="list/*">	<tr>  <xsl:for-each select=". | following-sibling::*[position() < $group-size]">  	<td>    <xsl:value-of select="." />  	</td>  </xsl:for-each>	</tr></xsl:template></xsl:stylesheet>

I tryed using distinc-values() function so I could also make it that new elements will automatically be added in a new column, but it didn't worked, because distinc-values seems to be XPath 2.0 function. Anyway, if a new element arises, you can simply edit the group-size parameter and that would be it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...