XSLT and XPath Reference

Please leave a remark at the bottom of each page with your useful suggestion.


XSLT and XPath Reference


Table of Contents


XPath Reference

An XPath expression generally defines a pattern in order to select a set of nodes. These patterns are used by XSLT to perform transformations or by XPointer for addressing purpose.

XPath specification specifies seven types of nodes which can be the output of execution of the XPath expression.


Node

  • Root - Root element node of an XML Document.
  • Element - Element node.
  • Text - Text of an element node.
  • Attribute - Attribute of an element node.
  • Comment - Comment
  • Processing Instruction
  • Namespace

XPath uses a path expression to select node or a list of nodes from an XML document. Following is the list of useful paths and expression to select any node/ list of nodes from an XML document.


XPath Notation

Selectors
nodename Selects all nodes with the name "nodename"
/Selects from the root node
//Selects nodes in the whole document
.Selects the current node
..Selects the parent of the current node
@Selects attributes
*Matches any element node
@*Matches any attribute node
//node[1]Selects the first element that is the child of the element.
//node[@class and @id]select the node with both "class" and "id"
//node[count(child)=2]select the node with two "child" elements
//node[contains(@title,"text")]select the node with "text" in the title attribute
//node[child/child1]select the node with "child/child1" child nodes
//node[position() mode 2 ==0]select the odd children elements
//node/text()[2]return the second text element of node
//node[not(@class)]the node without "class" attribute
Accessor Functions
node-name(node)Returns the node-name of the argument node
nilled(node)Returns a Boolean value indicating whether the argument node is nilled
data(item.item,...)Takes a sequence of items and returns a sequence of atomic values
base-uri() fn:base-uri(node)Returns the value of the base-uri property of the current or specified node
document-uri(node)Returns the value of the document-uri property for the specified node
Functions on Nodes
name()Returns the node-name of the argument node
local-name()Returns a Boolean value indicating whether the argument node is nilled
namespace-uri() Takes a sequence of items and returns a sequence of atomic values
lang(lang) Returns the value of the base-uri property of the current or specified node
root()Returns the value of the document-uri property for the specified node
Functions on Numeric Values
number(arg) Returns the numeric value of the argument. The argument could be a boolean, string, or node-set
abs(num)Returns the absolute value of the argument
ceiling(num) Returns the smallest integer that is greater than the number argument
floor(num)Returns the largest integer that is not greater than the number argument
round(num)Rounds the number argument to the nearest integer
Aggregate Functions
count((item,item,...))Returns the count of nodes
avg((arg,arg,...))Returns the average of the argument values
max((arg,arg,...))Returns the argument that is greater than the others
min((arg,arg,...))Returns the argument that is less than the others
sum(arg,arg,...)Returns the sum of the numeric value of each node in the specified node-set
Context Functions
position()Returns the index position of the node that is currently being processed
last()Returns the number of items in the processed node list
current-dateTime() Returns the current dateTime (with timezone)
current-date() Returns the current date (with timezone)
current-time() Returns the current time (with timezone)
Functions on Strings
string(arg) Returns the string value of the argument. The argument could be a number, boolean, or node-set
codepoints-to-string(int,int,...) Returns a string from a sequence of code points
string-to-codepoints(string) Returns a sequence of code points from a string
codepoint-equal(comp1,comp2) Returns true if the value of comp1 is equal to the value of comp2, according to the Unicode code point collation, otherwise it returns false
compare(comp1,comp2) Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 (according to the rules of the collation that is used)
string-join((string,string,...),sep) Returns a string created by concatenating the string arguments and using the sep argument as the separator
substring(string,start,len) Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end
string-length(string) Returns the length of the specified string. If there is no string argument it returns the length of the string value of the current node
normalize-space(string) Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node
normalize-unicode()
upper-case(string) Converts the string argument to upper-case
lower-case(string) Converts the string argument to lower-case
translate(string1,string2,string3) Converts string1 by replacing the characters in string2 with the characters in string3
escape-uri(stringURI,esc-res)
contains(string1,string2) Returns true if string1 contains string2, otherwise it returns false
starts-with(string1,string2) Returns true if string1 starts with string2, otherwise it returns false
ends-with(string1,string2) Returns true if string1 ends with string2, otherwise it returns false
substring-before(str1,str2) Returns the start of string1 before string2 occurs in it
substring-after(str1,str2) Returns the remainder of string1 after string2 occurs in it
matches(string,pattern) Returns true if the string argument matches the pattern, otherwise, it returns false
replace(string,pattern,replace) Returns a string that is created by replacing the given pattern with the replace argument
tokenize(string,pattern)
Functions on Boolean Values
boolean(arg) Returns a boolean value for a number, string, or node-set
not(arg) The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true
true() Returns the boolean value true
false() Returns the boolean value false



Write Your Comments or Suggestion...