Xml Tutorials

XPath – Predicates

We have learned how to select elements and attributes in an XML document, but we haven’t learned how to be eliminate unwanted items. This lesson will teach you how to impose restrictions in your XPath expressions using predicates.

We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:



$2.50 20

$1.50 10




$4.50 60
180





XPath – Being Choosy with Predicates
Imagine that we wanted to select all the products from lemonade2.xml that had an amount greater than 15. With the current knowledge you have, this would be impossible and would require programming outside of XPath expression to solve this problem.

The closest we could get would be to select all the products:

XPath Expression:
inventory/*/*

However, with the use of XPath predicates, the problem of selecting only those with an amount greater than 15 is easy to conquer.

A predicate is similar to an If/Then statement. If our predicate is TRUE, then the element will be selected. If the predicate is FALSE, it will be excluded. What would be our predicate for this example?

Well, we want to select those amount elements that are greater than 15, so in English, our predicate would be:

predicate: greater than 15
Now, we just need to know how to convert this English into an XPath predicate!

XPath – Predicates are in Brackets [ & ]
An XPath predicate is contained within square brackets [], and comes after the parent element of what will be tested!

Because we want to test each product’s amount element, we might create a separate predicate for each product: lemonade, pop and chips.

To start off simply, let’s just do lemonade first using an absolute path.

Predicate XPath Expression:
inventory/drink/lemonade[amount>15]

Notice that the form of a predicate is:

parent[child someTestHere]
If we wanted to do this predicate on all the products we would use the wildcard feature to select inventories children and then select drink and snack children.

Predicate XPath Expression:
inventory/*/*[amount>15]

Question: Which products will be selected?

Answer: lemonade and chips are selected because they both have amount elements greater than 15.

XPath – Predicates with Attributes
Besides testing the values of elements, you can also use predicates to check the values of attributes. The form pretty much the same as before, except the attribute belongs to the element before the predicate.

element[@element'sAttribute someTestHere]
Our XML document contains an attribute supplier which describes where the item originated from. With a predicate, we could find all the products that were purchased from the store.

Predicate XPath Expression:
inventory/*/*[@supplier='store']

This XPath expression would select the elements pop and chips since they were from the store.

XPath – Predicate Current Element .
Sometimes you don’t want to select the child element or attribute, but rather, the current element. XPath lets you refer to the current element with the period “.”, when placed within the predicate.

If we wanted to select the amount of pop where the amount was less than 20, we would need to use a period.

Predicate XPath Expression:
inventory/drink/pop/amount[. < 20]

Whenever you need to select an element and use a predicate on that same element you will need to use a period “.” to access that element’s value.


XPath – Vertical Bar | (Pipe)

XPath makes use of the character “|”, which we will be referring to as pipe from here on out. The pipe character is a way of combining two or more expressions into one. Depending on where you are using XPath, this ability to combine multiple expressions into one may be useful.

This lesson will teach you how to use XPath’s combine feature, “|”.

We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:



$2.50 20

$1.50 10




$4.50 60
180





XPath – Combining Two Expressions with |
When you need to select multiple things with one expression, chances are you will have to use the pipe character. In our lemonade2.xml document, we might want to select the children of chips and the children of pop. To do this, we would use the pipe character “|”.

Combining XPath Expression:
inventory/snack/chips/* | inventory/drink/pop/*

This expression combines two expressions into one and will select every element that matches either expression.

XPath – Combining Multiple Expressions with |
Although you probably won’t use this feature very often, it is possible to combine as many expressions as you want with the pipe character. Here is an example that selects all of the children elements of chips, using |.

Combining XPath Expression:
chips/price | chips/amount | chips/calories


  • Copyright © 1996-2010 Programming tutorials for beginners,. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress