15-Minute Primer: XPath Filters
Page 1 of 2    
 

The Concept

One of the more sophisticated mechanisms available in XPath is the "filter."  Filters are logical expressions that nodes must satisfy in order to be included in a result set.  A filter is added to an XPath expression by placing it in brackets to the right of a path term.  Filters typically employ logical operators acting upon an element's children.  For instance, the expression

                account-status[past-due > 30]

checks to see whether the element "account-status" has a child "past-due" that is greater than thirty.  So, if we have a document with the nodeset
 
 
 

we can obtain the account number of all account more than thirty days past due with the expression

                account-status[past-due > 30]/account-number/text()

The expression "text()" is used to extract the content of the preceding step in the path—in this case, the "account-number" element.  It's useful whenever one needs to extract the content of an element rather than the element as a whole.  Executing this expression provides the following result:
 
 
 
 
 
 

Note: The sample code in this tutorial makes use of the Tectonic programming language
(featured in the Delve SOA Fast-Prototyping Toolkit)



   

Not sure exactly what the heck XML is? 
Get started with this
excellent wikipedia article:
> Click Here!

Want to learn
the XML basics? 

We recommend
great books like:
> "XML in a Nutshell"

> "XML Crash Course"
Sponsors
Our primary sponsor is Cretaceous Software: 
Check out Cretaceous Software products!
> Click Here

 

 
 

© Copyright 2007 All Rights Reserved. XML How-To (xmlhowto.org)