Page 1 of 1

Using XSL to filter an XML document

Posted: Mon May 21, 2007 1:28 pm
by John Smart
Hi chaps.

I have a question regarding XSL. Basically, I have an xml file containing records of occupations (occupation name, category, and links to further information).

I am using html within an xsl file to display the list, and it all works fine.

There are 800 occupations though, so I want to be able to filter them alphabetically, by using input buttons to call in other xsl files, which filter the information (select="occupations/occupation[alpha$eq$'A'] etc). I've left in my onclick as it was (getA.xsl is the xsl file to display everything with A), as whilst this obviously doesn't work now, shows some of my method

I can't work out how to reference the other xsl files from my 'main' xsl file. We did have it working previously using the DATASRC and DATAFLD tags and using some javascript, but that's IE specific I believe, and also had trouble getting links to work correctly that way.

Any help would be most appreciated - I'm sure this is fairly straightforward but have been trauling the internet looking for help! I've attached a sample from my xml file (condensed to 1 record for ease), and my xsl file.

Thanks,

John

XML:

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="smallbustrucksoccupations.xsl"?>
<occupations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SmallBusTrucksOccupations.xsd">

<occupation>
<description>Agricultural Produce Distribution</description>
<term>Unacceptable</term>
<rationale>Distribution</rationale>
<url>/prod/kite/TechContainer.nsf/FreePagesByUNID/7AAACF15BD75D14B802572DD00293416?OpenDocument</url>
</occupation>
</occupations>

XSL:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>

<body>
<span style="font-family: verdana; font-size: 8pt; font-weight: bold; color: #000098;">OCCUPATIONS LISTINGS</span>

<br /><br />

<input type="button" style="font-family: verdana; font-size: 11" value="A" onclick="ApplyStyleSheet(getA.xsl);" />

<br /><br />
<table 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>Description</th>
<th>Term</th>
<th>Rationale</th>
</tr>
<xsl:for-each select="occupations/occupation">
<tr>
<td><xsl:value-of select="description" /></td>
<xsl:choose>
<xsl:when test="term = 'Acceptable'">
<td style="background-color:#128612; color:#ffffff;"><xsl:value-of select="term" /></td>
</xsl:when>
<xsl:when test="term = 'Refer'">
<td style="background-color:#D99918; color:#ffffff;"><xsl:value-of select="term" /></td>
</xsl:when>
<xsl:when test="term = 'Unacceptable'">
<td style="background-color:#E83F3F; color:#ffffff;"><xsl:value-of select="term" /></td>
</xsl:when>
</xsl:choose>
<td><a href="{url}"><xsl:value-of select="rationale" /></a></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Posted: Tue May 22, 2007 11:27 pm
by jkmyoung
We'd need to see the rest of the javascript.
Generally you'd pass in a parameter into the xslt, set up like:
<xsl:stylesheet .... >
<xsl:param name="start" select=""/>

and then change your for-each loop to
<xsl:for-each select="occupations/occupation[starts-with(description,$start)]">
<xsl:sort select="description"/>