View Full Version : Newbie xsl question
guttyguppy
09-14-2005, 11:39 PM
I did the w3schools.com tutorial for xslt, and have a question.
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
gets parsed nicely by
<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>
But what if you had more than one catalogue? Like <catalog1>, <catalogue2> etc.
How would you tell xslt to write out each catalogue seperately?
Thanks for any advice.
CrzySdrs
09-15-2005, 01:52 AM
Well, you can't actually have multiple catalog elements without making a new toplevel element. You can only have one toplevel element, which like I said in this case is catalog. What I think you are talking about is perhaps making multiple CDs... which if you wanted to do you wouldn't give them all different names (i.e. Cd1, cd2, cd3). They would all be called CD, like below.
XML Doc Example:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Other</title>
<artist>Bob Other</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Empire Other</title>
<artist>Bob Other</artist>
<country>Other</country>
<company>Other</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
Then your XSL that you currently have would list all of the CD's accordingly. If you wanted to reference each one individually you would need to have some sort of primary key associated with each one like <id>1</id>.
Then you could do a quick test and display whichever particular one you wanted to display.
<xsl:for-each select="catalog/cd">
<xsl:if test="id = 1">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
Which would only display the output where the id is 1. XSL isn't so much a language to pick and choose but more of a language to display the data in a more readable format than XML. If you wanted to do more in depth things like data manipulation you would need to use a server side language or other programming language to extract the information and play around with it.
guttyguppy
09-15-2005, 02:02 PM
Thanks,
So what if I wrap all the <catalogue> tags in <cd_collection> tags? Would there be any way for xsl to display each catalogue, followed by all the info within each catalogue?
KC-Luck
09-15-2005, 03:40 PM
yes,
<catalogs>
<catalog name="">
<blah..../>
</catalog>
<catalog name="">
<blah..../>
</catalog>
</catalogs>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/catalogs">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="catalog">
<table>
<caption><xsl:value-of select="@name"/></caption>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="country"/></td>
<td><xsl:value-of select="company"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.