Jump to content

Dynamically filtering a table using XSLT


Guest irwincoelho

Recommended Posts

Guest irwincoelho
Posted

I am fairly new to XSLT. I have created an XML table part of which is as follows:<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="songlist.xsl"?><songlist><song><artist>Jimmy Barnes</artist><duration>4:07</duration><genre>Jive</genre><title>KHE SAN</title></song><song><artist>Steppenwolf</artist><duration>3:33</duration><genre>Fast</genre><title>BornToBeWild</title></song><song><artist>Rob Thomas</artist><duration>3:53</duration><genre>Fast</genre><title>This is how a heart breaks</title></song><song><artist>DJ Otzi</artist><duration>3:39</duration><genre>Fast</genre><title>Hey Baby</title></song><song><artist>Chris Rea</artist><duration>4:37</duration><genre>Jive</genre><title>Lets dance</title></song></songlist>The xslt file is as follows:<?xml version="1.0" encoding="UTF-8"?><html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"><head><title>Irvy's Song List</title><link rel="stylesheet" type="text/css" href="irvy.css"/></head><body><table><tr><th>Title</th><th>Artist</th><th>Category</th><th>Duration</th></tr><xsl:for-each select="songlist/song"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="genre"/></td><td><xsl:value-of select="duration"/></td></tr></xsl:for-each></table></body></html>Can I use XSLT to filter the table using the headers?For example, I wish to look at only songs that have the genre="Jive".I do not want to create an xslt file every time I wish to do this and I want to do it through the browser possibly by clicking a button or some such event driven method.Thank you very much in advance.IrvyReply With Quote

Posted

Your stylesheet needs a global parameter then

<xsl:param name="genre"/>...  <xsl:for-each select="songlist/song[genre = $genre]">

and you would need to script the transformation with Javascript, with Mozilla and Opera, Safari, Chrome the API is described here: https://developer.mozilla.org/en/using_the_...transformations, with IE/MSXML find an example here:

var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");var xslproc;xsldoc.async = false;xsldoc.load("sheet.xsl");   xslt.stylesheet = xsldoc;   var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");   xmldoc.async = false;   xmldoc.load("input.xml");	  xslproc = xslt.createProcessor();	  xslproc.input = xmldoc;	  xslproc.addParameter("genre", "Jive");	  xslproc.transform();	  outputDiv.innerHTML = xslproc.output;

You need to use the normal XSLT stylesheet syntax with an xsl: stylesheet or transform root element, not the compact form you have choosen.

Archived

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

×
×
  • Create New...