Home > Mobile >  How can I write boolean predicates in XPath?
How can I write boolean predicates in XPath?

Time:07-01

I develop a CAD program and I would like to create smart selection operation. The user could formulate search phrases to select/deselect elements in the project. I thought if I build an XML DOM for the elements in the project and I call xpath searches against the DOM to get the list of the matching elements in the project could work.

If I have an Element type with three fields and stored in XML as attr1, attr2, attr3 attributes as Element tags:

<Elements>
  <Element attr1="" attr2="" att3="">
  ...
  <Element attr1="" attr2="" att3="">
</Elements>

How can I write an XPath equals to the following:

( attr1="value1" AND attr2="value2" ) OR attr3="value3"

Or here is another phrase just because you could not say : because of the operation priorities the parentheses are not necessary!

( attr1="value1" OR attr2="value2" ) AND attr3="value3"

I couldn't find anything about the usage of parenthesis in xpath on W3Schools. The call of the IXMLDocument calls are not necessary just the xpath phrases (if possible = there are parenthesis in XPath).

Or I have to do the logical operations on SearchNodes result lists manually?

CodePudding user response:

Your logical criteria of elements of interest, presented in an ad hoc syntax,

( attr1="value1" AND attr2="value2" ) OR attr3="value3"

corresponds to this XPath,

//*[( @attr1="value1" and @attr2="value2" ) or @attr3="value3"]

and will select all elements, regardless of name, that have the listed attribute values that meet the boolean expression in the predicate ([...]).

Replace * with Element to select only those elements named, Element, that meet the criteria.

CodePudding user response:

In what cases you need parenthesis or not is specified at w3.org. There the operator precedence of XPath is specified.

[...] normatively defines built-in precedence among the operators of XPath.

#   Operator                        Associativity  
1   , (comma)                       either  
2   for, let, some, every, if       NA
3   or                              either
4   and                             either
5   eq, ne, lt, le, gt, ge, =, 
    !=, <, <=, >, >=, is, <<, >>    NA
6   ||                              left-to-right
7   to                              NA
8    , - (binary)                   left-to-right
9   *, div, idiv, mod               left-to-right
10  union, |                        either
11  intersect, except               left-to-right
12  instance of                     NA
13  treat as                        NA
14  castable as                     NA
15  cast as                         NA
16  =>                              left-to-right
17  -,   (unary)                    right-to-left
18  !                               left-to-right
19  /, //                           left-to-right
20  [ ], ?                          left-to-right
21  ? (unary)                       NA
  • Related