XPath Operators
XPath expressions consist of (1) path expressions for selecting and filtering nodes, (2) XPath functions and (3) XPath operators. About XPath expressions and how Docentric utilizes them learn more in the overview tutorial.
Sample XML fragment
The following XML fragment is used to demonstrate how the XPath operators work - in the Example column in the below tables.
1 2 3 4 5 |
<ReportData> <SalesInvoiceLines LineAmount="1920.0000000000" Name="Mid-Range Speaker"> <PackingSlip PackingSlip="" ShowCustPackingSlipTrans="false" /> </SalesInvoiceLines> </ReportData> |
Comparison operators
Syntax | Description | Example |
---|---|---|
= | Equal to | SalesInvoiceLines/@Name = "Mid-Range Speaker" returns True. |
!= | Not equal to | SalesInvoiceLines/@Name != "Mid-Range Speaker" returns True. |
< | Less than | SalesInvoiceLines/@LineAmount < 2000 returns True. |
> | Greater than | SalesInvoiceLines/@LineAmount > 1000 returns True. |
<= | Less than or equal | SalesInvoiceLines/@LineAmount <= 1920 returns True. |
>= | Greater than or equal | SalesInvoiceLines/@LineAmount >= 1920 returns True. |
Logical operators
Syntax | Description | Example |
---|---|---|
and | Logical AND |
child::PackingSlip and SalesInvoiceLines/PackingSlip/ |
or | Logical OR |
child::PackingSlip or SalesInvoiceLines/PackingSlip/ |
| | Set operation; returns the union of two sets of nodes. | (child::PackingSlip) | (child::SalesInvoiceLines) will return all nodes that are either children of the PackingSlip element or children of the SalesInvoiceLines element. |
Numeric operators
Syntax | Description | Example |
---|---|---|
+ | Add | SalesInvoiceLines/@LineAmount + 80 returns 2000. |
- | Subtract | SalesInvoiceLines/@LineAmount - 20 returns 1900. |
* | Multiply | SalesInvoiceLines/@LineAmount * 2 returns 3840. |
div | Divide | SalesInvoiceLines/@LineAmount div 2 returns 960. |
mod | Modulo. Returns the remainder from a truncating division. | SalesInvoiceLines/@LineAmount mod 100 returns 20. |
Axes
An axis represents a relationship to the current node and is used to locate nodes relative to that node in the tree (i.e. XML document).
Syntax | Description | Example |
---|---|---|
ancestor:: | The ancestor of the context node. The ancestors of the context node consist of the parent of the context node and the parent's parent and so on; thus, the ancestor:: axis always includes the root node unless the context node is the root node. | ancestor::ReportData selects the ancestor nodes of the current node, up to the root node. In this case, the expression will select the <ReportData> node. |
ancestor-or-self:: | The context node and its ancestors. The ancestor-or-self:: axis always includes the root node. | ancestor-or-self::SalesInvoiceLines selects the ancestor nodes of the current node, including the current node itself. In this case, the expression will also select the current node, which is the <SalesInvoiceLines> node. |
attribute:: | The attributes of the context node. This axis will be empty unless the context node is an element. | attribute::Name selects the attributes of the current node. In this case, the expression will select the Name attribute of the <SalesInvoiceLines> node. |
child:: | The children of the context node. A child is any node immediately below the context node in the tree. However, neither attribute nor namespace nodes are considered children of the context node. | child::PackingSlip selects the child nodes of the current node. In this case, the expression will select the <PackingSlip> node. |
descendant:: | The descendants of the context node. A descendant is a child or a child of a child and so on; thus, the descendant:: axis never contains attribute or namespace nodes. | descendant::SalesInvoiceLines selects the descendant nodes of the current node. In this case, the expression will select the <SalesInvoiceLines> node and all of its child nodes, including the <PackingSlip> node. |
descendant-or-self:: | The context node and its descendants. | descendant-or-self::SalesInvoiceLines selects the descendant nodes of the current node, including the current node itself. In this case, the expression will also select the current node, which is the <SalesInvoiceLines> node. |
following:: | All nodes that are after the context node in the tree, excluding any descendants, attribute nodes, and namespace nodes. | following::PackingSlip selects all nodes that are after the current node in the tree, excluding any descendants, attribute nodes, and namespace nodes. In this case, the expression will select the <PackingSlip> node. |
following-sibling:: | All the following siblings of the context node. The following-sibling:: axis identifies just those children of a parent node who appear in the tree after the context node. This axis excludes all other children that appear before the context node. If the context node is an attribute node or namespace node, the following-sibling:: axis is empty. | following-sibling::PackingSlip selects all the following siblings of the current node. In this case, the expression will be empty because the <SalesInvoiceLines> node does not have any following siblings. |
namespace:: | The namespace nodes of the context node. There is a namespace node for every namespace which is in scope for the context node. This axis will be empty unless the context node is an element. | namespace::xmlns selects the namespace nodes of the current node. In this case, the expression will select the namespace node with the prefix xmlns. |
parent:: | The parent of the context node if there is one. The parent is the node immediately above the context node in the tree. | parent::SalesInvoiceLines selects the parent of the current node. In this case, the expression will select the <ReportData> node. |
preceding:: | All nodes that are before the context node in the tree, excluding any ancestors, attribute nodes, and namespace nodes. One way to think of the preceding axis is all nodes whose content occurs in their entirety before the start of the context node. | preceding::SalesInvoiceLines selects all nodes that are before the current node in the tree, excluding any ancestors, attribute nodes, and namespace nodes. In this case, the expression will be empty because the <PackingSlip> node is the first child of the <SalesInvoiceLines> node. |
preceding-sibling:: | All the preceding siblings of the context node. The preceding-sibling:: axis identifies just those children of a parent node who appear in the tree before the context node. This axis excludes all other children that appear after the context node. If the context node is an attribute node or namespace node, the preceding-sibling:: axis is empty. | preceding-sibling::PackingSlip selects all the preceding siblings of the current node. In this case, the expression will be empty because the <PackingSlip> node does not have any preceding siblings. |
self:: | Just the context node itself. | self::PackingSlip selects just the context node itself. In this case, the expression will select the <PackingSlip> node. |
See also
XPath Expressions Overview >>
XPath Selecting and Filtering Nodes >>
XPath Functions >>