Presentation is loading. Please wait.

Presentation is loading. Please wait.

XPath Carissa Mills Jill Kerschbaum. What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common.

Similar presentations


Presentation on theme: "XPath Carissa Mills Jill Kerschbaum. What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common."— Presentation transcript:

1 XPath Carissa Mills Jill Kerschbaum

2 What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common syntax and semantics for functionality shared between XSLT and XPointer. n Primary purpose: Address ‘parts’ of an XML document, and provide basic facilities for manipulation of strings, numbers and booleans. n W3C Recommendation. November 16, 1999 n Latest version: http://www.w3.org/TR/xpath

3 XPath Introduction Introduction Data Model Data Model Expressions Expressions Location Paths Location Paths Core Function Library Core Function Library Conclusion Conclusion

4 Introduction XPath uses a compact, string-based, rather than XML element-based syntax. XPath uses a compact, string-based, rather than XML element-based syntax. Operates on the abstract, logical structure of an XML document (tree of nodes) rather than its surface syntax. Operates on the abstract, logical structure of an XML document (tree of nodes) rather than its surface syntax. Uses a path notation (like URLs) to navigate through this hierarchical tree structure. Uses a path notation (like URLs) to navigate through this hierarchical tree structure. Introduction

5 Introduction Cont. Defines a way to compute a string-value for each type of node: element, attribute, text. Defines a way to compute a string-value for each type of node: element, attribute, text. Supports Namespaces. Supports Namespaces. Name of a node (a pair consisting of a local part and namespace URI). Name of a node (a pair consisting of a local part and namespace URI). Expression (Expr) is the primary syntactic construct. Expression (Expr) is the primary syntactic construct. Introduction

6 Data Model n Treats an XML document as a logical tree n This tree consists of 7 nodes:  Root Node – the root of the document  Element Nodes – one for each element in the document  Unique ID’s  Attribute Nodes  Namespace Nodes  Processing Instruction Nodes  Comment Nodes  Text Nodes n The tree structure is ordered and reads from top to bottom and left to right Data Model

7 Data Model Example For this simple doc: <doc> Some emphasis here. Some emphasis here. Some more stuff. Some more stuff. </doc> Might be represented as: root<doc> text text text text text texttext Data Model

8 Expression Evaluated to yield an object of 4 basic types. Evaluated to yield an object of 4 basic types.  node-set (unordered collection of nodes w/o duplicates).  boolean (true/false)  number (float)  string (sequence of UCS chars) Evaluation occurs with respect to a context. (XSLT/XPointer specified context) Evaluation occurs with respect to a context. (XSLT/XPointer specified context) Parsed by dividing the character string into tokens and then parsing the resulting sequence of tokens. Parsed by dividing the character string into tokens and then parsing the resulting sequence of tokens. Location paths select a set of nodes relative to the context node. Location paths select a set of nodes relative to the context node. Expression

9 Location Paths LocationPath (most important construct) describes a path from 1 point to another. LocationPath (most important construct) describes a path from 1 point to another. u Analogy: Set of street directions. “Second store on the left after the third light” LocationPath provides the mechanism for ‘addressing’ items in an XML doc LocationPath provides the mechanism for ‘addressing’ items in an XML doc Two types of paths: Relative & Absolute Two types of paths: Relative & Absolute Composed of a series of steps (1 or more) and optional predicates Composed of a series of steps (1 or more) and optional predicates LocationPath

10 Location Paths n General syntax: LocationPath::= RelativeLocationPath | AbsoluteLocationPath LocationPath::= RelativeLocationPath | AbsoluteLocationPath n Verbose syntax (has syntactic abbreviations for common cases) Examples (unabbreviated) u child::para selects the para element children of the context node u child::* selects all element children of the context node u attribute::name selects the name attribute of the context node u ancestor::div selects all div ancestors of the context node u self::para selects the context node if it is a para element (otherwise selects nothing) u child::*/child::para selects all para grandchildren of the context node u / selects the document root (which is always the parent of the document element) LocationPath

11 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) u 0 or more predicates (arbitrary expressions to refine the selected set of nodes) n Syntax: Step ::= Axis Specifier NodeTest Predicate* | AbbreviatedStep Axis Specifier ::= AxisName ‘::’ |AbbreviatedAxisSpecifier |AbbreviatedAxisSpecifier ex: child::para[position( )=1] =>child is the name of the axis, para is the node test, and [position()=1] is a predicate n 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. ex:descendant::para =>selects the para element descendants of the context node. =>selects the para element descendants of the context node. LocationPath

12 Location Steps Axes Axes  13 axes defined in XPath  Ancestor, ancestor-or-self  Attribute  Child  Descendant, descendant-or-self  Following  Preceding  Following-sibling, preceding-sibling  Namespace  Parent  Self Node test Node test  Identifies type of node. Evaluates to true/false  Can be a name or function to evaluate/verify type Predicate Predicate  XPath boolean expressions in square brackets following the basis(axis & node test)

13 Examples n Axis and Node Test: child::para selects the para elements that are children of the context node preceding-sibling::para selects the preceding para elements that are siblings to the context node n Basis and Predicate: child::para[3] selects the 3 rd para of the children of the context node child::para[attribute::type=“warning”] selects all para children of the context node that have a type attribute with value warning Para[@type=“warning”][5] selects the fifth para child of the context node that has a type attribute with value warning LocationPath

14 Abbreviated Syntax n child:: can be omitted from a location step. (child is the default axis) div/para is equivalent to child::div/child::para n attribute:: can be abbreviated to @ n // is short for /descendant-or-self::node()/ n A location step of. is short for self::node() ex:.//para is short for self::node()/descendant-or-self::node()/child::para n Location step of.. is short for parent::node() LocationPath

15 Expressions Function Calls Function Calls Node-sets Node-sets Booleans Booleans Numbers Numbers Strings Strings Lexical Structure Lexical Structure Expressions

16 Basics n A VariableReference evaluates to the value to which the variable name is bound in the set of variable bindings in the context. It is an error if the variable name is not bound to any value in the set of variable bindings in the expression context. n Parentheses may be used for grouping Expr::= OrExpr PrimaryExpr::= VariableReference | ’(‘ Expr ‘)’ | Literal | Number | FunctionCall

17 Function Calls n Function call expression is evaluated by using the FunctionName to identify a function in the expression evaluation context function library, evaluating each of the Arguments, converting each argument tot he type required by the function, and finally calling the function, passing it the converted arguments. Error, if the number of args is wrong or if an arg cannot be converted to the required type. The result of the FunctionCall expression is the result returned by the function. n An argument is converted to type string (as if calling the string function), converted to type boolean....converted to type number... n An argument that is not of type node-set cannot be converted to a node-set. n FunctionCall ::= FunctionName ‘(‘(Argument (‘,’ Argument)*)?’)’ Argument ::= Expr

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

19 Booleans n A boolean can only have two values: true or false n The following expressions can be evaluated: u or u and u =, != u =, > n Special rules apply when comparing node-sets as booleans

20 Numbers n A number represents a floating-point number n The numeric operators convert their operands to numbers n Operators include: u +, -, *, /, %, and union

21 Strings n Strings consist of a sequence of zero or more character n A character is defined in the XML Recommendation

22 Lexical Structure n When tokenizing, the longest possible token is returned

23 Core Function Library n XPath defines a core set of functions and operators n All implementations of Xpath must implement the core function library n Node Set Functions list/item[position() mod2 = 1] selects all odd number element of a list id)(“foo”)/child::para[position()=5] selects the 5 th para child of the element with the unique ID foo n String Functions substring(“12345”, 0, 3) returns “12” n Boolean Functions boolean true() returns “true” n Number Functions number sum(node-set) returns the sum of the nodes Core Library

24 Conclusion XPath provides a concise and intuitive way to address into XML documents XPath provides a concise and intuitive way to address into XML documents Standard part of the XSLT and XPointer specifications Standard part of the XSLT and XPointer specifications 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


Download ppt "XPath Carissa Mills Jill Kerschbaum. What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common."

Similar presentations


Ads by Google