I am creating an application that creates an XML as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="MyCDCatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<price>8.50</price>
<year>1990</year>
</cd>
</catalog>
I also a XSL file called MyCDCatalog.xsl (below) that transforms the above XML file.
<?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">
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="artist" />'s <xsl:value-of select="title" /> (<xsl:value-of select="year" />): $<xsl:value-of select="price" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Currently, the XSL file returns all the entries in a single column. How do I get my output in 3 or more columns?
I want the output as follows:
[cd1] [cd2] [cd3]
[cd4] [cd5] [cd6]....
where
cd1 = Bob Dylan's Empire Burlesque (1985): $10.90
cd2 = Bonnie Tyler's Hide your heart (1988): $9.90
etc.
Thanks for the help.
Another way to do this is to match the cd in the first, fourth, seventh (etc) position.
Where $columns is a parameter containing the number of columns
You can then match the cd elements in the row by looking at the relevant number of following siblings:
Here is the full XSLT
When applied to your sample XML, the following is output:
(I’m only outputing name and artist here, but I am sure you can see how to change it to show more information)
Note that there is bit of nasty recursive template to output empty cells should the number of remaining cd elements be less than the number of columns in the row.
Change the paramater to 4, for example, then the following is output: