Jump to content

Help with XSL table


AzaraT

Recommended Posts

Hi there!I was looking trough this tutorial: http://w3schools.com/xsl/xsl_server.asp I wanted to make a table with game release information which is stored in an xml file like this

 <Games>	<isvalid></isvalid>		<fromdate>2008-01-01</fromdate>	<todate>2008-01-31</todate>	<gamecount>54</gamecount>	<Game>		<GameID>6499</GameID>		<Title><![CDATA[PDC World Championship Darts 2008]]></Title>		<Category>Sport</Category>		<URLGameinfo><![CDATA[http://www.tothegame.com/game.asp?id=6499]]></URLGameinfo>		<Platform>			<ID>9</ID>			<Title>Wii</Title>		</Platform>		<ReleaseDates>			<Scandinavia>2008-01-11</Scandinavia>			<UK>2008-01-11</UK>			<US>N/A</US>		</ReleaseDates>		<UKPublisher><![CDATA[Oxygen Interactive]]></UKPublisher>		<Developer><![CDATA[To Be Announced]]></Developer>	</Game>	<Game>		<GameID>6500</GameID>		<Title><![CDATA[PDC World Championship Darts 2008]]></Title>		<Category>Sport</Category>		<URLGameinfo><![CDATA[http://www.tothegame.com/game.asp?id=6500]]></URLGameinfo>		<Platform>			<ID>1</ID>			<Title>PC</Title>		</Platform>		<ReleaseDates>			<Scandinavia>2008-01-11</Scandinavia>			<UK>2008-01-11</UK>			<US>N/A</US>		</ReleaseDates>		<UKPublisher><![CDATA[Oxygen Interactive]]></UKPublisher>		<Developer><![CDATA[To Be Announced]]></Developer>	</Game> ... continues

and the XSL file looks like this, and this is where the problem is

<?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><head><title>Udgivelser i denne måned</title></head><body><table><tr><th>Spil</th><th>Platform</th><th>Categori</th><th>Udgivelses Dato</th></tr><tr>		<xsl:for-each select="Games/Game">		<td><xsl:value-of select="Title" /></td></xsl:for-each>		<xsl:for-each select="Games/Game/Platform">		<td><xsl:value-of select="title" /></td></xsl:for-each>		<xsl:for-each select="Games/Game">		<td><xsl:value-of select="Category" /></td></xsl:for-each>		<xsl:for-each select="Games/Game/ReleaseDates">		<td><xsl:value-of select="Scandinavia" /></td></xsl:for-each></tr></table></body></html></xsl:template></xsl:stylesheet>

So what i want is line for each game with information for, Game title, platform, category and release date for scandinavia, so it should look something like the exemple in the tutorial I was taken ( http://w3schools.com/xsl/cdcatalog.asp ) But what I get out is this: http://zorps.dk/release_list/release_list2.aspSo there's two issues, it doesn't load the platform data, and it doesn't really make a new line for each game.Hope someone can help me out with this oneThanksAzaraT

Link to comment
Share on other sites

What your XSLT does is to first create a table cell for every game, then create more cells for every platform, then more cells for every category, and then more cells for each Scandinavian release date. All on one table row.I suppose you want to create a new row for each game, correct? Then, nest all value-of instructions in a single for-each that would create a table row for every game, like so:

<table><tr><th>Spil</th><th>Platform</th><th>Categori</th><th>Udgivelses Dato</th></tr><xsl:for-each select="Games/Game">		<tr>				<td><xsl:value-of select="Title" /></td>				<td><xsl:value-of select="Platform/Title" /></td>				<td><xsl:value-of select="Category" /></td>				<td><xsl:value-of select="ReleaseDates/Scandinavia" /></td>		</tr></xsl:for-each></table>

Know though, that there's much more that could be improved on this XSLT to make it more flexible to the XML being given. I'll leave it simple though, so that you could learn out from this example.

Link to comment
Share on other sites

Yes, and quite easily. You just need to write a class attribute on every second element, and then style all TDs that have that class. XSLT itself can detect the position of an element with the XPath position() function.I'll let you try to write the code yourself as an excersice (believe me, the satisfaction of seeing the end result will be better :) ), and only give you links to the things you need to know to make it happen, mainly the xsl:if element, How to create attributes in XSLT, and the XPath position() function.And here's a hint on using the XPath position() function:

position() mod 2 = 0

If even after those hints you hit a rock, post again, but please, try it first.

Link to comment
Share on other sites

hmm I don't really understand where / how to use XPath ?
The value of every "select" attribute in XSLT is an XPath expression, and so is "match" actually (though it defers from "select" a little, but in general, you won't be concerned with that difference).If you don't know the syntax of XPath, you should have tried to study it before you even began XSLT. W3Schools has a great XPath tutorial that can teach you XPath.Once you learn XPath, you should be able to completely understand what I mean by saying: (almost) everywhere in an XPath statement where you can specify a node, you can also use a function. The case with the position() function is not an exclusion, so, following the examples of W3Schools:
/bookstore/book[2]/position()

should return "2".I won't spoil you the fun next time by giving you the code :) .

Link to comment
Share on other sites

Hmm I never really figured this one out, but I didn't gave it time since I thought of another way to make it look better, and most likely with simpler code. However, I sttill cant get it working.What I want here, is to add a color for each of the differnt game consoles there is, so I made this code:

<table padding="0"><tr margin="0" padding="0"><th align="left">Spil</th><th align="left">Platform</th><th align="left">Categori</th><th align="left">Udgivelses Dato</th></tr><xsl:for-each select="Games/Game">				<xsl:choose>					<xsl:when test="Platform/Title > Wii">				  <tr margin="0" padding="0" bgcolor="#ff00ff">											   						<td><xsl:value-of select="Title" /></td>						<td><xsl:value-of select="Platform/Title" /></td>						<td><xsl:value-of select="Category" /></td>						<td><xsl:value-of select="ReleaseDates/Scandinavia" /></td>		</tr>		</xsl:when>		<xsl:otherwise>				<tr margin="0" padding="0">											   						<td><xsl:value-of select="Title" /></td>						<td><xsl:value-of select="Platform/Title" /></td>						<td><xsl:value-of select="Category" /></td>						<td><xsl:value-of select="ReleaseDates/Scandinavia" /></td>		</tr>		</xsl:otherwise>		</xsl:choose></xsl:for-each></table>

But I'm afraid it never decets the "Wii" and uses the other option again, any clue why this might be ? (its still the same xml file)Hope you can help me out :)

Link to comment
Share on other sites

I was hoping you'd read the signs.... sighs...

<table><tr><th>Spil</th><th>Platform</th><th>Categori</th><th>Udgivelses Dato</th></tr><xsl:for-each select="Games/Game">		<tr>				<xsl:if test="position() mod 2 = 0">						<xsl:attribute name="style">background-color:#ff00ff;</xsl:attribute>				</xsl:if>				<td><xsl:value-of select="Title" /></td>				<td><xsl:value-of select="Platform/Title" /></td>				<td><xsl:value-of select="Category" /></td>				<td><xsl:value-of select="ReleaseDates/Scandinavia" /></td>		</tr></xsl:for-each></table>

the xsl:if element tests if the position of the current "Game", divided by "2" gives a remainder of "0" (i.e. has no remainder). So, the test will be "true" if the position is "2", "4", "6", etc.... every second element. When the test is true, a new attribute is placed on the row - the "style" attribute with a value that creates the background color. You could (and should) use a class instead of embedding the CSS information though. That's just for the example.

Link to comment
Share on other sites

Thanks, however, I have found out that I rather make the background color depend on the "Platform/Title" so if the Platform/title = Wii I want the bg to be one color, if its PS3, another color and so on. So I created that code above, but it didn't work. I have also tried this, but that gave no resault either.

<xsl:for-each select="Games/Game">				<tr margin="0" padding="0">					   <xsl:if test="Platform/Title = Wii">						<xsl:attribute name="style">background-color:#ff00ff;</xsl:attribute>				</xsl:if>							   						<td><xsl:value-of select="Title" /></td>						<td><xsl:value-of select="Platform/Title" /></td>						<td><xsl:value-of select="Category" /></td>						<td><xsl:value-of select="ReleaseDates/Scandinavia" /></td>		</tr></xsl:for-each>

Link to comment
Share on other sites

Do not confuse a node-set with a string.A node-set is a node in XML which XPath may target. They are written directly.A string is, to put it simple - plain text.In order to differenciate between strings and node-sets, place apostrophes (') around strings like so:

				<xsl:if test="Platform/Title = 'Wii'">						<xsl:attribute name="style">background-color:#ff00ff;</xsl:attribute>				</xsl:if>

For your case however, I'd suggest using <xsl:choose> (as you initially intended to) for better performance's sake like so:

				<xsl:choose>								<xsl:when test="Platform/Title = 'Wii'">										<xsl:attribute name="style">background-color:#ff00ff;</xsl:attribute>								</xsl:when>								<xsl:when test="Platform/Title = 'PS3'">										<xsl:attribute name="style">background-color:#00ffff;</xsl:attribute>								</xsl:when>				</xsl:choose>

In a similar fashion, if you plan to always color the row somehow, place the <xsl:attribute/> element outside of the <xsl:choose/>.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...