How to utilize XSLT and XPath to filter XML data and render as XHTML

After learning the basics of XSLT one often wonders how to filter the XML data to returns data that contains a search string or has a value greater then or less than another value. To do this in XSL Stylesheets you can use take advantage of XPath in your xsl:foreach elements. Lets assume we have the following xml data as I used in my previous XSLT sorting article modified so that the year and publisher values are now attributes and not sub elements.




     
	Patrick Rothfuss
	The Name of the Wind
   
     
	Trudi Canavan
	The Magicain's Guild
   
     
	Trudi Canavan
	The Magicain's Apprentice
   
     
	Jim Butcher
	First Lord's Fury
   
     
	Jim Butcher
	Changes
   
     
	Brandon Sanderson
	Warbreaker
   

Next we’ll create a basic XSL Stylesheet that will simply display our data in a table.






My Books



    
    
    
Title Author Publisher Year

This stylesheet will give us the following output:

Title Author Publisher Year
Warbreaker Brandon Sanderson TOR 2009
Changes Jim Butcher ROC 2010
First Lord’s Fury Jim Butcher ACE 2009
The Name of the Wind Patrick Rothfuss DAW 2007
The Magicain’s Apprentice Trudi Canavan Orbit 2009
The Magicain’s Guild Trudi Canavan EOS 2004

Now for applying our filters, you will notice that in the xsl:for-each we use the select attribute to define the XML nodes to iterate over. This value books/book is an XPath value saying all elements of path books > book. To filter we simply modify the XPath. For example lets say we want all books published after 2008. To do this we would use the following XPath books/book[(number(@year) > 2008)]. This string says select all book elements that have an attribute of year greater then 2008. The xsl:for-each statement would look like the following.


    
    
    	  
       
       
       
       
    

This would render the following output:

Title Author Publisher Year
Warbreaker Brandon Sanderson TOR 2009
Changes Jim Butcher ROC 2010
First Lord’s Fury Jim Butcher ACE 2009
The Magicain’s Apprentice Trudi Canavan Orbit 2009

If we wanted our filter to be even more complicated we could use multiple attributes or child elements in are select statement for example we could only want books published after 2008 and only display authors that contain the letter i. Doing this we would make use of the XSL function contains to compare strings as well as the and keyword to join are statements. The finished XPath would be books/book[(number(@year) > 2007) and contains(author, ‘i’)] with the output being the following XHTML.

Title Author Publisher Year
Changes Jim Butcher ROC 2010
First Lord’s Fury Jim Butcher ACE 2009
The Magicain’s Apprentice Trudi Canavan Orbit 2009

That is pretty much all there is to it. There are many functions available to utilize in your XPath query strings and can be referenced here. For further details also visit the w3schools website’s XPath tutorial

// HTML/CSS // XML // XPath // XSLT //

Comments & Questions

Add Your Comment