Predicates


As noted earlier, a predicate restricts the context nodes based on some criterion, as specified in a Boolean expression. A later section suggests some of the flexibility that is available to you. XPath 1.0 will convert any expression to a Boolean expression, but that behavior may or may not yield the result you intend. We'll give you a hint of the issues now.

A predicate can restrict your selection to a context node that has a specified string value. Either of the following, equivalent location paths, for example, return the second Make node from our main example.

 /descendant::Make[self::node()='Ford'] /descendant::Make[.='Ford'] 

However, if you use a string where a Boolean is expected, the value of the expression is always true. Any of the following location paths selects all five Make nodes.

 /Insured/descendant::Make['Ford'] /Insured/descendant::Make['false'] /Insured/descendant::Make['true'] 

In each case, the string value of the first returned node is Honda.

You can use an XPath function in a predicate. Let's consider two examples.

The starts-with() function returns true if the second string argument is at the beginning of the first. Each of the following expressions returns the one Model node whose string value is Skylark.

 /descendant::Model[starts-with(self::node(),'Sky')] /descendant::Model[starts-with(.,'Sky')] 

The contains() function returns true if the second string argument is within the first. Each of the following expressions also returns the one Model node whose string value is Skylark.

 /descendant::Model[contains(self::node(),'lark')] /descendant::Model[contains(.,'lark')] /descendant::Model[contains(.,'Sky')] 

Last, you can use a predicate to test for the presence of an attribute or of an immediately subordinate element. Here's an expression that returns 5, which is the number of Vehicle nodes that are parents of a Make node.

 count(/descendant::Vehicle[Make]) 

The next expression returns 2, which is the number of Vehicle nodes that include the Domestic attribute.

 count(/descendant::Vehicle[@Domestic]) 




SOA for the Business Developer. Concepts, BPEL, and SCA
SOA for the Business Developer: Concepts, BPEL, and SCA (Business Developers series)
ISBN: 1583470654
EAN: 2147483647
Year: 2004
Pages: 157
Authors: Ben Margolis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net