Presentation is loading. Please wait.

Presentation is loading. Please wait.

XPath Tao Wan March 04, 2002. What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary.

Similar presentations


Presentation on theme: "XPath Tao Wan March 04, 2002. What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary."— Presentation transcript:

1 XPath Tao Wan March 04, 2002

2 What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary purpose: Address ‘part’ of an XML document, and provide basic facilities for manipulation of strings, numbers and booleans.

3 Outline Introduction Introduction Data Model Data Model Xpath Syntax Xpath Syntax Location Path Location Path General Xpath Expressions General Xpath Expressions Core Function Library Core Function Library XPath utilities XPath utilities Conclusion Conclusion

4 Introduction n W3C Recommendation. November 16, 1999 n Latest version: http://www.w3.org/TR/xpath n XPath uses a compact, string-based, rather than XML element-based syntax. n Operates on the abstract, logical structure of an XML document rather than its surface syntax. n Uses a path notation (like in URLs) to navigate through this hierarchical tree structure. Introduction

5 Introduction Cont. Xpath models an XML doc as a tree of nodes and defines a way to compute a string- value for each type of node. Xpath models an XML doc as a tree of nodes and defines a way to compute a string- value for each type of node. Supports Namespaces. Supports Namespaces. Expression (Expr) is the primary syntactic construct of Xpath. Expression (Expr) is the primary syntactic construct of Xpath. Introduction

6 Data Model n The way to represent an XML document. n This tree consists of 7 nodes:  Root Node  Element Nodes  Attribute Nodes  Namespace Nodes  Processing Instruction Nodes  Comment Nodes  Text Nodes n The tree structure is ordered in order of the occurrence of nodes’ start-tag in the XML doc. Data Model

7 Data Model Example IDG books Rick Hull Simon North XML complete 1997 Freeman Jeffrey D. Ullman Principles of Database 1998 IDG books Rick Hull Simon North XML complete 1997 Freeman Jeffrey D. Ullman Principles of Database 1998 </bib> Data Model

8 Xpath Syntax Expression is the primary syntactic construct in XPath Expression is the primary syntactic construct in XPath Evaluated to yield an object of 4 basic types. Evaluated to yield an object of 4 basic types.  node-set (unordered collection of nodes without duplicates).  boolean (true/false)  number (float)  string (sequence of UCS chars) Expression Evaluation occurs will respect to a context. (XSLT/XPointer specified context) Expression Evaluation occurs will respect to a context. (XSLT/XPointer specified context) Location path is one important kind of expression. Location path is one important kind of expression.  Location paths select a set of nodes relative to the context node. Expression

9 Location Path Location Path provides the mechanism for ‘addressing’ parts of an XML doc, similar to file system addressing. Location Path provides the mechanism for ‘addressing’ parts of an XML doc, similar to file system addressing. Ex: /book/year (select all the year elements that have a book parent) Ex: /book/year (select all the year elements that have a book parent) Every location path can be expressed using a straightforward but rather verbose syntax: Every location path can be expressed using a straightforward but rather verbose syntax:  unabbreviated syntax (verbose syntax) Ex: child::* (select all element children of the context node)  abbreviated syntax Ex. * (equivalent to unabbreviation above) Location Path

10 Location Path Cont. Two types of paths: Relative & Absolute Two types of paths: Relative & Absolute  Relative location path: consists of a sequence of one or more location steps separated by /  absolute location path: consists of / optionally followed by a relative location path Composed of a series of steps (1 or more) Composed of a series of steps (1 or more) Location Path Ex. Child::bib/child::book (select the book element children of the bib element children of the context node) Ex. / (select the root node of the document containing the context node)

11 Location Path Examples n Verbose syntax (has syntactic abbreviations for common cases) Examples (unabbreviated) u child::book selects the book element children of the context node node u child::* selects all element children of the context node u attribute::price selects the price attribute of the context node u descendant::book selects all book descendants of the context node u self::book selects the context node if it is a book element (otherwise selects nothing) u child::*/child::book selects all book grandchildren of the context node u / selects the document root (which is always the parent of the document element) parent of the document element) Location Path

12 Location Steps n 3 parts u axis (specifies relationship btwn selected nodes and the context node) u node test (specifies the node type and expanded-name of selected nodes) u predicates ( arbitrary expressions to refine the selected set of nodes ) n The syntax for location step is the axis name and node test separated by a double colon followed by zero or more expressions, each in square bracket. n Evaluate a location step is to generate an initial node-set from axis ( relationship to context node ) and node-test ( node-type and expanded-name ), then filter that node-set by each of the predicates in turn. Location Step ex: child::book[position( )=1] child is the name of the axis, book is the node test, and [position()=1] is a predicate ex: descendant::book[position( )=1] selects the all book element descendants of the context node firstly, then filter the one which is first book descendant of context node.

13 Location Steps Axes Axes  13 axes defined in XPath  Ancestor, ancestor-or-self  Attribute  Child  Descendant, descendant-or-self  Self  Following  Preceding  Following-sibling, preceding-sibling  Namespace  Parent Node test Node test  Identifies type and expanded-name of node.  Can use a name, wildcard or function to evaluate/verify type and name. ex. Child::text() select the text node children of context node. ex. Child::text() select the text node children of context node. Child::book select book element children of context node. Child::book select book element children of context node. Attribute::* select all attribute children of context node. Attribute::* select all attribute children of context node. Location step We’ve only seen these, so far

14 Location Step Cont. Predicate Predicate  A predicate filters a node-set with respect to an axis to produce a new node-set.  Use XPath expressions (normally, boolean expressions) in square brackets following the basis (axis & node test). Ex. Child::book[attribute::price=“25”] Ex. Child::book[attribute::price=“25”] (select all book children of the context node that have a price attribute with value 25. (select all book children of the context node that have a price attribute with value 25.  A predicateExpr is evaluated by evaluating the Expr and converting the result to a boolean (True or False)

15 Examples n Axis and Node Test: descendant::publisher (selects the publisher elements that are descendant of the context node) attributes::* (selects all attributes of the context node) n Basis and Predicate: child::book[3] (selects the 3 rd book of the children of the context node) child::*[self::author or self::year][position()=last()] (selects the last author or year child of the context node) child::book[attribute::page=“400”][5] (selects the fifth book child of the context node that has a page attribute with value 400) Location Path

16 Abbreviated Syntax n Abbreviated syntax is the simpler way to express location path. n For common case, abbreviation can be used to express concisely (not every case). n Each abbreviation can be converted to unabbreviated one. Location Path child:: can be omitted from a location step (child is the default axis) ex. bib/book is equivalent to child::bib/child::book A location step of. is short for self::node() ex:.//book is short for self::node()/descendant-or-self::node()/child::book Location step of.. is short for parent::node() ex.../title is short for parent::node()/child::title // is short for /descendant-or-self::node()/ ex. Book//author is short for book/descendant-or-self::node()/child::author attribute:: can be abbreviated to @ ex. Book[@price=“25”] is short for child::book[attribute::price=“25”]

17 Expressions Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Expressions Function Calls

18 n Function call expression is evaluated by using the FunctionName to identify a function in the expression evaluation context function library. n An argument is converted u to type string (as if calling the string function), u to type boolean (as if calling the Boolean function), u to type number (as if calling the number function), u An argument that is not of type node-set cannot be converted to a node-set. Ex. position() function returns the current node’s position in the context node list as a number. Ex. position() function returns the current node’s position in the context node list as a number. Expressions

19 Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Expressions

20 Node-sets n A location path can be used as an expression. n The expression returns the set of nodes selected by the path. Expressions

21 Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Expressions

22 Booleans n A boolean can only have two values: true or false n The following operators can be used in boolean expressions or combine two boolean expressions according to the usual rules of boolean logic: u or u and u =, != u =, > Ex. Book=‘XML complete’ or book=‘Principles of Database Expressions

23 Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Expressions

24 Numbers n A number represents a floating-point number, no pure integers exist in Xpath. n The basic arithmetic operators include: +, -, *, div and mod. Ex. @id div 10 Expressions

25 Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Expressions

26 Strings n Strings consist of a sequence of zero or more character. n May be enclosed in either single or double quotes. n Comparison operators: =, != Expressions

27 Core Function Library n XPath defines a core set of functions to evaluate expressions. n All implementations of Xpath must implement the core function library. n Four type of functions: u Node Set Functions: operate on or return info about node sets. u String Functions: are used for basic string operations. Ex. substring(“12345”, 0, 3) returns “12” Ex. substring(“12345”, 0, 3) returns “12” u Boolean Functions: all return true or false. u Number Functions: are used for basic number operations. Core Library

28 Xpath Utilities n Miscellaneous utilities related to Xpath n http://www.xmlsoftware.com/xpath/ n XPath Visualiser: u This is a powerful tool for the evaluation of an XPath expression and visual presentation of the resulting node-set. u allowing you to experiment with XPath for finding the correct expression. u The display of the XML source document is similar to the default IE display with the same syntax color and collapsible & expandable container nodes. IE display with the same syntax color and collapsible & expandable container nodes. u very straightforward XPath learning process. Xpath Utilities

29 XPath Visualiser Tree View of XML Doc Xpath input Xpath evaluating result Context Node Result is highlighted Xpath Utilities

30 Conclusion Xpath is complete pattern match language. Xpath is complete pattern match language. Provides an concise way for addressing parts of an XML document. Provides an concise way for addressing parts of an XML document. Base for XSLT, Xpointer and XML Query WG. Supported by W3C. Base for XSLT, Xpointer and XML Query WG. Supported by W3C. Implementing XPath basically requires learning the abbreviated syntax of location path expressions and the functions of the core library. Implementing XPath basically requires learning the abbreviated syntax of location path expressions and the functions of the core library. Conclusion

31 Reference n XML Path Language (XPath) V1.0 http://www.w3.org/TR/xpath http://www.w3.org/TR/xpath n XML in a Nutshell http://www.oreilly.com/catalog/xmlnut/chapter/ http://www.oreilly.com/catalog/xmlnut/chapter/ ch09.html ch09.html n Managing XML and Semistructured Data http://www.cs.washington.edu/homes/suciu/COUR SES/590DS/06xpath.htm n Xpath utilities http://www.xmlsoftware.com/xpath/ http://www.xmlsoftware.com/xpath/ Xpath Reference


Download ppt "XPath Tao Wan March 04, 2002. What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary."

Similar presentations


Ads by Google