XPath

Uploaded from authorPOINTLite
Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

By: gayu01 (18 month(s) ago)

really informative ppt. i wish this to download

By: thegeniuz (21 month(s) ago)

good ppt

By: lovedon1947 (42 month(s) ago)

goooood ppt

Presentation Transcript

Slide1: 

An Introduction to XPath Nick Davies Tuesday 20th January 2004

Slide2: 

XPath : Introduction XPath is a W3C recommendation (1999). According to W3C, it is a “language for addressing parts of an XML document It can be thought of as query language, like SQL, however it operates on XML documents, not databases, by specifying path expressions in order to identify nodes in the document XPath models XML documents as trees of nodes using the Document Object Model (DOM), text order is maintained in the tree XPath's primary intention was that of a component that can be used by other specifications (such as XPointer and XSLT)

Slide3: 

XPath : Syntax XPath uses path expressions to locate nodes with XML documents Here is a simple XML document, representing a DVD collection. <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide4: 

XPath : Syntax We can now describe the XPath syntax with examples from the previous XML document Locating nodes /collection/dvd/cert <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide5: 

XPath : Syntax We can now describe the XPath syntax with examples from the previous XML document Select Unknown Values using wildcards (*) /collection/dvd/* <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide6: 

XPath : Syntax We can now describe the XPath syntax with examples from the previous XML document Selecting Branches using square brackets /collection/dvd[1] /collection/dvd[last()] <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide7: 

XPath : Syntax We can now describe the XPath syntax with examples from the previous XML document Select Several Paths using the | operator /collection/dvd/genre | /collection/dvd/cert <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide8: 

XPath : Syntax We can now describe the XPath syntax with examples from the previous XML document Selecting Attributes using the @ prefix //dvd[@title=‘Fawlty Towers’] <?xml version=“1.0” encoding=“ISO-8859-1”?> <collection> <dvd title=“Fawlty Towers”> <genre>comedy</genre> <year>1975</year> <cert>PG</cert> </dvd> <dvd title=“Quadrophenia”> <genre>cult</genre> <year>1979</year> <length>114</length> </dvd> <dvd title=“Goldfinger”> <year>1964</year> <cert>PG</cert> <length>105</length> </dvd> <collection>

Slide9: 

XPath : Location Paths One of the most important XPath expressions is the location path. A location path expression results in a node-set. Each location path consists of one or more location steps. Each location step has ; an axis a node test one or more predicates (optional) Location Step Syntax and Example axisname::nodetest[predicate] child::dvd[genre=comedy]

Slide10: 

XPath : Location Paths : Axes There are several axes in XPath; each of which selects a different subset of the nodes in the document, depending on the context node. They are Ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self, following, following-sibling, namespace, parent, preceding, preceding-sibling, self. collection dvd dvd dvd year cert time The child axis with context node collection The preceding-sibling axis with context node time collection dvd dvd dvd year cert time

Slide11: 

XPath : Location Paths : Node Tests Node Test Determines what kinds of nodes be selected along a given axis. The node test is applied to each node in the axis If the test succeeds the node is kept, if not the node is disregarded There are 7 types of node : Example Location Paths root nodes child::foo - is there a foo child element? element nodes parent::text() - is the parent a text node? text nodes descendant::comment() - is there a comment attribute nodes node below? namespace nodes processing instruction nodes comment nodes

Slide12: 

XPath : Location Paths : Predicates Predicates The predicate is a further test to retain or eliminate nodes. It filters the node set into a new node-set. Each node is evaluated in turn and if a predicate is true for a given node, it is kept in the new node set, if false it is removed. Consists of a well-formed expressions consisting of boolean operators functions numbers, strings comparison operators Examples : child::dvd[genre=comedy] descendant::dvd[position()=2] descendant::collection[attribute::type=‘title’]

Slide13: 

XPath : Expressions Expressions describes a set of nodes in a documents. We’ve already seen one : a location path. Path expressions may be absolute or relative: absolute begin from the root node /collection/dvd/length relative begin from the context node dvd/genre XPath supports four types of expressions Numerical: + - * div mod Equality: = != Relational: < > <= >= Boolean: or and

Slide14: 

XPath : Functions XPath defines a number of useful functions for converting and translating data. Some take arguments, some don’t. Those that don’t, operate on the context node. There are 25+ functions. Here are only a few examples: Node Set Functions count() – returns the number of nodes in a node set String Functions concat() – returns the concatenations of all its arguments contains() – returns true if the second of 2 given strings is in the first Number Functions sum() – returns the total value of a set of numeric values in a node-set Boolean Functions not() – returns true if the argument is false, and false if the arguments is true

Slide15: 

XPath : Summary To sum up: XPath is a straightforward declarative language for selecting particular subsets of nodes from an XML document XPath has a syntax for the selection of nodes, unknown elements, branches, multiple paths and attributes XPath queries are written in expressions which can returns doubles or strings and allow for arithmetic and relational operations on data types Location paths are an important subset of expressions, they consist of one or more location step which in turn consists of an axis, a node test, and (optionally) one or more predicates XPath also provides a core set of functions for operating on the data types

Slide16: 

Thanks For Your Time Any Questions?