Baixe Xpath expressions - w3c e outras Notas de estudo em PDF para Cultura, somente na Docsity!
3 Expressions
This section discusses each of the basic kinds of expression. Each kind of expression has a name such as PathExpr, which is introduced on the left side of the grammar production that defines the expression. Since XPath is a composable language, each kind of expression is defined in terms of other expressions whose operators have a higher precedence. In this way, the precedence of operators is represented explicitly in the grammar. The order in which expressions are discussed in this document does not reflect the order of operator precedence. In general, this document introduces the simplest kinds of expressions first, followed by more complex expressions. For the complete grammar, see Appendix [ A XPath Grammar ]. The highest-level symbol in the XPath grammar is XPath. [1] XPath^ ::= Expr [2] Expr^ ::= ExprSingle^ (","^ ExprSingle)* [3] ExprSingle^ ::= ForExpr | QuantifiedExpr | IfExpr | OrExpr The XPath operator that has lowest precedence is the comma operator, which is used to combine two operands to form a sequence. As shown in the grammar, a general expression (Expr) can consist of multiple ExprSingle operands, separated by commas. The name ExprSingle denotes an expression that does not contain a top-level comma operator (despite its name, an ExprSingle may evaluate to a sequence containing more than one item.) The symbol ExprSingle is used in various places in the grammar where an expression is not allowed to contain a top-level comma. For example, each of the arguments of a function call must be an ExprSingle, because commas are used to separate the arguments of a function call. After the comma, the expressions that have next lowest precedence are ForExpr, QuantifiedExpr, IfExpr, and OrExpr. Each of these expressions is described in a separate section of this document. 3.1 Primary Expressions [Definition: Primary expressions are the basic primitives of the language. They include literals, variable references, context item expressions, and function calls. A primary expression may also be created by enclosing any expression in parentheses, which is sometimes helpful in controlling the precedence of operators.] [41] PrimaryExp^ ::= Literal^ |^ VarRef^ |^ ParenthesizedExpr^ |^ ContextItemExpr^ |^ FunctionCa
r ll 3.1.1 Literals [Definition: A literal is a direct syntactic representation of an atomic value.] XPath supports two kinds of literals: numeric literals and string literals. [42] Literal^ ::= NumericLiteral^ |^ StringLiteral [43] NumericLiteral^ ::= IntegerLiteral^ |^ DecimalLiteral^ |^ DoubleLiteral [71] IntegerLiteral^ ::= Digits [72] DecimalLiteral^ ::= ("."^ Digits)^ |^ (Digits^ "."^ [0-9]) [73] DoubleLiteral^ ::= (("."^ Digits)^ |^ (Digits^ ("."^ [0-9])?))^ [eE]^ [+-]?^ Digits [74] StringLiteral^ ::= ('"'^ (EscapeQuot^ |^ [^"])^ '"')^ |^ ("'"^ (EscapeApos^ |^ [^'])^ "'") [75] EscapeQuot^ ::= '""' [76] EscapeApos^ ::= "''" [81] Digits^ ::= [0-9]+ The value of a numeric literal containing no "." and no e or E character is an atomic value of type xs:integer. The value of a numeric literal containing "." but no e or E character is an atomic value of type xs:decimal. The value of a numeric literal containing an e or E character is an atomic value of type xs:double. The value of the numeric literal is determined by casting it to the appropriate type according to the rules for casting from xs:untypedAtomic to a numeric type as specified in Section 17.1.1 Casting from xs:string and xs:untypedAtomic FO . The value of a string literal is an atomic value whose type is xs:string and whose value is the string denoted by the characters between the delimiting apostrophes or quotation marks. If the literal is delimited by apostrophes, two adjacent apostrophes within the literal are interpreted as a single apostrophe. Similarly, if the literal is delimited by quotation marks, two adjacent quotation marks within the literal are interpreted as one quotation mark. Here are some examples of literal expressions:
- "12.5" denotes the string containing the characters '1', '2', '.', and '5'.
- 12 denotes the xs:integer value twelve.
- 12.5 denotes the xs:decimal value twelve and one half.
- 125E2 denotes the xs:double value twelve thousand, five hundred.
- "He said, ""I don't like it.""" denotes a string containing two quotation marks and one apostrophe. Note: When XPath expressions are embedded in contexts where quotation marks have special significance, such as inside XML attributes, additional escaping may be needed.
not in scope. If a variable is bound in the static context for an expression, that variable is in scope for the entire expression. If a variable reference matches two or more variable bindings that are in scope, then the reference is taken as referring to the inner binding, that is, the one whose scope is smaller. At evaluation time, the value of a variable reference is the value of the expression to which the relevant variable is bound. The scope of a variable binding is defined separately for each kind of expression that can bind variables. 3.1.3 Parenthesized Expressions [46] ParenthesizedExpr^ ::= "("^ Expr?^ ")" Parentheses may be used to enforce a particular evaluation order in expressions that contain multiple operators. For example, the expression (2 + 4) * 5 evaluates to thirty, since the parenthesized expression (2 + 4) is evaluated first and its result is multiplied by five. Without parentheses, the expression 2 + 4 * 5 evaluates to twenty-two, because the multiplication operator has higher precedence than the addition operator. Empty parentheses are used to denote an empty sequence, as described in 3.3. Constructing Sequences. 3.1.4 Context Item Expression [47] ContextItemExpr^ ::= "." A context item expression evaluates to the context item, which may be either a node (as in the expression fn:doc("bib.xml")/books/book[fn:count(./author)>1]) or an atomic value (as in the expression (1 to 100)[. mod 5 eq 0]). If the context item is undefined, a context item expression raises a dynamic error [err:XPDY0002]. 3.1.5 Function Calls [Definition: The built-in functions supported by XPath are defined in [XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)].] Additional functions may be provided in the static context. XPath per se does not provide a way to declare functions, but a host language may provide such a mechanism. [48] FunctionCall^ ::= QName^ "("^ (ExprSingle^ (","^ ExprSingle)*)?^ ")" A function call consists of a QName followed by a parenthesized list of zero or more expressions, called arguments. If the QName in the function call has no namespace prefix, it is considered to be in the default function namespace.
If the expanded QName and number of arguments in a function call do not match the name and arity of a function signature in the static context, a static error is raised [err:XPST0017]. A function call is evaluated as follows:
- Argument expressions are evaluated, producing argument values. The order of argument evaluation is implementation-dependent and a function need not evaluate an argument if the function can evaluate its body without evaluating that argument.
- Each argument value is converted by applying the function conversion rules listed below.
- The function is evaluated using the converted argument values. The result is either an instance of the function's declared return type or a dynamic error. The dynamic type of a function result may be a type that is derived from the declared return type. Errors raised by functions are defined in [XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)]. The function conversion rules are used to convert an argument value to its expected type; that is, to the declared type of the function parameter. The expected type is expressed as asequence type. The function conversion rules are applied to a given value as follows:
- If XPath 1.0 compatibility mode is true and an argument is not of the expected type, then the following conversions are applied sequentially to the argument value V:
- If the expected type calls for a single item or optional single item (examples: xs:string, xs:string?, xs:untypedAtomic, xs:untypedAtomic?, node(), no de()?, item(),item()?), then the value V is effectively replaced by V[1].
- If the expected type is xs:string or xs:string?, then the value V is effectively replaced by fn:string(V).
- If the expected type is xs:double or xs:double?, then the value V is effectively replaced by fn:number(V).
- If the expected type is a sequence of an atomic type (possibly with an occurrence indicator *, +, or ?), the following conversions are applied:
- Atomization is applied to the given value, resulting in a sequence of atomic values.
- Each item in the atomic sequence that is of type xs:untypedAtomic is cast to the expected atomic type. For built-in functions where the expected type is specified asnumeric, arguments of type xs:untypedAtomic are cast to xs:double.
- For each numeric item in the atomic sequence that can be promoted to the expected atomic type using numeric promotion as described in B.1 Type Promotion , the promotion is done.
- For each item of type xs:anyURI in the atomic sequence that can be promoted to the expected atomic type using URI promotion as described in B.1 Type Promotion , the promotion is done.
effect of these initial steps is to establish an initial node sequence that contains the root of the tree in which the context node is found, plus all nodes descended from this root. This node sequence is used as the input to subsequent steps in the path expression. If the context item is not a node, a type erroris raised [err:XPTY0020]. At evaluation time, if the root node above the context node is not a document node, a dynamic error is raised [err:XPDY0050]. Note: The descendants of a node do not include attribute nodes or namespace nodes. Each non-initial occurrence of "//" in a path expression is expanded as described in 3.2. Abbreviated Syntax , leaving a sequence of steps separated by "/". This sequence of steps is then evaluated from left to right. Each operation E1/E2 is evaluated as follows: Expression E1 is evaluated, and if the result is not a (possibly empty) sequence of nodes, a type error is raised [err:XPTY0019]. Each node resulting from the evaluation of E1 then serves in turn to provide an inner focus for an evaluation of E2, as described in 2.1. Dynamic Context. The sequences resulting from all the evaluations of E2 are combined as follows:
- If every evaluation of E2 returns a (possibly empty) sequence of nodes, these sequences are combined, and duplicate nodes are eliminated based on node identity. The resulting node sequence is returned in document order.
- If every evaluation of E2 returns a (possibly empty) sequence of atomic values, these sequences are concatenated, in order, and returned.
- If the multiple evaluations of E2 return at least one node and at least one atomic value, a type error is raised [err:XPTY0018]. Note: Since each step in a path provides context nodes for the following step, in effect, only the last step in a path is allowed to return a sequence of atomic values. As an example of a path expression, child::div1/child::para selects the para element children of the div1 element children of the context node, or, in other words, the para element grandchildren of the context node that have div1 parents. Note: The "/" character can be used either as a complete path expression or as the beginning of a longer path expression such as "/". Also, "" is both the multiply operator and a wildcard in path expressions. This can cause parsing difficulties when "/" appears on the left hand side of "". This is resolved using the leading-lone-slash constraint. For example, "/" and "/ " are valid path expressions containing wildcards, but "/5" and "/ * 5 " raise syntax errors. Parentheses must be used when "/" is used on the left hand side of an operator, as in "(/) * 5 ". Similarly, " 4 + / * 5 " raises a syntax error, but " 4 + (/) * 5 " is a
valid expression. The expression " 4 + /" is also valid, because / does not occur on the left hand side of the operator. 3.2.1 Steps [27] StepExpr^ ::= FilterExpr^ |^ AxisStep [28] AxisStep^ ::= (ReverseStep^ |^ ForwardStep)^ PredicateList [29] ForwardStep^ ::= (ForwardAxis^ NodeTest)^ |^ AbbrevForwardStep [32] ReverseStep^ ::= (ReverseAxis^ NodeTest)^ |^ AbbrevReverseStep [39] PredicateList^ ::= Predicate* [Definition: A step is a part of a path expression that generates a sequence of items and then filters the sequence by zero or more predicates. The value of the step consists of those items that satisfy the predicates, working from left to right. A step may be either an axis step or a filter expression.] Filter expressions are described in 3.3.2 Filter Expressions. [Definition: An axis step returns a sequence of nodes that are reachable from the context node via a specified axis. Such a step has two parts: an axis , which defines the "direction of movement" for the step, and a node test, which selects nodes based on their kind, name, and/or type annotation.] If the context item is a node, an axis step returns a sequence of zero or more nodes; otherwise, a type error is raised [err:XPTY0020]. The resulting node sequence is returned in document order. An axis step may be either a forward step or a reverse step , followed by zero or more predicates. In the abbreviated syntax for a step, the axis can be omitted and other shorthand notations can be used as described in 3.2.4 Abbreviated Syntax. The unabbreviated syntax for an axis step consists of the axis name and node test separated by a double colon. The result of the step consists of the nodes reachable from the context node via the specified axis that have the node kind, name, and/or type annotation specified by the node test. For example, the step child::para selects the para element children of the context node: child is the name of the axis, and para is the name of the element nodes to be selected on this axis. The available axes are described in 3.2.1.1 Axes. The available node tests are described in 3.2.1.2 Node Tests. Examples of steps are provided in 3.2.3 Unabbreviated Syntax and 3.2.4 Abbreviated Syntax. 3.2.1.1 Axes [30] ForwardAxis^ ::= ("child"^ "::") | ("descendant" "::") | ("attribute" "::") | ("self" "::") | ("descendant-or-self" "::") | ("following-sibling" "::") | ("following" "::") | ("namespace" "::") [33] ReverseAxis^ ::= ("parent"^ "::") | ("ancestor" "::")
- the preceding axis contains all nodes that are descendants of the root of the tree in which the context node is found, are not ancestors of the context node, and occur before the context node in document order
- the attribute axis contains the attributes of the context node, which are the nodes returned by the dm:attributes accessor in [XQuery 1.0 and XPath 2.0 Data Model (Second Edition)]; the axis will be empty unless the context node is an element
- the self axis contains just the context node itself
- the descendant-or-self axis contains the context node and the descendants of the context node
- the ancestor-or-self axis contains the context node and the ancestors of the context node; thus, the ancestor-or-self axis will always include the root node
- the namespace axis contains the namespace nodes of the context node, which are the nodes returned by the dm:namespace-nodes accessor in [XQuery 1.0 and XPath 2. Data Model (Second Edition)]; this axis is empty unless the context node is an element node. The namespace axis is deprecated in XPath 2.0. If XPath 1. compatibility mode is true, the namespace axis must be supported. If XPath 1. compatibility mode is false, then support for the namespace axis is implementation- defined. An implementation that does not support the namespace axis when XPath 1. compatibility mode is false must raise a static error [err:XPST0010] if it is used. Applications needing information about the in-scope namespaces of an element should use the functions fn:in-scope-prefixes and fn:namespace-uri-for- prefix defined in [XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)]. Axes can be categorized as forward axes and reverse axes. An axis that only ever contains the context node or nodes that are after the context node in document order is a forward axis. An axis that only ever contains the context node or nodes that are before the context node in document order is a reverse axis. The parent, ancestor, ancestor-or-self, preceding, and preceding-sibling axes are reverse axes; all other axes are forward axes. The ancestor, descendant, following,preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document. [Definition: Every axis has a principal node kind. If an axis can contain elements, then the principal node kind is element; otherwise, it is the kind of nodes that the axis can contain.] Thus:
- For the attribute axis, the principal node kind is attribute.
- For the namespace axis, the principal node kind is namespace.
- For all other axes, the principal node kind is element. 3.2.1.2 Node Tests
[Definition: A node test is a condition that must be true for each node selected by a step.] The condition may be based on the kind of the node (element, attribute, text, document, comment, or processing instruction), the name of the node, or (in the case of element, attribute, and document nodes), the type annotation of the node. [35] NodeTest^ ::= KindTest^ |^ NameTest [36] NameTest^ ::= QName^ |^ Wildcard [37] Wildcard^ ::= "" | (NCName ":" "") | ("" ":" NCName) [Definition: A node test that consists only of a QName or a Wildcard is called a name test .] A name test is true if and only if the kind of the node is the principal node kind for the step axis and the expanded QName of the node is equal (as defined by the eq operator) to the expanded QName specified by the name test. For example, child::para selects the paraelement children of the context node; if the context node has no para children, it selects an empty set of nodes. attribute::abc:href selects the attribute of the context node with the QName abc:href; if the context node has no such attribute, it selects an empty set of nodes. A QName in a name test is resolved into an expanded QName using the statically known namespaces in the expression context. It is a static error [err:XPST0081] if the QName has a prefix that does not correspond to any statically known namespace. An unprefixed QName, when used as a name test on an axis whose principal node kind is element, has the namespace URI of the default element/type namespace in the expression context; otherwise, it has no namespace URI. A name test is not satisfied by an element node whose name does not match the expanded QName of the name test, even if it is in a substitution group whose head is the named element. A node test * is true for any node of the principal node kind of the step axis. For example, child:: will select all element children of the context node, and attribute::* will select all attributes of the context node. A node test can have the form NCName:*. In this case, the prefix is expanded in the same way as with a QName, using the statically known namespaces in the static context. If the prefix is not found in the statically known namespaces, a static error is raised [err:XPST0081]. The node test is true for any node of the principal node kind of the step axis whose expanded QName has the namespace URI to which the prefix is bound, regardless of the local part of the name. A node test can also have the form *:NCName. In this case, the node test is true for any node of the principal node kind of the step axis whose local name matches the given NCName, regardless of its namespace or lack of a namespace.
predicate. The context size is the number of items in the input sequence. The context position is the position of the context item within the input sequence. For the purpose of evaluating the context position within a predicate, the input sequence is considered to be sorted as follows: into document order if the predicate is in a forward-axis step, into reverse document order if the predicate is in a reverse-axis step, or in its original order if the predicate is not in a step. For each item in the input sequence, the result of the predicate expression is coerced to an xs:boolean value, called the predicate truth value , as described below. Those items for which the predicate truth value is true are retained, and those for which the predicate truth value is false are discarded. The predicate truth value is derived by applying the following rules, in order:
- If the value of the predicate expression is a singleton atomic value of a numeric type or derived from a numeric type, the predicate truth value is true if the value of the predicate expression is equal (by the eq operator) to the context position , and is false otherwise. [Definition: A predicate whose predicate expression returns a numeric type is called a numeric predicate .]
- Otherwise, the predicate truth value is the effective boolean value of the predicate expression. Here are some examples of axis steps that contain predicates:
- This example selects the second chapter element that is a child of the context node:
- child::chapter[2]
- This example selects all the descendants of the context node that are elements named "toy" and whose color attribute has the value "red":
- descendant::toy[attribute::color = "red"]
- This example selects all the employee children of the context node that have both a secretary child element and an assistant child element:
- child::employee[secretary][assistant] Note: When using predicates with a sequence of nodes selected using a reverse axis , it is important to remember that the the context positions for such a sequence are assigned inreverse document order. For example, preceding::foo[1] returns the first qualifying foo element in reverse document order, because the predicate is part of an axis step using a reverse axis. By contrast, (preceding::foo)[1] returns the first qualifying foo element in document order, because the parentheses cause (preceding::foo) to be parsed as aprimary expression in which context positions are assigned in document order. Similarly, ancestor::[1] returns the nearest ancestor element, because the ancestor axis is a reverse axis, whereas (ancestor::)[1] returns the root element (first ancestor in document order).
The fact that a reverse-axis step assigns context positions in reverse document order for the purpose of evaluating predicates does not alter the fact that the final result of the step is always in document order. 3.2.3 Unabbreviated Syntax This section provides a number of examples of path expressions in which the axis is explicitly specified in each step. The syntax used in these examples is called the unabbreviated syntax. In many common cases, it is possible to write path expressions more concisely using an abbreviated syntax , as explained in 3.2. Abbreviated Syntax.
- child::para selects the para element children of the context node
- child::* selects all element children of the context node
- child::text() selects all text node children of the context node
- child::node() selects all the children of the context node. Note that no attribute nodes are returned, because attributes are not children.
- attribute::name selects the name attribute of the context node
- attribute::* selects all the attributes of the context node
- parent::node() selects the parent of the context node. If the context node is an attribute node, this expression returns the element node (if any) to which the attribute node is attached.
- descendant::para selects the para element descendants of the context node
- ancestor::div selects all div ancestors of the context node
- ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the context node as well
- descendant-or-self::para selects the para element descendants of the context node and, if the context node is a para element, the context node as well
- self::para selects the context node if it is a para element, and otherwise returns an empty sequence
- child::chapter/descendant::para selects the para element descendants of the chapter element children of the context node
- child::*/child::para selects all para grandchildren of the context node
- / selects the root of the tree that contains the context node, but raises a dynamic error if this root is not a document node
- /descendant::para selects all the para elements in the same document as the context node
- /descendant::list/child::member selects all the member elements that have a list parent and that are in the same document as the context node
- child::para[fn:position() = 1] selects the first para child of the context node
- child::para[fn:position() = fn:last()] selects the last para child of the context node
for child::section/child::para, and the path expression section/@id is an abbreviation forchild::section/attribute::id. Similarly, section/attribute(id) is an abbreviation for child::section/attribute::attribute(id). Note that the latter expression contains both an axis specification and a node test.
- Each non-initial occurrence of // is effectively replaced by /descendant-or- self::node()/ during processing of a path expression. For example, div1//para is short forchild::div1/descendant-or-self::node()/child::para and so will select all para descendants of div1 children. Note: The path expression //para[1] does not mean the same as the path expression /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their respective parents.
- A step consisting of .. is short for parent::node(). For example, ../title is short for parent::node()/child::title and so will select the title children of the parent of the context node. Note: The expression ., known as a context item expression , is a primary expression, and is described in 3.1.4 Context Item Expression. Here are some examples of path expressions that use the abbreviated syntax:
- para selects the para element children of the context node
- selects all element children of the context node
- text() selects all text node children of the context node
- @name selects the name attribute of the context node
- @* selects all the attributes of the context node
- para[1] selects the first para child of the context node
- para[fn:last()] selects the last para child of the context node
- */para selects all para grandchildren of the context node
- /book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node that contains the context node
- chapter//para selects the para element descendants of the chapter element children of the context node
- //para selects all the para descendants of the root document node and thus selects all para elements in the same document as the context node
- //@version selects all the version attribute nodes that are in the same document as the context node
- //list/member selects all the member elements in the same document as the context node that have a list parent
- .//para selects the para element descendants of the context node
- .. selects the parent of the context node
- ../@lang selects the lang attribute of the parent of the context node
- para[@type="warning"] selects all para children of the context node that have a type attribute with value warning
- para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning
- para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
- chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children whose typed value is equal to the string Introduction
- chapter[title] selects the chapter children of the context node that have one or more title children
- employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute
- book/(chapter|appendix)/section selects every section element that has a parent that is either a chapter or an appendix element, that in turn is a child of a book element that is a child of the context node.
- If E is any expression that returns a sequence of nodes, then the expression E/. returns the same nodes in document order, with duplicates eliminated based on node identity. 3.3 Sequence Expressions XPath supports operators to construct, filter, and combine sequences of items. Sequences are never nested—for example, combining the values 1 , (2, 3), and ( ) into a single sequence results in the sequence (1, 2, 3). 3.3.1 Constructing Sequences [2] Expr^ ::= ExprSingle^ (","^ ExprSingle)* [11] RangeExpr^ ::= AdditiveExpr^ (^ "to"^ AdditiveExpr^ )? [Definition: One way to construct a sequence is by using the comma operator , which evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence.] Empty parentheses can be used to denote an empty sequence. A sequence may contain duplicate atomic values or nodes, but a sequence is never an item in another sequence. When a new sequence is created by concatenating two or more input sequences, the new sequence contains all the items of the input sequences and its length is the sum of the lengths of the input sequences. Note:
[Definition: A filter expression consists simply of a primary expression followed by zero or more predicates. The result of the filter expression consists of the items returned by the primary expression, filtered by applying each predicate in turn, working from left to right.] If no predicates are specified, the result is simply the result of the primary expression. The ordering of the items returned by a filter expression is the same as their order in the result of the primary expression. Context positions are assigned to items based on their ordinal position in the result sequence. The first context position is 1. Here are some examples of filter expressions:
- Given a sequence of products in a variable, return only those products whose price is greater than 100.
- $products[price gt 100]
- List all the integers from 1 to 100 that are divisible by 5. (See 3.3.1 Constructing Sequences for an explanation of the to operator.)
- (1 to 100)[. mod 5 eq 0]
- The result of the following expression is the integer 25:
- The following example returns the fifth through ninth items in the sequence bound to variable $orders.
- $orders[fn:position() = (5 to 9)]
- The following example illustrates the use of a filter expression as a step in a path expression. It returns the last chapter or appendix within the book bound to variable $book:
- $book/(chapter | appendix)[fn:last()]
- The following example also illustrates the use of a filter expression as a step in a path expression. It returns the element node within the specified document whose ID value istiger:
- fn:doc("zoo.xml")/fn:id('tiger') 3.3.3 Combining Node Sequences [14] UnionExpr^ ::= IntersectExceptExpr^ (^ ("union"^ | "|") IntersectExceptExpr )* [15] IntersectExceptExpr^ ::= InstanceofExpr^ (^ ("intersect"^ | "except") InstanceofExpr )* XPath provides the following operators for combining sequences of nodes:
- The union and | operators are equivalent. They take two node sequences as operands and return a sequence containing all the nodes that occur in either of the operands.
- The intersect operator takes two node sequences as operands and returns a sequence containing all the nodes that occur in both operands.
- The except operator takes two node sequences as operands and returns a sequence containing all the nodes that occur in the first operand but not in the second operand. All these operators eliminate duplicate nodes from their result sequences based on node identity. The resulting sequence is returned in document order. If an operand of union, intersect, or except contains an item that is not a node, a type error is raised [err:XPTY0004]. Here are some examples of expressions that combine sequences. Assume the existence of three element nodes that we will refer to by symbolic names A, B, and C. Assume that the variables $seq1, $seq2 and $seq3 are bound to the following sequences of these nodes:
- $seq1 is bound to (A, B)
- $seq2 is bound to (A, B)
- $seq3 is bound to (B, C) Then:
- $seq1 union $seq2 evaluates to the sequence (A, B).
- $seq2 union $seq3 evaluates to the sequence (A, B, C).
- $seq1 intersect $seq2 evaluates to the sequence (A, B).
- $seq2 intersect $seq3 evaluates to the sequence containing B only.
- $seq1 except $seq2 evaluates to the empty sequence.
- $seq2 except $seq3 evaluates to the sequence containing A only. In addition to the sequence operators described here, [XQuery 1.0 and XPath 2. Functions and Operators (Second Edition)] includes functions for indexed access to items or sub-sequences of a sequence, for indexed insertion or removal of items in a sequence, and for removing duplicate items from a sequence. 3.4 Arithmetic Expressions XPath provides arithmetic operators for addition, subtraction, multiplication, division, and modulus, in their usual binary and unary forms. [12] AdditiveExpr^ ::= MultiplicativeExpr^ (^ ("+"^ |^ "-")^ MultiplicativeExpr^ )* [13] MultiplicativeExpr^ ::= UnionExpr^ (^ (""^ |^ "div"^ |^ "idiv"^ |^ "mod")^ UnionExpr^ ) [20] UnaryExpr^ ::= ("-"^ |^ "+")*^ ValueExpr [21] ValueExpr^ ::= PathExpr A subtraction operator must be preceded by whitespace if it could otherwise be interpreted as part of the previous token. For example, a-b will be interpreted as a name, but a -