90
Introduction to Xpath

Introduction to Xpath

  • Upload
    cicada

  • View
    48

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to Xpath. Sources. XML Path Language (XPath) Version 1.0, http://www.w3.org/TR/xpath http://www.w3schools.com/xpath/xpath_examples.asp Essential XML Quick Reference (A. Skonnard and M. Gudgin) http://www.w3schools.com/xpath XML In a Nutshell, O’Reilly, Harold & Means. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Xpath

Introduction to Xpath

Page 2: Introduction to Xpath

Sources

• XML Path Language (XPath)Version 1.0, http://www.w3.org/TR/xpath

• http://www.w3schools.com/xpath/xpath_examples.asp

• Essential XML Quick Reference (A. Skonnard and M. Gudgin)

• http://www.w3schools.com/xpath• XML In a Nutshell, O’Reilly, Harold & Means

Page 3: Introduction to Xpath

Xpath 1.0

• Examples • Data Model• Syntax• Location paths• Expressions• Functions• Data Model for Xpath 2.0 and Xquery

1.0

Page 4: Introduction to Xpath

Xpath ExamplesA CD catalog with entries such as:<cd>

<title>Hide your heart</title><artist>Bonnie Tyler</artist><country>UK</country><company>CBS Records</company><price>9.90</price><year>1988</year>

</cd>

Page 5: Introduction to Xpath

/catalog/cd : selects all the cd nodes

<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><country>USA</country><company>Columbia</company><price>10.90</price><year>1985</year>

</cd><cd>

<title>Hide your heart</title><artist>Bonnie Tyler</artist><country>UK</country><company>CBS Records</company><price>9.90</price><year>1988</year>

</cd>…….File at www.cs.technion.ac.il/~oshmu/cd.xml

Page 6: Introduction to Xpath

/catalog/cd[1] : selects the first cd node

<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><country>USA</country><company>Columbia</company><price>10.90</price><year>1985</year>

</cd>

Page 7: Introduction to Xpath

/catalog/cd/price : selects price nodes

<price<10.90</price<<price<9.90</price<<price<9.90</price<

<price<10.20</price<<price<9.90</price<

<price<10.90</price<<price<8.10</price<<price<8.50</price<

<price<10.80</price<<price<8.70</price<

<price<10.90</price<<price<10.20</price<

<price<8.70</price<

<price<9.90</price<<price<8.20</price<<price<7.90</price<

<price<8.90</price<<price<8.90</price<<price<7.90</price<<price<8.90</price<<price<7.80</price<<price<9.90</price <

<price<7.90</price<<price<7.20</price<<price<7.80</price<<price<8.20</price<

Page 8: Introduction to Xpath

/catalog/cd/price/text(): selects price text nodes

10.909.909.9010.209.9010.908.108.5010.808.7010.9010.208.709.908.20

8.709.908.20

7.908.908.907.908.907.809.907.907.207.808.20

Page 9: Introduction to Xpath

/catalog/cd [price < 7.80] : selects cd nodes whose price text value is less than 7.80

<cd<

< title<Picture book</title<

< artist<Simply Red</artist<

< country<EU</country<

< company<Elektra</company<

< price<7.20</price<

< year<1985</year<

/<cd<

Page 10: Introduction to Xpath

/catalog/cd [price < 7.80]/ price : selects price nodes whose text value is less than 7.80

<price<7.20</price<

Page 11: Introduction to Xpath

/catalog/cd [price < 7.80]/ price/text() : selects text nodes within price nodes whose text value is less than 7.80

7.20

Page 12: Introduction to Xpath

Semantics of Location Paths

• A relative location path consists of a sequence of one or more location steps separated by /.

• The steps in a relative location path are composed together from left to right.

• Each step in turn selects a set of nodes relative to a context node.

• An initial sequence of steps is composed together with a following step as follows. – The initial sequence of steps selects a set of nodes relative to a

context node. – Each node in that set is used as a context node for the following

step. – The sets of nodes identified by that step are unioned together. The

set of nodes identified by the composition of the steps is this union.

Page 13: Introduction to Xpath

Semantics of Location Paths

• An absolute location path consists of / optionally followed by a relative location path.

• A / by itself selects the root node of the document containing the context node.

• If it is followed by a relative location path, then the location path selects:– the set of nodes that would be selected by the relative

location path relative to the root node of the document containing the context node.

Page 14: Introduction to Xpath

Concepts

Page 15: Introduction to Xpath

Data Model

• A formalism for internal representation of an XML document.

• Later on we’ll see the more general data model for Xpath 2.0 and Xquery 1.0.

• Applies after resolving entities and CDATA sections.

• The data model instance, conceptually, is the object to which Xpath queries are applied.

• The data model is a tree made out of nodes and edges between them.

Page 16: Introduction to Xpath

Kinds of Nodes: Root

• Unique root of the tree– Has comment children nodes, one per comment– Has processing instruction nodes, one per PI– One child node for the document element– No information regarding: XML declaration,

DTD, whitespace before or after the document element

– Has no parent node– Its value is that of the document element

Page 17: Introduction to Xpath

Kinds of Nodes: Element

• Represents an element in the document– Has a name– Has a namespace URI– Has a parent node and a list of child nodes– Children may be: other element nodes, comment

nodes, PI nodes, text nodes– Has a list of attributes – Not considered as children– Has a list of namespaces - Not considered as children– Value = text, after entities are resolved, appearing

between the start and end tags of the element, after PIs, comments and tags are removed.

Page 18: Introduction to Xpath

Kinds of Nodes: Attribute

• Represents an attribute in the document– Has a name– Value = normalized attribute value– Has a namespace URI– Has a value– Has a parent node and NO child nodes– Is NOT considered a child of its parent– xlmns and xlmns:prefix attributes are NOT

represented as attribute nodes

Page 19: Introduction to Xpath

Kinds of Nodes: Text

• Represents max contiguous text between tags, PIs and comments– Has a parent node– Has no child nodes– Value = text in node

Page 20: Introduction to Xpath

Kinds of Nodes: Namespace

• Represents a namespace in whose scope the element lies– Has name = prefix– Has value = namespace URI– One xlmns or xlmns:prefix declaration may give

rise to MULTIPLE namespace nodes– Has a parent node– Is NOT considered a child of its parent

Page 21: Introduction to Xpath

Kinds of Nodes: PI

• Represents a PI– Has a target– Has name = target – Has data– Has value = data minus initial whitespace– Has a parent node– Has no children– IS considered a child of its parent

Page 22: Introduction to Xpath

Kinds of Nodes: Comment

• Represents a comment– Has a target– Has a parent node– Has value = string content of comment without

<! - - and - -<– Has no children– IS considered a child of its parent

Page 23: Introduction to Xpath

Xpath Data Types

• Boolean: true, false

• Number: floating point

• String: sequence of characters

• Node-sets: node collection, no duplicates

• Document order: the order in which start-tags appear (DFS on the tree)

Page 24: Introduction to Xpath

Example File<?xml version="1.0" encoding="ISO-8859-1"?< <catalog< <cd country="USA"< <title<Empire Burlesque</title< <artist<Bob Dylan</artist< <price<10.90</price< </cd< <cd country="UK"< <title<Hide your heart</title< <artist<Bonnie Tyler</artist< <musthave< yes</musthave< <price<9.90</price< </cd< <cd country="USA"< <title<Greatest Hits</title< <artist<Dolly Parton</artist< <scratch< yes</scratch< <price<9.90</price< </cd< </catalog<

Page 25: Introduction to Xpath

Navigation• Generally the syntax is either of the form /location

step/…/location step or location step/…/location step

• If path starts with / then it matches the root document node (absolute path)

• Otherwise, it is a relative path that matches the context node

• With each step there is an associated set of context nodes

• For each node in this set the next step is evaluated• The union of the resulting sets forms the next

context set (how is this union done?)

Page 26: Introduction to Xpath

Navigation Example

• Select all the price elements of all the cd elements of the catalog element:

• /catalog/cd/price– <price<10.90</price<– <price<9.90</price<– <price<9.90</price<

• In fact, in its full version this is:• /child::catalog/child::cd/child::price

Page 27: Introduction to Xpath

Navigation – Union

• Select all the price or title elements of all the cd elements of the catalog element:

• /catalog/cd/price | /catalog/cd/title– <title<Empire Burlesque</title< – <price<10.90</price< </cd< – <title<Hide your heart</title< – <price<9.90</price< </cd< – <title<Greatest Hits</title<<price<9.90</price< </cd<

• The result order is document order (always, for union)• cd/price identifies the child price elements of the context

node’s cd child

Page 28: Introduction to Xpath

Axes

• A location step is of the form – axis::nodetest [ ] … [ ] where each [ ] denotes

a predicate, zero or more predicates• Axis can be: self, child, parent, descendant,

descendant-or-self, ancestor, ancestor-or-self, following, following-sibling, preceding, preceding-sibling, attribute, namespace

• An axis is either forward (e.g., descendant) or backward (e.g., ancestor)

Page 29: Introduction to Xpath

Axes (Cont.)

• Each axis has a principal node type• When identifying nodes via * or via name, only

nodes of the principal type are candidates• The attribute axis has principal node type of

Attribute• The namespace axis has principal node type of

Namespace• All other axes have principal node type of Element

Page 30: Introduction to Xpath

self

• Identifies the context node

• /catalog/cd/self::cd

• Same as:

• /catalog/cd

Page 31: Introduction to Xpath

child

• Identifies the child nodes of the context node • Default axis• /catalog/cd• Same as:• /catalog/child::cd• Same as:• /child::catalog/child::cd

Page 32: Introduction to Xpath

parent

• Identifies the parent node of the context node

• /catalog/cd/parent::catalog

• Same as:• /catalog/cd/parent::catalog/cd/parent::catalog

Page 33: Introduction to Xpath

descendant and descendant-or-self

• Identifies the descendant nodes of the context node

• /catalog/descendant::title– <title<Empire Burlesque</title<– <title<Hide your heart</title< – <title<Greatest Hits</title<

• /catalog/descendant-or-self::title returns the same result

• /catalog/descendant-or-self::catalog returns the catalog node

Page 34: Introduction to Xpath

ancestor and ancestor-or-self

• Identifies the ancestor nodes of the context node • /catalog/descendant::title/ancestor::cd returns the three cd

nodes• /catalog/descendant::title/ancestor::catalog returns the

catalog node• /catalog/descendant::title/ancestor-or-self::title returns the

three title nodes, in reverse document order (?)– <title<Greatest Hits</title< – <title<Hide your heart</title< – <title<Empire Burlesque</title<

• /catalog/descendant::title/ancestor-or-self::cd returns the three cd nodes, again - in reverse document order (?)

Page 35: Introduction to Xpath

following

• Identifies the nodes, except for descendant nodes, attribute nodes and namespace nodes, which follow the context node in document order

• /catalog/descendant::scratch/following::* returns <price<9.90</price<

• /catalog/descendant::scratch/parent::cd/child::title/following::* returns – <artist<Dolly Parton</artist< – <scratch< yes</scratch<– <price<9.90</price<

Page 36: Introduction to Xpath

following-sibling

• /catalog/cd/following-sibling returns<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<

<scratch< yes</scratch<

<price<9.90</price< </cd<

Page 37: Introduction to Xpath

preceding

• Identifies the nodes, except for ancestor nodes, attribute nodes and namespace nodes, which precede the context node in document order

• /catalog/descendant::musthave/preceding::* returns (note the reverse document order)

– <artist<Bonnie Tyler</artist<– <title<Hide your heart</title<– <price<10.90</price< </cd<– <artist<Bob Dylan</artist<– <title<Empire Burlesque</title< – <cd country="USA"< <title<Empire Burlesque</title< <artist<Bob Dylan</artist< <price<10.90</price< </cd<

Page 38: Introduction to Xpath

preceding-sibling

• /catalog/descendant::musthave/preceding-sibling::*– <artist<Bonnie Tyler</artist<

– <title<Hide your heart</title<

Page 39: Introduction to Xpath

attribute

• Identifies the attributes of the context node.

• /catalog/cd/attribute::* returns– country="USA"– country="UK"– country="USA"

Page 40: Introduction to Xpath

namespace

• Identifies the namespace nodes of the context node.

Page 41: Introduction to Xpath

Node Tests

• Node Test by name

• Node Test by type

Page 42: Introduction to Xpath

Node Test by name

• Need to establish namespace bindings for the Xpath processor (various possibilities)

• If prefix:local name is used then a matching node must have the same namespace as that bound to the prefix

• If a name test does not include a prefix, the identified nodes should belong to no namespace (no defaults here)

Page 43: Introduction to Xpath

Node Test by name - Examples

• Suppose prefix j is bound to namespace urn:eorg:invoice

• Then, child::j:item identifies child item element nodes of the context node in the namespace urn:eorg:invoice

• child::j:* identifies child element nodes of the context node in the namespace urn:eorg:invoice

• /child::catalog identifies child catalog element nodes of the root that belong to no namespace

Page 44: Introduction to Xpath

Node Test by type

• text() : node is a text node

• comment() : node is a comment node

• processing-instruction(target?)

• node()

Page 45: Introduction to Xpath

Node Test by type - Examples• child::node() identifies all child nodes of the context

node regardless of type• //scratch/child::text() returns text node yes• //scratch/text() also returns text node yes• //cd/price/text() returns 3 text nodes 10.90 9.90 9.90• /catalog/comment() identifies comment child nodes

of the root’s catalog child element• /processing-instruction(‘xsl-stylesheet’) identifies

processing instruction child nodes of the document node that has target equal to xsl-stylesheet

Page 46: Introduction to Xpath

Shorthand notationLong formShort form

child::

attribute::@

self::node().

parent::node()..

/descendant-or-self

::node()/

//

[position() = number][number]

Page 47: Introduction to Xpath

Shorthand notation - Examples

Long formShort form

/child::catalog/child::cd/catalog/cd

/child::catalog/attribute::country/catalog/@country

/self::node()/descendant-or-self::node()/child::title

/.//title (how about //title ?)

/descendant-or-self::node/scratch/ parent::node()

//scratch/..

Page 48: Introduction to Xpath

Predicates

• Zero or more predicates appear, each in square brackets, following the node test

• A predicate may contain any expression; the result is coerced to Boolean

• Each predicate is applied to each of the resulting nodes after the node test

• If any evaluates to false, the node is eliminated• Otherwise, all tests are true, the node stays as a

member of the node set

Page 49: Introduction to Xpath

Predicates - Examples<catalog< <cd country="USA"< <title<Empire Burlesque</title< <artist<Bob Dylan</artist< <price<10.90</price< </cd< ….

• /catalog/cd [ artist = “Bob Dylan”] returns <cd country="USA"< <title<Empire Burlesque</title< <artist<Bob Dylan</artist< <price<10.90</price< </cd<

Page 50: Introduction to Xpath

Predicates - Examples• /catalog/cd [ position() = 1] returns <cd country="USA"< <title<Empire Burlesque</title< <artist<Bob Dylan</artist< <price<10.90</price< </cd<

• //cd[ price < 9.90 ] returns the same• /catalog/cd [1] returns the same• /catalog[cd / scratch]/ cd[2] returns the second cd

element; the predicate is satisfied because of the 3rd cd element

Page 51: Introduction to Xpath

Expressions

• Boolean Expressions

• Equality expressions

• Relational Expressions

• Numerical expressions

Page 52: Introduction to Xpath

Boolean Expressions

• The operands are and, not and or• Each operand is evaluated and converted to boolean

(similar to applying boolean())• /catalog/cd/scratch or /catalog/@country returns true• /catalog/cd [scratch and price] returns <cd country="USA"<

<title<Greatest Hits</title<<artist<Dolly Parton</artist<<scratch< yes</scratch<<price<9.90</price<

</cd<

Page 53: Introduction to Xpath

Boolean Expressions

• /catalog/cd [not ( scratch and price )] returns <cd country="USA"<

<title<Empire Burlesque</title<<artist<Bob Dylan</artist<<price<10.90</price<

</cd<<cd country="UK"<

<title<Hide your heart</title<<artist<Bonnie Tyler</artist<<musthave< yes</musthave<<price<9.90</price<

</cd<

• /catalog/cd [(price < 9) or (price < 11)] returns an empty node set

Page 54: Introduction to Xpath

Equality expressions: =, !=• Equality between objects holds when they are

equal• Equality between node sets holds when there are

elements in each that have the same string value, so there is an implicit existential quantifier

• Inequality between node sets holds when there are elements in each that have different string values

• So, two node sets may be equal and unequal at the same time

• When compared to a number (resp., string, boolean), the string value is converted to a number (resp., string, boolean)

Page 55: Introduction to Xpath

Equality expressions: Examples• price = 9.90 true if at least one child price

element has string value that when converted to a number equals 9.90

• price != 9.90 true if at least one child price element has string value that when converted to a number does not equal 9.90 what if there are no price children?

• not (price = 9.90) true if there is no price element such that when converted to a number has string value of 9.90 what if there are no price children?

Page 56: Introduction to Xpath

Equality expressions: Examples• not (price != 9.90) true if there is no price

element such that when converted to a number has string value that is unequal to 9.90, in other words, all price elements are such that when their string values are converted to a number, it’s 9.90, what if there are no price children?

• @country = ‘USA’ true for elements that have the value USA for their country attribute

Page 57: Introduction to Xpath

Equality expressions: Examples• //cd [scratch = " yes"] returns <cd country="USA"<

<title<Greatest Hits</title<<artist<Dolly Parton</artist<<scratch< yes</scratch<<price<9.90</price<

</cd<• //cd [scratch != " yes"] returns an empty node set (“there exists a child of cd whose

string value is different than “ yes”)• //cd [not ( scratch = " yes") ] returns (“it is not the case that there exists a child of cd

whose string value is “ yes”) <cd country="USA"<

<title<Empire Burlesque</title<<artist<Bob Dylan</artist<<price<10.90</price<

</cd< <cd country="UK"<

<title<Hide your heart</title<<artist<Bonnie Tyler</artist<<musthave< yes</musthave<<price<9.90</price<

</cd<

Page 58: Introduction to Xpath

Equality expressions: Examples• //catalog [cd [not (price = 9.90)] ] returns <catalog<

<cd country="USA"<<title<Empire Burlesque</title<<artist<Bob Dylan</artist<<price<10.90</price<

</cd<<cd country="UK"<

<title<Hide your heart</title<<artist<Bonnie Tyler</artist<<musthave< yes</musthave<<price<9.90</price<

</cd<<cd country="USA"<

<title<Greatest Hits</title<<artist<Dolly Parton</artist<<scratch< yes</scratch<<price<9.90</price<

</cd<</catalog<• //catalog [not (cd [price = 9.90] ) ] returns an empty node set

Page 59: Introduction to Xpath

Equality expressions: Examples• //cd [ not ( not ( scratch) ) ] returns

<cd country="USA"<

<title<Greatest Hits</title<

<artist<Dolly Parton</artist<

<scratch< yes</scratch<

<price<9.90</price<

</cd<

Page 60: Introduction to Xpath

Coercions

• If neither operand is a node set and the objects have different types, coercion of the lower precedence object to the higher precedence object is performed

• Order: boolean < number < string• true() = ‘joe’ is true as ‘joe’ is coerced into true• true() != 1.50 is false as 1.50 is coerced into true• “1.56” = 1.56 is true as “1.56” is coerced into 1.56

Page 61: Introduction to Xpath

Relational Expressions• <, <=, <, <=• Both operands are converted into numbers• Existential semantics as for equality• price <= 50 true if there is a child price element

with a price element whose string value when converted to a number is greater than or equal to 50

• price < preceding::price true if there is a child price element whose value is smaller than the value of some preceding price element, what if there is no preceding child element?

Page 62: Introduction to Xpath

Relational Expressions• //cd [ price < preceding::price ] returns <cd country="UK"<

<title<Hide your heart</title<<artist<Bonnie Tyler</artist<<musthave< yes</musthave<<price<9.90</price<

</cd<<cd country="USA"<

<title<Greatest Hits</title<<artist<Dolly Parton</artist<<scratch< yes</scratch<<price<9.90</price<

</cd<

Page 63: Introduction to Xpath

Numerical Expressions• Increasing precedence: +, -, div, mod, *,

unary -

• Each operand is coerced into a number

• 5 + 7 * 2 yields 19

• 5 + 7 * 2 = 19.0 yields true

• 5 mod 2 yields 1

• [ price div 2 = 1 ] is true for odd prices

Page 64: Introduction to Xpath

Functions

• Node-test functions: id, lang, last, local-name, name, namespace-uri, position

• Boolean functions: boolean, false, not, true• Numerical functions: ceiling, count, floor,

number, round, sum• String functions: concat, contains, normalize-

space, starts-with, string, string-length, substring, substring-after, substring-before, translate

Page 65: Introduction to Xpath

Node-test functions

• id(‘101’) returns the unique element with id 101

• id(‘ 101 102’) returns the unique elements with ids 101 or 102

• When applied to a node set, each node is converted to its string value and then id is applied to each string value

Page 66: Introduction to Xpath

Node-test functionsName Description Signature

count() number of nodes in a node-setinvoice [count (item) < 5]

number count(node-set)

id() Selects elements by their unique ID, see nextid (book/@similarbook)

node-set id(value)

last() Return position number of the last node in the node sequenceinvoice/item [last() < 3]

number last()Note: size of context set as a whole/catalog/cd/node() [last()=3] [self::title] [last() = 1]

local-name() the local part of a node(prefix::local-name)

string local-name(node)

name() the Qname of a node string name(node)

namespace-uri() the namespace URI of a specified node

uri namespace-uri(node)

position() the position in the node sequence of the node

number position()

Page 67: Introduction to Xpath

String functionsName Description Signature & Example

concat() the concatenation of all its arguments

string concat(val1, val2, ..)

Example:concat('The',' ','XML') = 'The XML' /catalog/cd [concat(title,artist) = "Hide your heartBonnie Tyler"]

contains() true if the second string is contained within the first string

boolean contains(val, substr)

Example:contains('XML','X') = true

normalize-space()

Removes leading and trailing spaces from a string

string normalize-space(string)

Example:normalize-space(' The   XML ') = 'The XML'

Page 68: Introduction to Xpath

starts-with()

true if the first string starts with the second string

boolean starts-with(string, substr) Example:starts-with('XML','X') = true

string() convert the argument to a string

string(value) Example:string(128) = '128'

string-length()

the number of characters in a string

number string-length(string) Example:string-length('Israel') = 6

substring() the part of the string argument specified in the argument by start and length

string substring(string, start, length) Example:substring('Beatles',1,4) = 'Beat'

 

Name Description Signature & Example

Page 69: Introduction to Xpath

String functions

substring-after()

the part of the string argument that occurs after the substr argument

string substring-after(string, substr)Example:substring-after('12/10','/') = '10'

substring-before()

the part of the string argument that occurs before the substr argument

string substring-before(string, substr) Example:substring-before('12/10','/') = '12'

Name Description Signature & Example

Page 70: Introduction to Xpath

String functions

translate() character by character replacement, the value argument characters contained in string1 are each replaced, by character for the in the same position in string2

string translate(value, string1, string2)

Examples:translate('12:30','30','45') = '12:45'translate('12:30','03','54') = '12:45'translate('12:30','0123','abcd') = 'bc:da'

Name Description Signature & Example

Page 71: Introduction to Xpath

Boolean functions

 

Name Description Signature & Example

boolean() Converts the value argument to Boolean and returns true or false

boolean boolean(value)

false() false false() Example:number(false())=0

lang() true if the language argument matches the language of the xsl:lang element

boolean lang(language)

Page 72: Introduction to Xpath

Boolean functions

Name Description Signature & Example

not() true if the condition argument is false

boolean not(condition)

Example:not(false())

true() true true()

Example:number(true()) = 1

Page 73: Introduction to Xpath

Xpath 2.0 Data Model

• A tree with the following node types– Document (root), element, attribute, text, namespace,

processing instruction, and comment

• Document node at the root• Various accessors are used to characterize nodes• See http://www.w3.org/TR/xpath-datamodel/

which covers:XQuery 1.0 and XPath 2.0 Data Model, W3C Working Draft 02 May 2003

Page 74: Introduction to Xpath

Accessors

• Accessors are defined on Nodes. • Some accessors may return a constant

empty sequence on certain node kinds. • There are additional accessors that we do

not cover.• Accessors are descriptions of the interface

that an implementation of the data model must expose to applications.

Page 75: Introduction to Xpath

Accessors

• dm:base-uri($n as Node) as xs:anyURI? • dm:node-kind($n as Node) as xs:string • dm:node-name($n as Node) as xs:QName? • dm:parent($n as Node) as Node?• dm:string-value($n as Node) as xs:string• dm:typed-value($n as Node) as xdt:anyAtomicType* • dm:type($n as Node) as xs:QName? • dm:children($n as Node) as Node* • dm:attributes($n as Node) as AttributeNode*• dm:namespaces($n as Node) as NamespaceNode* • dm:nilled($n as Node) as xs:boolean

Page 76: Introduction to Xpath

Data Model File Example

<?xml version="1.0"?<<?xml-stylesheet type="text/xsl" href="dm-example.xsl"?<

<catalog xmlns="http://www.example.com/catalog"

xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance" xsi:schemaLocation="http://www.example.com/catalog dm-example.xsd" version="0.1"<

Page 77: Introduction to Xpath

More Data

<tshirt code="T1534017" label=" Staind : Been Awhile " xlink:href="http://example.com/0,,1655091,00.html" sizes="M L XL"<

<title< Staind: Been Awhile Tee Black (1-sided) </title< <description< <html:p< Lyrics from the hit song 'It's Been

Awhile' are shown in white, beneath the large 'Flock &amp; Weld' Staind logo. A very unique logo that looks as cool as it feels! </html:p< </description<

<price< 25.00 </price< </tshirt<

Page 78: Introduction to Xpath

Cont.

<album code="A1481344" label=" Staind : Its Been A While " formats="CD"<

<title< It's Been A While </title< <description xsi:nil="true" /<

<price currency="USD"< 10.99 </price< <artist< Staind </artist<

</album< </catalog<

Page 79: Introduction to Xpath

Data Model Nodes

• // Document node D1 • dm:base-uri(D1)= xs:anyURI("http://www.example.com/

catalog.xml")• dm:string-value(D1)="  Staind:  Been  Awhile  Tee  Black 

(1-sided)  \n            Lyrics  from  the  hit  song  'It's  Been  Awhile'\n            are  shown  in  white,  beneath  the  large\n            'Flock  &  Weld'  Staind  logo.  A  very  unique\n            logo  that  looks  as  cool  as  it  feels!\n          25.00    It's  Been  A  While    10.99    Staind  “

• dm:children(D1)= ([E1])

Page 80: Introduction to Xpath

Data Model Nodes: Namespace Nodes

• // Namespace node N1

• dm:node-kind(N1)= "namespace“

• dm:node-name(N1)= xs:QName("", "xml")

• dm:string-value(N1)=http://www.w3.org/XML/1998/namespace

• Similarly for N2, N3, N4 and N5.

Page 81: Introduction to Xpath

Data Model Nodes: Processing Instruction Nodes

• // Processing Instruction node P1• dm:base-uri(P1)= xs:anyURI("http://

www.example.com/catalog.xml")• dm:node-kind(P1)= "processing-instruction“• dm:node-name(P1)= xs:QName("", "xml-

stylesheet")• dm:string-value(P1)="type="text/xsl"  href="dm-

example.xsl"“• dm:parent(P1)= ([D1])

Page 82: Introduction to Xpath

Data Model Nodes: Element Nodes

• // Element node E1• dm:base-uri(E1)= xs:anyURI("http://www.example.com/catalog.xml")• dm:node-kind(E1)= "element"dm:node-name(E1)= xs:QName("http://

www.example.com/catalog", "catalog")• dm:string-value(E1)="  Staind:  Been  Awhile  Tee  Black  (1-sided)  \n            Lyrics 

from  the  hit  song  'It's  Been  Awhile'\n            are  shown  in  white,  beneath  the  large\n            'Flock  &  Weld'  Staind  logo.  A  very  unique\n            logo  that  looks  as  cool  as  it  feels!\n          25.00    It's  Been  A  While    10.99    Staind  “

• dm:typed-value(E1)= fn:error()// xs:anyType because of the anonymous type definition• dm:type(E1)= xs:anyType• dm:parent(E1)= ([D1])• dm:children(E1)= ([E2], [E7])• dm:attributes(E1)= ([A1], [A2])• dm:namespaces(E1)= ([N1], [N2], [N3], [N4], [N5])

Page 83: Introduction to Xpath

Data Model Nodes: Attribute Nodes

// Attribute node A1dm:node-kind(A1)= "attribute“dm:node-name(A1)= xs:QName("http://www.w3.org/2001/

XMLSchema-instance", "xsi:schemaLocation")dm:string-value(A1)="http://www.example.com/catalog         

                                                  dm-example.xsd“dm:typed-value(A1)= (xs:anyURI("http://

www.example.com/catalog"), xs:anyURI("catalog.xsd"))dm:type(A1)= xs:anySimpleTypedm:parent(A1)= ([E1])

Page 84: Introduction to Xpath
Page 85: Introduction to Xpath

Summary of (Some) Accessors

• We cover some of the accessors, the rest are summarized at

http://www.w3.org/TR/xpath-datamodel/

Page 86: Introduction to Xpath

dm:base-uri

On node typeReturns:

DocumentsThe value of the base-uri property

ElementsThe value of the base-uri property or its parent's base URI

Attributes()

Namespaces()

Processing Instructions

The value of the base-uri property or its parent's base URI

CommentsThe base URI of its parent

TextThe base URI of its parent

Page 87: Introduction to Xpath

dm:node-kind

On node typeReturns:

Documents"document"

Elements"element"

Attributes"attribute"

Namespaces"namespace"

Processing Instructions"processing-instruction"

Comments"comment"

Text"text"

Page 88: Introduction to Xpath

dm:node-name

On node typeReturns:

Documents()

ElementsThe xs:QName of the element

AttributesThe xs:QName of the attribute

NamespacesA xs:QName with the namespace prefix in the local-

name and an empty URI

Processing Instructions

A xs:QName with the processing-instruction target in the local-name and an empty URI

Comments()

Text()

Page 89: Introduction to Xpath

dm:parent

On node typeReturns:

Documents()

ElementsThe parent element or document node

AttributesThe parent element node

NamespacesThe parent element node

Processing InstructionsThe parent element or document node

CommentsThe parent element or document node

TextThe parent element or document node

Page 90: Introduction to Xpath

dm:string-value

On node typeReturns:

DocumentsThe concatenation of the string-values of all the text node descendants of the document in document order

ElementsThe concatenation of the string-values of all the text node descendants of the element in document order

AttributesThe value of the attribute

NamespacesThe namespace name (URI) of the node

Processing Instructions

The content of the processing-instruction

CommentsThe content of the comment

TextThe text content