1

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
SOLID Developper
  • 672
  • 4
  • 12

2 Answers2

1

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
zx485
  • 28,498
  • 28
  • 50
  • 59
  • I modified the question. It declares the request I should satisfy. – SOLID Developper Jun 30 '22 at 14:31
  • I don't want to depend on the defined precedences at all! I need logical operations between search options (and grouped by parenthesis) as defined by the user on the UI. I think I have to make the AND/OR operations on then sub result lists by the program. There is no way to make one xpath phrase to get a single node list back. – SOLID Developper Jun 30 '22 at 14:45
  • @SOLIDDevelopper: zx485 answered the question that I thought you were asking as well. If you're not asking about operator precedence, what about parentheses in XPath do you not understand? Even with your question update, it's entirely unclear what you're asking if this answer doesn't address your concern. – kjhughes Jun 30 '22 at 14:51
  • The examples from your question are good examples. `and` and `or` are at positions 3 and 4 of the table. So the parenthesis _do_ make a difference, because without them, the expressions would be different. But I'm not sure if this is what you're asking for.... – zx485 Jun 30 '22 at 14:51
  • @kjhughes Could you write the xpath phrases for the two searches? I have just a minimal knowledge about xpath. I can't see parentheses in the table above even now! You say what I dont understand. I say what you dont understand? I asked for xpath phrases not tables. – SOLID Developper Jun 30 '22 at 14:57
  • Maybe you're looking for the [Associative property](https://en.wikipedia.org/wiki/Associative_property). – zx485 Jun 30 '22 at 15:00
  • @SOLIDDevelopper: Parentheses are used to override precedence. When you ask, as you title does, How can I use "parenthesis" in XPath?, most readers will think you're asking about how to override operator precedence because that's what parentheses do in this context. I've reopened your question since you say it's not about operator precedence. From what I can tell now, you're simply asking how to write an XPath that expresses the sketched criteria of `( attr1="value1" AND attr2="value2" ) OR attr3="value3"`. I'll answer that in its own answer... – kjhughes Jun 30 '22 at 15:09
1

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Just one more question, please. What if I would like to select "Circle" tags or "Line" tags with attr="value1" (no other elements). Is it correct : ( //Circle[@attr1="value1"] or //Line[@attr1="value1"] ) ? – SOLID Developper Jun 30 '22 at 15:23
  • @SOLIDDevelopper: Yes, you got it! (...assuming you intended to write `attr1="value1"`, not `attr="value1"`, the first time. Also, terminology: `Circle` and `Line` are *elements*; `attr1` is an *attribute*.) – kjhughes Jun 30 '22 at 15:39