W3C XML - Technical University of Košiceporuban/wt/XPath.pdf · W3C XML XPath Overview XPath in...
Transcript of W3C XML - Technical University of Košiceporuban/wt/XPath.pdf · W3C XML XPath Overview XPath in...
W3CXML
XPath
Jaroslav Porubän2008
W3CXML XPath Overview
XPath is a language for addressing parts of an XML document
XPath gets its name from its use of a path notation as in URLs
XPath operates on the abstract structure of an XML document
XPath models an XML document as a tree of nodes
XPath does not exist in isolation
W3CXML XPath Overview
XPath in the context of XML technologies is a major element in the XSLT
standard XQuery and XPointer are both built on
XPath expressions XQuery 1.0 and XPath 2.0 share the
same data model and support the same functions and operators
XPath is not an XML language
W3CXML XPath Example
XML<people> <person> <name license="kill"> <first>James</first> <last>Bond</last> </name> <name> <first>Major</first> <last>Zeman</last> </name> </person></people>
XPath/people/person/name/people/person/name/@license/people/person/name[@license="kill"]
W3CXML XPath Nodes
Seven kind of tree nodes: root node element nodes attribute nodes text nodes namespace nodes processing-instruction nodes comment nodes
W3CXML Relationship of Nodes
Parent each element and attribute has one parent
Children element nodes may have one or more
children Siblings
nodes with the same parent Ancestors
node’s parent, parent of parent, ... Descendants
node’s children, children's children, ...
W3CXML Context Node
The context indicates the location of the node where a processor is currently situated – context node
Context includes a context position and a context size
Absolute location path starts with slash /
Relative location path is specified with context in mind
W3CXML XPath Expression
XPath expression consists of a sequence of location steps (separated by /)
Location step is made of Axis A node test An optional predicate
child::person[position()=2]Axis Node test Predicate
W3CXML XPath Axes (Directions)
child axis attribute axis ancestor axis ancestor-or-self axis descendant axis descendant-or-self axis following axis following-sibling axis preceding axis preceding-sibling axis parent axis self axis namespace axis (deprecated in XPath 2.0)
W3CXML child Axis (Default Axis)
Selects nodes that are immediate child nodes of the context node
Selects nodes with name personchild::personperson (abbreviated)
Select all element node (only element nodes have names)child::** (abbreviated)
W3CXML child Axis
Selects all nodes, including comment nodes, processing instruction nodes, and text nodeschild::node()node() (abbreviated)
Selects text nodes onlychild::text()text() (abbreviated)
Selects comment nodes onlychild::comment()comment() (abbreviated)
Selects processing instruction nodes onlychild::processing-instruction()processing-instruction() (abbreviated)
W3CXML attribute Axis
Selects the attribute nodes associated with an element node
Selects attributes with name licenseattribute::license@license (abbreviated)
Select all attribute nodes of the context nodeattribute::*@* (abbreviated)
W3CXML ancestor Axis
Selects the parent node of the context node and its parent up to the hierarchy till the root node of the document is selected
Selects all ancestor nodes of the context node with name personancestor::person
Selects all ancestor nodes of the context nodeancestor::*
W3CXML ancestor-or-self Axis
Selects the context node and its parent node and its parent up to the hierarchy till the root node of the document is selected
Selects context node and all ancestor nodes of the context node with name personancestor-or-self::person
Selects context node and all ancestor nodes of the context nodeancestor-or-self::*
W3CXML descendant Axis
Selects the child nodes of the context node, the child nodes of those child nodes down to the hierarchy
Selects the context node and all descendant nodes of the context node with name firstdescendant::first
Selects the context node and all descendant nodes of the context nodedescendant::*
W3CXML descendant-or-self Axis
Selects the context node and child nodes of the context node, the child nodes of those child nodes down to the hierarchy
Selects the context node and all descendant nodes of the context node with name firstdescendant-or-self::first//first (abbreviated)
Selects the context node and all descendant nodes of the context nodedescendant-or-self::*
W3CXML following Axis
Selects all nodes that come after the context node in document order, but excludes all descendant nodes and any attribute nodes and namespace nodes associated with the context node
Selects the following nodes of the context node with name firstfollowing::first
Selects following nodes of the context nodefollowing::*
W3CXML following-sibling Axis
Selects all sibling nodes that share their parent node with the context node
Selects the following sibling nodes of the context node with name firstfollowing-sibling::first
Selects following sibling nodes of the context node
following-sibling::*
W3CXML preceding Axis
Selects all nodes that come before the context node in document order, excluding nodes in the ancestor axis and attribute and namespace nodes
Selects the preceding nodes of the context node with name firstpreceding::first
Selects preceding nodes of the context nodepreceding::*
W3CXML preceding-sibling Axis
Selects those nodes that are in the preceding axis and that also share a parent node with the context node
Selects the preceding sibling nodes of the context node with name firstpreceding-sibling::first
Selects preceding sibling nodes of the context nodepreceding-sibling::*
W3CXML parent Axis
Selects the parent node of the context node
Selects the parent node with name person if existsparent::person
Selects the parent nodeparent::node().. (abbreviated)
W3CXML self Axis
Selects the context node
Selects the context nodeself::node(). (abbreviated)
W3CXML namespace Axis
Selects namespace nodes. An element node has a separate namespace node for each in-scope namespace.
Selects the namespace nodes of the context node with name firstxmml::first
W3CXML XPath Datatypes
An expression is evaluated to: node-set (an unordered collection of
nodes without duplicates) boolean (true or false) number (a floating-point number) string (a sequence of unicode
characters)
W3CXML XPath Operators
W3CXML XPath Functions
XPath 1.0 Functions Boolean Functions Node-Set Functions Numeric Functions String Functions
W3CXML Boolean XPath Functions
boolean(object)- takes an object as its argument and returns a boolean value
false()- returns the boolean value false true()- returns the boolean value true not(boolean)- returns true if the argument
evaluates to false, and false if the argument evaluates to true
lang(string) - returns true if the language of the context node is the language indicated by the string argument or one of its sublanguages
W3CXML Node-Set XPath Functions
count(node-set)- returns a number of nodes in the node-set
position()- returns a value equal to the context position
last()- returns a value equal to the context size id(object)- returns a node-set containing any
node that has an attribute of type ID equal to the function’s argument
local-name(node-set?)- returns the local part of the element name if it exists
name(node-set?)- returns the name of the node in prefix:localpart format
namespace-uri(node-set?) - returns the namespace URI
W3CXML Numeric XPath Functions
number(object?)- takes a string, boolean or node-set as its argument and returns a number
ceiling(number)- returns the smallest integer greater than this
floor(number)- returns the largest integer that is lower than this
round(number)- returns the integer that is closest to the number argument
sum(node-set)- returns the sum of the value of each individual node after converting the values to a numeric type if possible. Be careful when using sum()
W3CXML String XPath Functions
string(object?)- takes a boolean, node-set, or number as its argument and returns a string value
concat(string, string, string*)- returns the concatenation of its arguments
starts-with(string, string)- returns true if the first argument string starts with the second argument string, and otherwise returns false
contains(string, string)- returns true if the first argument string contains the second argument string, and otherwise returns false
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.
W3CXML String XPath Functions
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, or the empty string if the first argument string does not contain the second argument string.
substring-before(string, string)- function returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string
string-length()- number of characters in the string
W3CXML String XPath Functions
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
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
W3CXML XPath Predicates
Predicates are used to filter node-sets selected using an axis and location step
A predicate is optional in each location step of an XPath expression
There can be more than one predicate in any one location step.
/people/person[@licence="kill"]//person[@licence="kill"][@weapon="wisdom"]
W3CXML XPath User Functions
Set of predefined functions can be extended by user defined functions if needed
User XPath functions are implemented in the context of XPath processor