410 likes | 684 Views
XPath. Kanda Runapongsa ( krunapon@kku.ac.th ) Dept. of Computer Engineering Khon Kaen University. What is XPath?. XPath is a language designed to address specific parts of an XML document It was designed to be used by both XSLT and XPointer
E N D
XPath Kanda Runapongsa (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University
What is XPath? • XPath is a language designed to address specific parts of an XML document • It was designed to be used by both XSLT and XPointer • XSLT: transforms an XML document into any text-based format, such as HTML • XPointer: the basis for a fragment identifies only for text/xml & application/xml media types 168493: XML and Web Services (II/2546)
XPath Expressions • The XPath data model views a document as a tree of nodes • An instance of the XPath language is called an expression • A path expression is an expression used for selecting a node set by following a path or steps 168493: XML and Web Services (II/2546)
XPath Expressions (Cont.) • XPath use path expressions to identify nodes in an XML document • These path expressions look very much like the expressions you see when you work with a computer file system Documents/courses/168493/xpath.ppt 168493: XML and Web Services (II/2546)
Sample Document <?xml version=“1.0”?> <catalog> <cd country=“UK”> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <price>9.90</price> </cd> <cd country=“USA”> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <price>10.90</price> </cd> </catalog> 168493: XML and Web Services (II/2546)
Path Expressions Examples • /catalog This XPath expression selects the ROOT element catalog • /catalog/cd This XPath expression selects all the cd elements of the catalog element • /catalog/cd/price This XPath expression selects all the price elements of all the cd elements of the catalog element 168493: XML and Web Services (II/2546)
/ and // • / select the root node of the current document Example: /catalog Select the root element catalog • An absolute path to an element • // select nodes that are the descendant nodes of context node Example: //cd Select the cd elements in the document 168493: XML and Web Services (II/2546)
Selecting Unknown Elements • Wildcards (*) can be used to select unknown elements • /catalog/cd/* This XPath expression selects all the child elements of all the cd elements of the catalog element 168493: XML and Web Services (II/2546)
Selecting Unknown Elements (Cont.) • /catalog/*/price This expression selects all the price elements that are grandchild elements of the catalog element • /*/*/price This expression selects all price elements which have 2 ancestors • How to select all elements? 168493: XML and Web Services (II/2546)
Selecting Branches • By using square brackets in an XPath expression you can specify an element further • /catalog/cd[1] The XPath expression selects the first cd child element of the catalog element 168493: XML and Web Services (II/2546)
Selecting Branches (Cont.) • /catalog/cd[last()] The expression selects the last cd element of the catalog element • /catalog/cd[price] The expression selects all the cd elements of the catalog element that have a price element 168493: XML and Web Services (II/2546)
Selecting Branches (Cont.) • /catalog/cd[price=10.90] Selects all the cd elements of the catalog element that have a price element with a value of 10.90 • /catalog/cd[price=10.90]/price Selects all the price elements of all the cd elements of the catalog element that have a price element with a value of 10.90 168493: XML and Web Services (II/2546)
Selecting Several Branches • By using the | operator in an XPath expression you can select several paths • /catalog/cd/title | /catalog/cd/artist Selects all the title and artist elements of the cd element of the catalog element 168493: XML and Web Services (II/2546)
Selecting Attributes • In XPath all attributes are specified by the @ prefix • //@country Selects all attributes named country • //cd[@country] Selects all cd elements which have an attribute named country 168493: XML and Web Services (II/2546)
Selecting Attributes (Cont.) • //cd[@*] Selects all cd elements which have any attribute • //cd[@country=‘UK’] Selects all cd elements which have an attribute named country with a value of ‘UK’ 168493: XML and Web Services (II/2546)
A Location Path • It is the most important kind of expressions in the XPath notation • It can be absolute or relative • An absolute path starts with a slash (/) and a relative location path does not start with a slash • The location path consists of one or more location steps, each separated by a slash 168493: XML and Web Services (II/2546)
A Location Path (Cont.) • An absolute location path: /step/step/… • A relative location path: step/step/… • The location steps are evaluated in order one at a time, from left to right • Each step is evaluated against the nodes in the current node-set 168493: XML and Web Services (II/2546)
A location Path (Cont.) • The current node-set that is being selected is called the set of the context nodes • Each step of a location path (a location step) has three following parts: • An axis – specifies the tree relationship between the nodes selected by the location step and the current node • A node test – specifies the node type • Zero or more predicates – use expressions to further refine the set of nodes 168493: XML and Web Services (II/2546)
A Location Step • The syntax for a location step is: axisname::nodetest[predicate] • Example: child::cd[price=9.90] • An axis defines a node-set relative to the current node • A node test is used to identify a node with an axis 168493: XML and Web Services (II/2546)
Axis Names • ancestor: contains all ancestors (parent, grandparent, etc.) of the current node • ancestor-or-self: contains the current node plus all its ancestors (parent, grandparent, etc.) • What is the node that this axis always includes? 168493: XML and Web Services (II/2546)
Axis Names (Cont.) • attribute: contains all attributes of the current node • child: contains all children of the current node • descendant: contains all descendants (children, grandchildren, etc.) of the current node • Does this axis contain attribute nodes? 168493: XML and Web Services (II/2546)
Axis Names (Cont.) • descendant-or-self: contains the current node plus all its descendants • following: contains everything in the document after the closing tag of the current node, except descendants • following-sibling: contains all siblings after the current node 168493: XML and Web Services (II/2546)
Axis Names (Cont.) • namespace: contains all namespace nodes of the current node • parent: contains the parent of the current node • preceding: contains everything in the document that is before the starting tag of the current node, except ancestors 168493: XML and Web Services (II/2546)
Axis Names (Cont.) • preceding-sibling: contains all siblings before the current node • self: contains the current node • For any given context node v, the four major axes specify a partitioning of the document containing v v/descendants U v/ancestors U v/following U v/preceding U {v} 168493: XML and Web Services (II/2546)
Examples • child:cd Selects all cd elements that are children of the current node • attribute::src Selects the src attribute of the current node 168493: XML and Web Services (II/2546)
Examples (Cont.) • child::cd[position() < 6] Selects the first five cd children of the current node • child::cd[attribute::type=“classic”] Selects all cd children of the current node that have a type attribute with value classic 168493: XML and Web Services (II/2546)
Unabbreviated vs. Abbreviated • The name of the location path is long • Location paths can be expressed using abbreviated syntax • The most important abbreviation is child:: which can be omitted from a location step • Example: cd is short for child::cd 168493: XML and Web Services (II/2546)
Abbreviated Syntax • @ is short for attributes:: Example: cd[@type=“classic”] is short for child::cd[attribute::type=“classic”] • . is short for self::node() Example:: ./cd is short for self::node()/child::cd 168493: XML and Web Services (II/2546)
Abbreviated Syntax (Cont.) • .. is short for parent::node Example: ../cd is short for parent::node()/child::cd • // is short for /descendant-or-self::node()/ Example:: //cd is short for /descendant-or-self::node()/child::cd 168493: XML and Web Services (II/2546)
Examples • cd[@type=“classic”] Selects all cd children of the current node that has a type attribute with value classic • cd[@type=“classic”][5] Selects the fifth cd child of the current node that has a type attribute with value classic 168493: XML and Web Services (II/2546)
Examples • cd[5][@type=“classic”] Selects the fifth cd child of the current node if that child has a type attribute with value classic • cd[@type and @country] Selects all the cd children of the current node that have both a type attribute and a country attribute 168493: XML and Web Services (II/2546)
Node Set Functions • Function count() counts the number of selected elements • Function name() returns name of the element • Function position() returns the position in the node list of the node that is currently being processed 168493: XML and Web Services (II/2546)
String Functions • Function contains() returns true if the 1st argument string contains the 2nd argument string Example: contains(‘XML’, ‘M’) Result: true • Function string-length() returns the number of characters in the string Example: string-length(‘XML’) Result: 3 168493: XML and Web Services (II/2546)
String Functions (Cont.) • Function substring() returns the substring of the first argument starting at the position specified in the 2nd argument with length specified in the 3rd argument Example: substring(‘Students’,4,4) Result: ‘dent’ 168493: XML and Web Services (II/2546)
String Functions (Cont.) • Function starts-with() returns true if the first argument starts with the second argument Example: starts-with(‘XML’, ‘X’) Result: true • Function string() converts the value argument to a string Example: string(314) Result: ‘314’ 168493: XML and Web Services (II/2546)
Number Functions • celing() returns the smallest integer that is not less than the number argument Example: ceiling(3.14) 4 • sum() returns the total value of a set of numeric values in a node-set Example: sum(/cd/price) 168493: XML and Web Services (II/2546)