Skip to content

Operations

Nico Kutscherauer edited this page Jun 21, 2022 · 3 revisions

XPath operations are represented as <operation> elements in the model. Generic sub expressions specifying an operator are modeled as <arg> child element. The most operators are represented as empty elements with a specific name (e.g. /<slash/>).

The Operation Element

An operation element has always a type attribute and contains at least one arg element. Depending on the operation type the operation has a different content model.

Regular Operations

A regular operation contains a list of operator arguments (<arg>) and operator elements. Depending on the operation type only a restricted set of operator elements are available.

Operation Type Operator Elements Sample XPath Expressions
step <slash> foo/@bar
sequence <comma> (1, 2, 3)
additive <plus>, <minus> 1 + 2 - 3
unary <plus>, <minus> -1, +2
multiplicativ <x>, <div>, <mod> 1 * 2 div 3 mod 4
compare <gt>, <lt>, <ge>, <le>, <ne>, <eq> 3 > 2
value-compare <gt>, <lt>, <ge>, <le>, <ne>, <eq> 2 le 3
node-compare <gt>, <lt>, <eq> foo >> bar, $foo is $bar
or <or> foo or bar
and <and> foo and bar
union <union> $foo | $bar, $foo union $bar
intersect-except <intersect>, <except> $foo intersect $bar except $baz
range <to> 1 to 10
map <map> $foo ! @bar
concat <concat> 'foo' || 'bar'
Examples

XPath:

foo/@bar

Model:

<operation type="step">
   <arg>
      <locationStep axis="child">
         <nodeTest name="foo" kind="element"/>
      </locationStep>
   </arg>
   <slash/>
   <arg>
      <locationStep axis="attribute">
         <nodeTest name="bar" kind="attribute"/>
      </locationStep>
   </arg>
</operation>

XPath:

1 + 2 - 3

Model:

<operation type="additive">
   <arg>
      <integer value="1"/>
   </arg>
   <plus/>
   <arg>
      <integer value="2"/>
   </arg>
   <minus/>
   <arg>
      <integer value="3"/>
   </arg>
</operation>

XPath:

foo = 'bar'

Model:

<operation type="compare">
   <arg>
      <locationStep axis="child">
         <nodeTest name="foo" kind="element"/>
      </locationStep>
   </arg>
   <eq/>
   <arg>
      <string value="bar"/>
   </arg>
</operation>

Type Operations

Type operations have only one arg element as child followed by an operator element. As last child an element declares a sequence type for the type operation.

Operation Type Available Operators Sample XPath Expressions
instance-of <instanceOf> $foo instance of xs:string
treat-as <treatAs> $foo treat as document-node()
castable <castableAs> $foo castable as xs:boolean
cast <castAs> $foo cast as xs:integer
Example

XPath:

$foo instance of xs:string*

Model:

<operation type="instance-of">
   <arg>
      <varRef name="foo"/>
   </arg>
   <instanceOf/>
   <itemType occurrence="zero-or-more">
      <atomic name="xs:string"/>
   </itemType>
</operation>

Special Operations

Special operations contains other constructs than only arg elements and operator elements and/or the arguments have special roles specified by a role attribute.

Arrow

The XPath arrow operator (=>) is represented by the operation type arrow. The only available operator element is also an <arrow> element. In opposite to regular operations a function-call element is following on each arrow operator element.

Example

XPath:

$foo => count()

Model:

<operation type="arrow">
   <arg>
      <varRef name="foo"/>
   </arg>
   <arrow/>
   <function-call>
      <function name="count"/>
   </function-call>
</operation>

Postfix

The postfix operation (type='postfix') represents implicit operations which are applied on the result of another sub expression. Samples are:

  • Predicate on sequences: (1, 2, 3)[...], $foo[...]
  • Dynamic function calls: $foo(), (function(){...})()
  • Map lookups: $map?some-key, (map{...})?some-key

In the model the operation element contains only one arg element followed by one of: <predicate>, <function-call> or <lookup>.

Example

XPath:

(1, 2)[. ge 2]

Model:

<operation type="postfix">
   <arg>
      <operation type="sequence">
         <arg>
            <integer value="1"/>
         </arg>
         <comma/>
         <arg>
            <integer value="2"/>
         </arg>
      </operation>
   </arg>
   <predicate>
      <operation type="value-compare">
         <arg>
            <self/>
         </arg>
         <ge/>
         <arg>
            <integer value="2"/>
         </arg>
      </operation>
   </predicate>
</operation>

Variable based Operations

The following operations are based on an inline variable binding:

Operation Type Sample XPath Expression
let-binding for $var in ... return ...
for-loop let $var := ... return ...
some-satisfies some $var in ... satisfies ...
every-satisfies every $var in ... satisfies ...

In opposite of other operations, the content of the regarding operation elements is starting with one or more let elements. The last child is an arg element marked by a role attribute with the value return or satisfies.

Example

XPath:

let $foo := foo return $foo/@bar

Model:

<operation type="let-binding">
   <let name="foo">
      <arg>
         <locationStep axis="child">
            <nodeTest name="foo" kind="element"/>
         </locationStep>
      </arg>
   </let>
   <arg role="return">
      <operation type="step">
         <arg>
            <varRef name="foo"/>
         </arg>
         <slash/>
         <arg>
            <locationStep axis="attribute">
               <nodeTest name="bar" kind="attribute"/>
            </locationStep>
         </arg>
      </operation>
   </arg>
</operation>

Condition

The XPath inline condition (if(...) then ... else ...) is represented by the operation type condition. This is the only operation which contains only - always three - arg elements. Each arg elements represents one of the three sub-expressions for the condition. The order and the role attributes with the values if, then and else specifies the corresponding usage.

Example

XPath:

if ($foo) then $bar else $baz

Model:

<operation type="condition">
   <arg role="if">
      <varRef name="foo"/>
   </arg>
   <arg role="then">
      <varRef name="bar"/>
   </arg>
   <arg role="else">
      <varRef name="baz"/>
   </arg>
</operation>

Brackets

Brackets are only implicit represented in the model. In the model the XML hierarchy describes the prescedence order. An operation element wraps one or more operations on the same level of the XPath precedence order.

Example

The following examples shows the impact on the model by using bracktes:

No Brackets

XPath:

`1 + 2 * 3 + 4`

Model:

<operation type="additive">
   <arg>
      <integer value="1"/>
   </arg>
   <plus/>
   <arg>
      <operation type="multiplicativ">
         <arg>
            <integer value="2"/>
         </arg>
         <x/>
         <arg>
            <integer value="3"/>
         </arg>
      </operation>
   </arg>
   <plus/>
   <arg>
      <integer value="4"/>
   </arg>
</operation>

Useless Brackets

XPath:

`1 + (2 * 3) + 4`

Model (no differences to the above model):

<operation type="additive">
   <arg>
      <integer value="1"/>
   </arg>
   <plus/>
   <arg>
      <operation type="multiplicativ">
         <arg>
            <integer value="2"/>
         </arg>
         <x/>
         <arg>
            <integer value="3"/>
         </arg>
      </operation>
   </arg>
   <plus/>
   <arg>
      <integer value="4"/>
   </arg>
</operation>

Useful Brackets

XPath:

`(1 + 2) * (3 + 4)`

Model:

<operation type="multiplicativ">
   <arg>
      <operation type="additive">
         <arg>
            <integer value="1"/>
         </arg>
         <plus/>
         <arg>
            <integer value="2"/>
         </arg>
      </operation>
   </arg>
   <x/>
   <arg>
      <operation type="additive">
         <arg>
            <integer value="3"/>
         </arg>
         <plus/>
         <arg>
            <integer value="4"/>
         </arg>
      </operation>
   </arg>
</operation>

Clone this wiki locally