Simple examples

This page is a list of examples that you can use to write your own XPaths and XQueries. The examples on this page are limited in complexity.

Some examples on this page are also used to show XPath/XQuery alternative for some of the deprecated blueprintQuery, domQuery, and domInfo functions.

Many of these functions required a nodeSpec in the form of a Javascript function. This function is, in most cases, replaced by a filter expression.

Tests

XPath tests are used when using one of the configureAs functions or the configureProperties function. These tests are run the node that needs to be checked set as the context item.

Matching any node

Other

self::node()

Open example in playground

Matching any element

Other

self::element()

Open example in playground

Match an element having an "id" attribute

Other

self::element()[@id]

Open example in playground

Match an element having an "id" attribute set to "nodeA"

Other

self::element()[@id = "nodeA"]

Open example in playground

Match hovercrafts full of eels

Other

self::hovercraft[eel and not(*[not(self::eel)])]

Open example in playground

Selecting nodes

Here you will find some examples for selecting nodes. Remember to take a look at the Selecting nodes page for more examples.

Selecting child nodes

Selecting child nodes is simple. If you want to select a child element of the context node, simply write its name. This is called a name test.

The following two examples will return the same result. The second example omits the usage of the child axis, which is the abbreviated form of the child axis.

Child node selector

XQuery

child::childNode

Open example in playground

Abbreviated child node selector

XQuery

childNode

Open example in playground

Use a filter expression to only select child elements which, for example, have an id attribute.

Child node selector selecting a child with an id attribute

XQuery

childNode[@id]

Open example in playground

You can also use the filter expression to select a node at a given index.

Indices in XPath and XQuery are one-based instead of zero-based. This means that the first item in a sequence is located at index 1, the second item at index 2, and so forth.

The filter is part of the step expression. This might lead to unexpected results. Read more about why this happens on the Selecting nodes page.

Child node selector selecting the third child node

XQuery

childNode[3]

Open example in playground

Selecting ancestor nodes

Ancestor axis

XQuery

ancestor::ancestorNode

Open example in playground

Select all ancestor having an attribute "id" set to "wrapper"

XQuery

ancestor::ancestorNode[@id = "wrapper"]

Open example in playground

Selecting descendant nodes

These examples replace the domQuery.findDescendants and blueprintQuery.findDescendants functions.

Descendant axis

XQuery

descendant::descendantNode

Open example in playground

Descendant axis with filter

XQuery

descendant::descendantNode[child::text()]

Open example in playground

Selecting siblings

Following sibling axis

XQuery

following-sibling::sibling

Open example in playground

Preceding sibling axis

XQuery

preceding-sibling::sibling

Open example in playground

Node types

You can select nodes based on their node type.

The following examples can be used to replace the isElement, isTextNode, isProcessingInstruction, isComment, and isDocument functions exposed by domInfo.

Elements

Element kind test

XQuery

element()

Open example in playground

Text nodes

Text node kind test

XQuery

text()

Open example in playground

Processing instructions

Processing instruction kind test

XQuery

processing-instruction()

Open example in playground

Comments

Comment kind test

XQuery

comment()

Open example in playground

Documents

Here we explicitly use the ancestor-or-self axis since a document node can never be a child node of any other node in a document.

This example can be used to replace the blueprintQuery.isInDocument and domQuery.isInDocument functions.

Document node kind test

XQuery

ancestor-or-self::document-node()

Open example in playground

Counting

You can count the number of items in a sequence. You can do this by using the fn:count built-in function.

The following examples can be used to replace the blueprintQuery.getNodeLength and blueprintQuery.getParentChildIndex functions.

Count the number of child elements of the context node

XQuery

count(child::*)

Open example in playground

Count the number of ancestor elements of a given node

XQuery

let $givenNode := .
return count($givenNode/ancestor::*)

Open example in playground

Get the number of characters in a text node

XQuery

let $givenTextNode := .
return string-length($givenTextNode)

Open example in playground

Count the number of preceding nodes to determine the index of the context node

XQuery

count(preceding-sibling::node()) + 1

Open example in playground