15-Minute Primer: XPath Filters
Page 2 of 2    
 

Applying Boolean and Arithmetic Operators

XPath includes the usual Boolean operators, in the form of the keywords "or," "and," and "not."  Using our "account-status" nodeset, the following expression could be used to obtain all past-due account status elements with an account number over 25000.
                 
                account-status[past-due > 30 and account-number > 25000]

Parenthetic groupings can be combined with logical and arithmetic operators to create more sophisticated expressions.  For instance, suppose we have a document "boxes," show below
 
 
 

and want to extract those members with a volume greater than 2000.  This can be accomplished using the expression

                boxes/box[(length * width * height) > 2000]

Embedding XPath expressions within a filter is allowed; making it possible to filter elements based on any of their descendents, not just their immediate children.  Let's enhance our "box" elements to include a "material" child representing the stuff a box is actually made of.
 
 
 
 

The following expression can then be used to extract all elements representing boxes with a volume greater than 1500 and a thickness less than 10.

  boxes/box[(length * width * height) > 1500 and material/thickness < 10]

The result of evaluating the expression consists of the element boldfaced above.

XPath expressions embedded in filters may themselves includes XPath expressions.  Let's say our theoretical set of boxes is allowed to have members for which no material name is specified—that is, they may be made of cardboard, or wood, or whatever—it's unspecified.

 
 
 
 

If we want to extract only those boxes over a certain thickness for which a material name is specified, the easiest way to do it is with a nested filter (highlighted below).

       boxes/box[(length * width * height) > 700 and material[mat-name]/thickness < 30]

The two elements matching these criteria are boldfaced in the same nodeset shown above.  Note that the last box in the set would have qualified if a material name had been present.
 
 
 

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)