16
XPath Carissa Mills Carissa Mills Jill Kerschbaum Jill Kerschbaum

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

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

XPathXPath

Carissa MillsCarissa Mills

Jill KerschbaumJill Kerschbaum

Page 2: 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

What is XPath?What is XPath?

A language designed to be used by both XSL A language designed to be used by both XSL Transformations (XSLT) and XPointer.Transformations (XSLT) and XPointer.

Provides common syntax and semantics for Provides common syntax and semantics for functionality shared between XSLT and XPointer.functionality shared between XSLT and XPointer.

Primary purpose: Address ‘parts’ of an XML Primary purpose: Address ‘parts’ of an XML document, and provide basic facilities for document, and provide basic facilities for manipulation of strings, numbers and booleans.manipulation of strings, numbers and booleans.

W3C Recommendation. November 16, 1999W3C Recommendation. November 16, 1999 Latest version: http://www.w3.org/TR/xpathLatest version: http://www.w3.org/TR/xpath

Page 3: 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

XPathXPath

IntroductionIntroduction Data ModelData Model ExpressionsExpressions Location PathsLocation Paths Core Function LibraryCore Function Library ConclusionConclusion

Page 4: 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

IntroductionIntroduction

XPath uses a compact, string-based, rather XPath uses a compact, string-based, rather than XML element-based syntax.than XML element-based syntax.

Operates on the abstract, logical structure of Operates on the abstract, logical structure of an XML document (tree of nodes) rather an XML document (tree of nodes) rather than its surface syntax.than its surface syntax.

Uses a path notation (like URLs) to Uses a path notation (like URLs) to navigate through this hierarchical tree navigate through this hierarchical tree structure.structure.

Introduction

Page 5: 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

Introduction Cont.Introduction Cont.

Defines a way to compute a string-value for Defines a way to compute a string-value for each type of node: element, attribute, text.each type of node: element, attribute, text.

Supports Namespaces.Supports Namespaces. Name of a node (a pair consisting of a local Name of a node (a pair consisting of a local

part and namespace URI).part and namespace URI). Expression (Expr) is the primary syntactic Expression (Expr) is the primary syntactic

construct.construct.

Introduction

Page 6: 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

Data ModelData Model Treats an XML document as a logical treeTreats an XML document as a logical tree This tree consists of 7 nodes:This tree consists of 7 nodes:

Root Node – the root of the documentRoot Node – the root of the document Element Nodes – one for each element in the documentElement Nodes – one for each element in the document

Unique ID’sUnique ID’s Attribute NodesAttribute Nodes Namespace NodesNamespace Nodes Processing Instruction NodesProcessing Instruction Nodes Comment NodesComment Nodes Text NodesText Nodes

The tree structure is ordered and reads from top to The tree structure is ordered and reads from top to bottom and left to rightbottom and left to right

Data Model

Page 7: 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

Data Model ExampleData Model ExampleFor this simple doc:For this simple doc:

<doc><doc><?Pub Caret?><?Pub Caret?><para>Some <em>emphasis</em> here. </para><para>Some <em>emphasis</em> here. </para><para>Some more stuff.</para><para>Some more stuff.</para></doc></doc>

Might be represented as:Might be represented as:rootroot

<doc><doc>

<?pi?> <?pi?> <para> <para> <para><para>

text <em> text <em> text text texttext

texttext

Data Model

Page 8: 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

ExpressionExpression Evaluated to yield an object of 4 basic types.Evaluated to yield an object of 4 basic types.

node-set node-set (unordered collection of nodes w/o duplicates).(unordered collection of nodes w/o duplicates). booleanboolean (true/false) (true/false) number number (float)(float) string string (sequence of UCS chars)(sequence of UCS chars)

Evaluation occurs with respect to a context. Evaluation occurs with respect to a context. (XSLT/XPointer specified context) (XSLT/XPointer specified context)

Parsed by dividing the character string into tokens and then Parsed by dividing the character string into tokens and then parsing the resulting sequence of tokens.parsing the resulting sequence of tokens.

Location paths select a Location paths select a set of nodes set of nodes relative to the relative to the contextcontext node.node.

Expression

Page 9: 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

Location PathsLocation Paths

LocationPath (most important construct) describes a path LocationPath (most important construct) describes a path from 1 point to another. from 1 point to another. Analogy: Set of street directions.Analogy: Set of street directions.

““Second store on the left after the third light”Second store on the left after the third light”

LocationPath provides the mechanism for ‘addressing’ LocationPath provides the mechanism for ‘addressing’ items in an XML docitems in an XML doc

Two types of paths: Relative & AbsoluteTwo types of paths: Relative & Absolute Composed of a series of Composed of a series of stepssteps (1 or more) and optional (1 or more) and optional predicatespredicates

LocationPath

Page 10: 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

Location PathsLocation Paths

General syntax:General syntax: LocationPathLocationPath ::= RelativeLocationPath::= RelativeLocationPath

| AbsoluteLocationPath | AbsoluteLocationPath Verbose syntax (has syntactic abbreviations for common cases)Verbose syntax (has syntactic abbreviations for common cases)

Examples (unabbreviated)Examples (unabbreviated) child::para child::para selects the selects the parapara element children of the context node element children of the context node child::* child::* selects all element children of the context nodeselects all element children of the context node attribute::name attribute::name selects the selects the namename attribute of the context node attribute of the context node ancestor::div ancestor::div selects all selects all divdiv ancestors of the context node ancestors of the context node self::para self::para selects the context node if it is a selects the context node if it is a parapara element element

(otherwise selects nothing)(otherwise selects nothing) child::*/child::para child::*/child::para selects all para grandchildren of the context nodeselects all para grandchildren of the context node / / selects the document root selects the document root

(which is always the parent of the document element)(which is always the parent of the document element)

LocationPath

Page 11: 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

Location StepsLocation Steps 3 parts3 parts

axis axis (specifies relationship btwn selected nodes and the context node)(specifies relationship btwn selected nodes and the context node) node test node test (specifies the node type and expanded-name)(specifies the node type and expanded-name) 0 or more0 or more predicates predicates (arbitrary expressions to refine the selected set of nodes)(arbitrary expressions to refine the selected set of nodes)

Syntax: Syntax: Step Step ::= Axis Specifier NodeTest Predicate*::= Axis Specifier NodeTest Predicate*

| AbbreviatedStep| AbbreviatedStep

Axis Specifier Axis Specifier ::= AxisName ‘::’::= AxisName ‘::’ |AbbreviatedAxisSpecifier|AbbreviatedAxisSpecifier

ex: child::para[position( )=1]ex: child::para[position( )=1]=>child is the name of the axis, para is the node test, and [position()=1] is a predicate=>child is the name of the axis, para is the node test, and [position()=1] is a predicate

Generate an initial node-set from axis Generate an initial node-set from axis (relationship to context node) (relationship to context node) and node-test and node-test (node-type and expanded-name), (node-type and expanded-name), then filter that node-set by each of the predicates.then filter that node-set by each of the predicates.

ex:ex: descendant::paradescendant::para =>selects the para element descendants of the context node.=>selects the para element descendants of the context node.

LocationPath

Page 12: 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

Location StepsLocation Steps AxesAxes

13 axes defined in XPath13 axes defined in XPath Ancestor, ancestor-or-selfAncestor, ancestor-or-self AttributeAttribute ChildChild Descendant, descendant-or-selfDescendant, descendant-or-self FollowingFollowing PrecedingPreceding Following-sibling, preceding-siblingFollowing-sibling, preceding-sibling NamespaceNamespace ParentParent SelfSelf

Node testNode test Identifies type of node. Evaluates to true/falseIdentifies type of node. Evaluates to true/false Can be a name or function to evaluate/verify typeCan be a name or function to evaluate/verify type

PredicatePredicate XPath boolean expressions in square brackets following the XPath boolean expressions in square brackets following the

basis(axis & node test)basis(axis & node test)

Page 13: 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

ExamplesExamples

Axis and Node Test:Axis and Node Test:child::para child::para selects the selects the parapara elements that are children of the context node elements that are children of the context node

preceding-sibling::parapreceding-sibling::para selects the preceding selects the preceding parapara elements that are elements that are siblings to the context nodesiblings to the context node

Basis and Predicate:Basis and Predicate:child::para[3] child::para[3] selects the 3selects the 3rdrd para para of the children of the context node of the children of the context node

child::para[attribute::type=“warning”]child::para[attribute::type=“warning”] selects all selects all parapara children children of the context node that have a type of the context node that have a type attributeattribute with value with value warningwarning

Para[@type=“warning”][5] Para[@type=“warning”][5] selects the fifth selects the fifth parapara child of the child of the context node that has a type context node that has a type attributattribute with value e with value warningwarning

LocationPath

Page 14: 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

Abbreviated SyntaxAbbreviated Syntax

child:: can be omitted from a location step.child:: can be omitted from a location step.(child is the default axis)(child is the default axis)div/para is equivalent to child::div/child::paradiv/para is equivalent to child::div/child::para

attribute:: can be abbreviated to @attribute:: can be abbreviated to @ // is short for /descendant-or-self::node()/// is short for /descendant-or-self::node()/ A location step of . is short for self::node()A location step of . is short for self::node()

ex: .//para is short for ex: .//para is short for self::node()/descendant-or-self::node()/child::paraself::node()/descendant-or-self::node()/child::para

Location step of .. is short for parent::node()Location step of .. is short for parent::node()

LocationPath

Page 15: 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

Core Function LibraryCore Function Library XPath defines a core set of functions and operatorsXPath defines a core set of functions and operators All implementations of Xpath must implement the All implementations of Xpath must implement the

core function librarycore function library Node Set FunctionsNode Set Functions

list/item[position() mod2 = 1]list/item[position() mod2 = 1] selects all odd number element of a listselects all odd number element of a list id)(“foo”)/child::para[position()=5]id)(“foo”)/child::para[position()=5] selects the 5selects the 5thth para child of the para child of the

element with the unique ID fooelement with the unique ID foo

String FunctionsString Functionssubstring(“12345”, 0, 3) returns “12”substring(“12345”, 0, 3) returns “12”

Boolean FunctionsBoolean Functionsbooleanboolean true() returns “true” true() returns “true”

Number FunctionsNumber Functionsnumbernumber sum(node-set) returns the sum of the nodes sum(node-set) returns the sum of the nodes

Core Library

Page 16: 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

ConclusionConclusion

XPath provides a concise and intuitive way XPath provides a concise and intuitive way to address into XML documentsto address into XML documents

Standard part of the XSLT and XPointer Standard part of the XSLT and XPointer specificationsspecifications

Implementing XPath basically requires Implementing XPath basically requires learning the abbreviated syntax of location learning the abbreviated syntax of location path expressions and the functions of the path expressions and the functions of the core librarycore library

Conclusion