Iv’e got an xml file containing data about games, which are an .exe download; the game name; the image location and the alternate text to use. Which is sorted into catagorys. (See Below)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="games.xsl"?>
<all>
<general>
<catagory_name>
<catg_1>Action</catg_1>
<catg_2>Other</catg_2>
</catagory_name>
</general>
<games>
<catagory name='Action'>
<game>
<name>1945<!--name--></name>
<url><!--URL to game download--></url>
<image><!--image location--></image>
<alt>1945 Icon<!--alt text for image--></alt>
</game>
</catagory>
<catagory name='Other'>
<game>
<name>Platform Game<!--name--></name>
<url><!--URL to game download--></url>
<image><!--image location--></image>
<alt>Platform Game Icon<!--alt text for image--></alt>
</game>
</catagory>
</games>
</all>
I’ve then got this xslt file to convert the xml to xhtml…
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/all">
<html>
<body>
<h1 style="text-align:center;">Games</h1>
<center>
<table>
<xsl:if test="//general/catagory_name/catg_1">
<xsl:variable name="catg">
<xsl:value-of select="//general/catagory_name/catg_1"/>
</xsl:variable>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = (1)]/url">
<tr>
<td colspan="4" rowspan="1">
<h2>
<xsl:copy-of select="$catg"/>
</h2>
</td>
</tr>
<xsl:for-each select="//games/catagory[@name=$catg]/game[position() mod 4 = 1]">
<xsl:variable name="pos" select="position() * 4"/>
<tr>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
</tr>
<tr>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -3)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -2)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -1)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos)]/name"/>
</p>
</a>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</xsl:if>
</xsl:if>
<xsl:if test="//general/catagory_name/catg_2">
<xsl:variable name="catg2">
<xsl:value-of select="//general/catagory_name/catg_2"/>
</xsl:variable>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = (1)]/url">
<tr>
<td colspan="4" rowspan="1">
<h2>
<xsl:copy-of select="$catg2"/>
</h2>
</td>
</tr>
<xsl:for-each select="//games/catagory[@name=$catg2]/game[position() mod 4 = 1]">
<xsl:variable name="pos" select="position() * 4"/>
<tr>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
</tr>
<tr>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -3)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -2)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -1)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos)]/name"/>
</p>
</a>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</xsl:if>
</xsl:if>
</table>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This code works absolutely fine however it is already quite complicated. First it checks to see if a particular category exists then if that category has any games in it. Then it displays it but only if the data exists. So you’ll be pleased that that’s all fine. My problem is that every time i want to add a new category i have to go into the xslt file and add the code below with the variable $catg being called $catg1, $catg2 and so on for all the catagorys i need because you can’t change the value of an existing variable.
<xsl:if test="//general/catagory_name/catg_2">
<xsl:variable name="catg2">
<xsl:value-of select="//general/catagory_name/catg_2"/>
</xsl:variable>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = (1)]/url">
<tr>
<td colspan="4" rowspan="1">
<h2>
<xsl:copy-of select="$catg2"/>
</h2>
</td>
</tr>
<xsl:for-each select="//games/catagory[@name=$catg2]/game[position() mod 4 = 1]">
<xsl:variable name="pos" select="position() * 4"/>
<tr>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/image)"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/alt)"/>
</xsl:attribute>
</img>
</a>
</td>
</xsl:if>
</tr>
<tr>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -3)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -2)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -1)]/name"/>
</p>
</a>
</td>
</xsl:if>
<xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
</xsl:attribute>
<p>
<xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos)]/name"/>
</p>
</a>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</xsl:if>
</xsl:if>
What i want to do is to be able to use a for each so only have to have one of the code (above) however complicated that never needs altering for a new catagory. So my question is how would i do this? Ive tried with variables ect but ou’d still need to edit the code each time you wanted a new catagory because you can’t change the value of an existing variable.
The only solution i can think of is to replace the $catg with the node value it represents ie general/catagory_name/catg and loop it every time but i haven’t had any success.
Use OL for numbering them from 1.
Select each category, irrespectively of duplicated values.
Find out if this is the first occurrence of category, by id of the first game, and id of first-of-all game within the same category.
If this is the first occurrence of category, just apply-templates for each game with ancestor as the given category. Use li for each category, and use classes and CSS to achieve table-like arrangement.
Sorts are added to achieve sorted output…