XML information pipe

Ihre Spezialisten für XML

Banner Werbung

List of XPaths

Contents 1 - 19


XPath as filesystem addressing

The basic XPath syntax is similar to filesystem addressing. If the path starts with the slash / , then it represents an absolute path to the required element.

/AAA
/AAA/CCC
/AAA/DDD/BBB

XPath 1.0 introduction

Start with //

If the path starts with // then all elements in the document which fulfill following criteria are selected.

//BBB
//DDD/BBB

XPath 1.0 introduction

All elements: *

The star * selects all elements located by preceeding path

/AAA/CCC/DDD/*
/*/*/*/BBB
//*

XPath 1.0 introduction

Further conditions inside []

Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.

/AAA/BBB[1]
/AAA/BBB[last()]

XPath 1.0 introduction

Attributes

Attributes are specified by @ prefix.

//@id
//BBB[@id]
//BBB[@name]
//BBB[@*]
//BBB[not(@*)]

XPath 1.0 introduction

Attribute values

Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space.

//BBB[@id='b1']
//BBB[@name='bbb']
//BBB[normalize-space(@name)='bbb']

XPath 1.0 introduction

Nodes counting

Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.

//*[name()='BBB']
//*[starts-with(name(),'B')]
//*[contains(name(),'C')]

XPath 1.0 introduction

Playing with names of selected elements

Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.

//*[name()='BBB']
//*[starts-with(name(),'B')]
//*[contains(name(),'C')]

XPath 1.0 introduction

Length of string

The string-length function returns the number of characters in the string. You must use < as a substitute for < and > as a substitute for > .

//*[string-length(name()) = 3]
//*[string-length(name()) < 3]
//*[string-length(name()) > 3]

XPath 1.0 introduction

Combining XPaths with |

Several paths can be combined with | separator.

//CCC | //BBB
/AAA/EEE | //BBB
/AAA/EEE | //DDD/CCC | /AAA | //BBB

XPath 1.0 introduction

Child axis

The child axis contains the children of the context node. The child axis is the default axis and it can be omitted.

/AAA
/child::AAA
/AAA/BBB
/child::AAA/child::BBB
/child::AAA/BBB

XPath 1.0 introduction

Descendant axis

The descendant axis contains 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::*
/AAA/BBB/descendant::*
//CCC/descendant::*
//CCC/descendant::DDD

XPath 1.0 introduction

Parent axis

The parent axis contains the parent of the context node, if there is one.

//DDD/parent::*

XPath 1.0 introduction

Ancestor axis

The ancestor axis contains the ancestors of the context node; the ancestors of the context node consist of the parent of context node and the parent's parent and so on; thus, the ancestor axis will always include the root node, unless the context node is the root node.

/AAA/BBB/DDD/CCC/EEE/ancestor::*
//FFF/ancestor::*

XPath 1.0 introduction

Following-sibling axis

The following-sibling axis contains all the following siblings of the context node.

/AAA/BBB/following-sibling::*
//CCC/following-sibling::*

XPath 1.0 introduction

Preceding-sibling axis

The preceding-sibling axis contains all the preceding siblings of the context node

/AAA/XXX/preceding-sibling::*
//CCC/preceding-sibling::*

XPath 1.0 introduction

Following axis

The following axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes.

/AAA/XXX/following::*
//ZZZ/following::*

XPath 1.0 introduction

Preceding axis

The preceding axis contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes and namespace nodes

/AAA/XXX/preceding::*
//GGG/preceding::*

XPath 1.0 introduction

Descendant-or-self axis

The descendant-or-self axis contains the context node and the descendants of the context node

/AAA/XXX/descendant-or-self::*
//CCC/descendant-or-self::*

XPath 1.0 introduction