XPath Selecting and Filtering Nodes

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.

Selecting nodes

XPath treats XML as a tree of nodes and uses path expressions to select them in an XML document. There are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment and root node.

In Docentric, report data sources are XML-based (DDSP – Docentric Data Source Package), with data records represented as XML elements and data fields as XML attributes. Thus, selecting nodes via path expressions is used for selecting data records and fields in report data sources. Learn more how Docentric utilizes path expressions >>

Path expression Description
. Selects the current node.
.. Selects the parent of the current node.
@ Selects attributes.
/ Selects from the root node.
// Selects nodes in the document from the current node that match the selection no matter where they are.
NodeName Selects all nodes with the name "NodeName".

Learn more on selecting XML nodes using XPath >>

Selecting nodes using wildcards

Wildcards can also be used in path expressions to select XML nodes.

Sample XML fragment

That is used to demonstrate how the wildcards work - in the Example column in the below table.

Wildcard Description Example
* Matches any element node. count(*) will match any node in the current context without its children and return the number of <SalesInvoiceLines> nodes so it returns 3.
@* Matches any attribute node. count(SalesInvoiceLines/PackingSlip/@*) will match all attributes from all <PackingSlip> nodes so it returns 6.
node() Matches any node of any type. count(node()) will select all child nodes of any type without descendants in the current context and return 3 which is a number of <SalesInvoiceLines> nodes.

Filtering nodes using predicates

Predicates are used to find a specific node or a node that contains a specific value and are always embedded in square brackets, e.g. SalesInvoiceLines[@LineAmount>1000]. Docentric usually uses predicates for filtering nodes, e.g. selecting data records with the data fields which fulfill a specific condition.

Sample XML fragment

That is used to demonstrate how the predicates work - in the Result column in the below table.

Path expression Result
SalesInvoiceLines[1]/@LineAmount Returns 700.0000000000.

NOTE: To display the result that is a number in a particular format (e.g. 700.00), it should be formatted by applying Format String (e.g. "n2").
SalesInvoiceLines[last()]/@LineAmount Returns 900.0000000000.

NOTE: To display the result that is a number in a particular format (e.g. 900), it should be formatted by applying Format String (e.g. "n0").
SalesInvoiceLines[@LineAmount > 1000] Returns <SalesInvoiceLines LineAmount="2000.0000000000"></SalesInvoiceLines> node.

See also

XPath Expressions Overview >>
XPath Functions >>
XPath Operators >>
XPath Tutorial on W3Schools >>

IN THIS ARTICLE