Jump to content

XPath Filtering Question


John Smart

Recommended Posts

Hi,Apologies if this should go on the XSLT forum - I'm still very much a beginner with XML, XSL etc.I have an XML file with a list of records (trades in this case), and I'd like to be able to add something into the XSL file to only display ineligible trades for instance. I'm trying to add something to the "xsl:for-each" line, but to no success. I've posted my code below, and an example of something from my XML file, I would be ecstatic if someone could help me.I've edited out all the style etc so it's cleaner to read.Thanks,JohnXML file

<trades>  <trade>	<trade_description>Abattoir</trade_description>	<type>Ineligible</type>	<trade_group>Food and Drink</trade_group>	<trade_code>7</trade_code>  </trade>  <trade>	<trade_description>Abrasive Wheel and Powder Manufacturers</trade_description>	<type>Amber</type>	<trade_group>Metalworkers</trade_group>	<trade_code>8</trade_code>  </trade></trades>

XSL File

<table>  <tr>	<th>Description</th> 	<th>Type</th> 	<th>Trade Group</th>   </tr><xsl:for-each select="trades/trade">  <tr>	  <td><xsl:value-of select="trade_description" /></td>	  <td><xsl:value-of select="type" /></td>	  <td><xsl:value-of select="trade_group" /></td>  </tr></xsl:for-each></table>

Link to comment
Share on other sites

Ever heared of predicates?

<xsl:for-each select="trades/trade[type='Ineligible']">

(they are part of the XPath tutorial after all)

Link to comment
Share on other sites

Hi, thanks boen_robot for the reply.I'd tried that, as it's very similar to the example on the tutorial - yet for some reason it's still not working. Taking the tutorial example compared to mine:bookstore = tradesbook = tradeprice= typeSo it should work, since it's a straight swap of elements, but for some reason it won't display any data when I add in the predicate. Removing the [type=ineligible] shows me the full list as expected, so it's working up till that point. I've included my whole XSL file incase there is something within my code elsewhere that's stopping it from working. You'll see my aim is to have 3 buttons to apply different xsl's to my XML data to filter by Eligible, Ineligible, and displaying all trades. Once i've worked this bit out that's the next challenge!Again, any help is very much appreciated.John

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><span style="font-family: verdana; font-size: 8pt; font-weight: bold; color: #000098;">SBC BUSINESS A-Z TRADES</span><br /><br /><input type="button" value="Eligible Only" onclick="location.href='';" style="font-family: verdana; font-size: 11; float:left; margin: 0px 2px 0px 2px" /><input type="button" value="Ineligible Only" onclick="location.href='';" style="font-family: verdana; font-size: 11; float:left; margin: 0px 2px 0px 2px" /><input type="button" value="Display All" onclick="location.href='';" style="font-family: verdana; font-size: 11; float:left; margin: 0px 2px 0px 2px" /><br /><br /><table width="900px" style="border-style:solid; border-width:1; cellpadding:0; cellspacing:0; border-color:#c0c0c0; font-family:verdana; font-size:8pt;">	<tr style="background-color:#D8EDF1; color:#002b7c; border-style:solid; border-width:1; cellpadding:0; cellspacing:0; border-color:#c0c0c0; font-family:verdana; font-size:8pt;">		<th width="450px">Description</th> 		<th width="100px">RAG</th> 		<th width="350px">Trade Group</th> 	</tr>	<xsl:for-each select="trades/trade[type='Ineligible']">	<tr>		<xsl:choose>			<xsl:when test="rag = 'Green'">				<td style="background-color:#CCFFCC; color:#0D880D; font-weight:bold;"><xsl:value-of select="trade_description" /></td>				<td style="background-color:#CCFFCC; color:#0D880D; font-weight:bold;"><xsl:value-of select="rag" /></td>				<td style="background-color:#CCFFCC;"><a href="{url}"><xsl:value-of select="trade_group" /></a></td>			</xsl:when>			<xsl:when test="rag = 'Amber'">				<td style="background-color:#F3D091; color:#D97803; font-weight:bold;"><xsl:value-of select="trade_description" /></td>				<td style="background-color:#F3D091; color:#D97803; font-weight:bold;"><xsl:value-of select="rag" /></td>				<td style="background-color:#F3D091;"><a href="{url}"><xsl:value-of select="trade_group" /></a></td>			</xsl:when>			<xsl:when test="rag = 'Red'">				<td style="background-color:#F8A8A8; color:#E31D06; font-weight:bold;"><xsl:value-of select="trade_description" /></td>				<td style="background-color:#F8A8A8; color:#E31D06; font-weight:bold;"><xsl:value-of select="rag" /></td>				<td style="background-color:#F8A8A8;"><a href="{url}"><xsl:value-of select="trade_group" /></a></td>			</xsl:when>			<xsl:when test="rag = 'Ineligible'">				<td style="background-color:#D3D3D3; color:#66625D; font-weight:bold;"><xsl:value-of select="trade_description" /></td>				<td style="background-color:#D3D3D3; color:#66625D; font-weight:bold;"><xsl:value-of select="rag" /></td>				<td style="background-color:#D3D3D3;"><a href="{url}"><xsl:value-of select="trade_group" /></a></td>			</xsl:when>		</xsl:choose>	</tr>	</xsl:for-each></table></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

[type='ineligible']

is NOT the same as

[type='Ineligible']

XPath, like most XML related languages itself, is case sencetive.Using aalbetski's approach would select the same set of nodes regardless of the template you're in. While this would fix the situation in your case, I'd advise against using it as not only it decreases performance, but it will also make it more difficult for you to learn more about templates when the time comes.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...