XPath

Xpath content
admin's picture

XPath - Some Important Abbreviations

The most important abbreviation is thatchild::can be omitted from a location step. In effect,child is the default axis.
 
For example, a location path
 
Data/username is short for
child::Data/child::username
There is also an abbreviation for attributes: attribute::can be abbreviated to @.
 
For example, a location path
 
/*/*/*/*/*/*/*/perm[@type="admin"]is short for
/*/*/*/*/*/*/*/child::perm[attribute::type="admin"]
 
... and so selects
 
permchildren with a type attribute with value equal to admin.
//is short for /descendant-or-self::node()/.
 
For example,
 
1.)
 
//perm is short for
/descendant-or-self::node()/child::perm
 
... and so will select anyperm element in the document (even a perm element that is a document element will be selected by //perm since the document element node is a child of the root node);
 
2.)
 
Data//perm is short for
Data/descendant-or-self::node()/child::perm
 
... and so will select all perm descendants of Data children.
 
  • A location step 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
 
  • Example that can be tested on example XML_example.xml:
 
//getUserToken/usertoken/permissions/../usernameis short for
//getUserToken/usertoken/permissions/parent::node()/child::username
 

 
admin's picture

XPath - Abbreviated Syntax

 
XPath Description
* selects all element children of the context node (child::*)
text()
selects all text node children of the context node
@name selects the name attribute of the context node (attribute::name)
@* selects all the attributes of the context node (attribute::*)
perm[1]
selects the first perm child of the context node
perm[last()]
selects the last perm child of the context node
../perm select the perm children of the parent of the context node (parent::name)
perm selects the perm element, child of the context node (child::perm)
//perm selects all the perm) descendants of the document root and thus selects all perm elements in the same document as the context node (descendant::perm
. selects the context node (self::node())
.. selects the parent of the context node (parent::node())
//permissions/perm[5]
selects the fifth perm of the permissions
../@attName
selects the attName attribute of the parent of the context node
perm[@type="admin"]
selects all perm children of the context node that have a type attribute with value admin
para[@type="admin"][5]
selects the fifth perm child of the context node that has a type attribute with value admin
para[5][@type="admin"]
selects the fifth perm child of the context node if that child has a type attribute with value admin
 
 

 

admin's picture

XPath - String, Boolean and Number Functions

  

String functions
Boolean functions
Number functions
string string(object?)
 
converts an object to a string
boolean not(boolean)
 
returns true if its argument is false, and false otherwise
number number(object?)
 
converts its argument to a number
string concat(string, string, string*)
 
returns the concatenation of its arguments
boolean true()
 
returns true
number sum(node-set)
 
returns the sum, for each node in the argument node-set
boolean starts-with(string, string)
 
returns true if the first argument string starts with the second argument string, and otherwise returns false
boolean false()
 
returns false
number floor(number)
 
returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer
boolean contains(string, string)
 
returns true if the first argument string contains the second argument string, and otherwise returns false
boolean lang(string)
 
returns true or false depending on whether the language of the context node as specified by xml:lang attributes is the same as or is a sublanguage of the language specified by the argument string
number ceiling(number)
 
returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer
string substring-before(string, string)
 
returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string
 
number round(number)
 
returns the number that is closest to the argument and that is an integer
string substring-after(string, string)
 
returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string
 
 
string substring(string, number, number?)
 
returns the substring of the first argument starting at the position specified in the second argument with length specified in the third argument
 
 
number string-length(string?)
 
returns the number of characters in the string
 
 
string normalize-space(string?)
 
returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space
 
 
string translate(string, string, string)
 
returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string
 
 

 
 
 
admin's picture

XPath - Axes and Node Set Functions

 
 
Axes Node Set Functions
child
 
 
axis contains the children of the context node
last()
returns a number equal to the context size from the expression evaluation context
descendant
axis contains the descendants of the context node; a descendant is a child or a child of a child and so on
position()
function returns a number equal to the context position from the expression evaluation context
parent
axis contains the parent of the context node
count(node-set)
returns the number of nodes in the argument node-set
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
node-set id(object)
selects elements by their unique ID
following-sibling
axis contains all the following siblings of the context node
string local-name(node-set?)
 
returns the local part of the expanded-name of the node in the argument node-set that is first in document order
preceding-sibling
axis contains all the preceding siblings of the context node
string namespace-uri(node-set?)
 
returns the namespace URI of the expanded-name of the node in the argument node-set that is first indocument order
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
string name(node-set?)
returns a string containing a QName representing the expanded-name of the node in the argument node-set that is first in document order
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
 
attribute
axis contains the attributes of the context node; the axis will be empty unless the context node is an element
 
namespace
axis contains the namespace nodes of the context node; the axis will be empty unless the context node is an element
 
self
axis contains just the context node itself
 
descendant-or-self
axis contains the context node and the descendants of the context node
 
ancestor-or-self
axis contains the context node and the ancestors of the context node; thus, the ancestor axis will always include the root node
 
 
admin's picture

XPath - Functions (all)

XPath is a language for finding information in an XML document.
XPath includes many built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
  • XPath is a syntax for defining parts of an XML document
  • XPath uses path expressions to navigate in XML documents
  • XPath contains a library of standard functions
  • XPath is a major element in XSLT
  • XPath is a W3C recommendation
Before Xpath examples, there must be a part of theory and probably a table with path locations axes and functions (number, string, boolean) as a resume after theory!
 
Note: This document describes Xpath version 1.0 that is implemented in Sterling Integrator.
 
All the Xpath functions:
 

Axes
Node Set Functions
String functions
Boolean functions
Number functions
Child
 
axis contains the children of the context node
 
returns a number equal to the context size from the expression evaluation context
string string(object?)
 
converts an object to a string
boolean boolean(object)
 
converts its argument to a boolean
number number(object?)
 
converts its argument to a number
descendant
 
axis contains the descendants of the context node; a descendant is a child or a child of a child and so on
 
function returns a number equal to the context position from the expression evaluation context
string concat(string, string, string*)
 
returns the concatenation of its arguments
boolean not(boolean)
 
returns true if its argument is false, and false otherwise
number sum(node-set)
 
returns the sum, for each node in the argument node-set
parent
 
axis contains the parent of the context node
count(node-set)
 
returns the number of nodes in the argument node-set
boolean starts-with(string, string)
 
returns true if the first argument string starts with the second argument string, and otherwise returns false
boolean true()
 
returns true
number floor(number)
 
returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer
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
node-set id(object)
 
selects elements by their unique ID
boolean contains(string, string)
 
returns true if the first argument string contains the second argument string, and otherwise returns false
boolean false()
 
returns false
number ceiling(number)
 
returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer
following-sibling
 
axis contains all the following siblings of the context node
string local-name(node-set?)
 
returns the local part of the expanded-name of the node in the argument node-set that is first in document order
string substring-before(string, string)
 
returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string
boolean lang(string)
 
returns true or false depending on whether the language of the context node as specified by xml:lang attributes is the same as or is a sublanguage of the language specified by the argument string
number round(number)
 
returns the number that is closest to the argument and that is an integer
preceding-sibling
 
axis contains all the preceding siblings of the context node
string namespace-uri(node-set?)
returns the namespace URI of the expanded-name of the node in the argument node-set that is first indocument order
string substring-after(string, string)
 
returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string
 
 
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
string name(node-set?)
 
returns a string containing a QName representing the expanded-name of the node in the argument node-set that is first in document order
string substring(string, number, number?)
 
returns the substring of thefirst argument starting at the position specified in the second argument with length specified in the third argument
 
 
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
 
number string-length(string?)
 
returns the number of characters in the string
 
 
attribute
 
axis contains the attributes of the context node; the axis will be empty unless the context node is an element
 
string normalize-space(string?)
 
returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space
 
 
namespace
 
axis contains the namespace nodes of the context node; the axis will be empty unless the context node is an element
 
string translate(string, string, string)
 
returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string
 
 
self
 
axis contains just the context node itself
 
 
 
 
descendant-or-self
 
axis contains the context node and the descendants of the context node
 
 
 
 
ancestor-or-self
 
axis contains the context node and the ancestors of the context node; thus, the ancestor axis will always include the root node
 
 
 
 

admin's picture

XPath

Find the XPath explanation and examples in the following documents.

Syndicate content