17
1/17 ITApplications ITApplications XML Module XML Module Session 7: Session 7: Introduction to XPath Introduction to XPath

1/17 ITApplications XML Module Session 7: Introduction to XPath

Embed Size (px)

Citation preview

Page 1: 1/17 ITApplications XML Module Session 7: Introduction to XPath

1/17

ITApplications ITApplications XML ModuleXML Module

Session 7:Session 7:Introduction to XPathIntroduction to XPath

Page 2: 1/17 ITApplications XML Module Session 7: Introduction to XPath

2/17

Purpose of XPathPurpose of XPath

Syntax for identifying nodes and groups of Syntax for identifying nodes and groups of nodes in an XML documentnodes in an XML document

Use it for navigating an XML documentUse it for navigating an XML document Use it to query and test contentUse it to query and test content XPath is a major component of XSLTXPath is a major component of XSLT XPath is a W3C standardXPath is a W3C standard

Page 3: 1/17 ITApplications XML Module Session 7: Introduction to XPath

3/17

XPath – Definition of nodesXPath – Definition of nodes

Nodes are the components of an XML documentNodes are the components of an XML document There are 7 types of nodes but the most There are 7 types of nodes but the most

common are:common are: ElementsElements AttributesAttributes TextText Document node (root node)Document node (root node)

Used in XSLT to identify content and valuesUsed in XSLT to identify content and values Forms a tree structure with the root node at the Forms a tree structure with the root node at the

base (or top)base (or top)

Page 4: 1/17 ITApplications XML Module Session 7: Introduction to XPath

4/17

An XML treeAn XML tree

Page 5: 1/17 ITApplications XML Module Session 7: Introduction to XPath

5/17

AxesAxes An axis defines a node-set relative to the current node:An axis defines a node-set relative to the current node:

ancestor: Selects all ancestors (parent, grandparent, etc.) of the current node ancestor: Selects all ancestors (parent, grandparent, etc.) of the current node ancestor-or-self: Selects all ancestors (parent, grandparent, etc.) of the current node and ancestor-or-self: Selects all ancestors (parent, grandparent, etc.) of the current node and

the current node itself the current node itself attribute: Selects all attributes of the current node attribute: Selects all attributes of the current node child: Selects all children of the current node child: Selects all children of the current node descendant: Selects all descendants (children, grandchildren, etc.) of the current node descendant: Selects all descendants (children, grandchildren, etc.) of the current node descendant-or-self: Selects all descendants (children, grandchildren, etc.) of the current descendant-or-self: Selects all descendants (children, grandchildren, etc.) of the current

node and the current node itself node and the current node itself following: Selects everything in the document after the closing tag of the current node following: Selects everything in the document after the closing tag of the current node following-sibling: Selects all siblings after the current node following-sibling: Selects all siblings after the current node namespace: Selects all namespace nodes of the current node namespace: Selects all namespace nodes of the current node parent: Selects the parent of the current node parent: Selects the parent of the current node preceding: Selects everything in the document that is before the start tag of the current node preceding: Selects everything in the document that is before the start tag of the current node preceding-sibling: Selects all siblings before the current node preceding-sibling: Selects all siblings before the current node self: Selects the current node self: Selects the current node

Page 6: 1/17 ITApplications XML Module Session 7: Introduction to XPath

6/17

XPath – ExpressionsXPath – Expressions

Locate or identify a node or node-setLocate or identify a node or node-set Compute a valueCompute a value Include functionsInclude functions Test conditionsTest conditions PredicatesPredicates

Page 7: 1/17 ITApplications XML Module Session 7: Introduction to XPath

7/17

XPath – Location ExpressionsXPath – Location Expressions

Context nodeContext node Node currently processed by XSLTNode currently processed by XSLT Represented by “.”Represented by “.” Result of XSLT “select” or “match”Result of XSLT “select” or “match”

Path relative to context nodePath relative to context node The node you seek is the last in the pathThe node you seek is the last in the path

Page 8: 1/17 ITApplications XML Module Session 7: Introduction to XPath

8/17

XPath – Location ExpressionsXPath – Location Expressions Different levels of an XML tree are separated by Different levels of an XML tree are separated by

“/”“/”/addressbook/person/name/lastname/addressbook/person/name/lastname

Use “@” to select an attributeUse “@” to select an attribute /person/address[1]/@type/person/address[1]/@type

Parent node represented by “..”Parent node represented by “..” ../telephone../telephone

““//” identifies any or all nodes that match from //” identifies any or all nodes that match from context nodecontext node

//person//personperson//@typeperson//@type

Page 9: 1/17 ITApplications XML Module Session 7: Introduction to XPath

9/17

XPath – Operators and FunctionsXPath – Operators and Functions

You can use mathematical operators for calculations on You can use mathematical operators for calculations on any node values that can be represented as numbersany node values that can be represented as numbers

salary[@week=‘1’] + salary[@week=‘2’]salary[@week=‘1’] + salary[@week=‘2’](totalrevenue * taxrate) / capital(totalrevenue * taxrate) / capital

Functions can calculate or test certain valuesFunctions can calculate or test certain valuescontain() tests if a certain string contains a stringcontain() tests if a certain string contains a stringposition() returns the context node’s position in the position() returns the context node’s position in the node-setnode-set

Use operators to test conditionsUse operators to test conditionsactualsalary != promisedsalaryactualsalary != promisedsalarylastname <= “F”lastname <= “F”@today > @expiry@today > @expiry

Page 10: 1/17 ITApplications XML Module Session 7: Introduction to XPath

10/17

XPath – Predicates ExpressionsXPath – Predicates Expressions

Part of an expression that identifies a Part of an expression that identifies a specific node due to be testedspecific node due to be tested

Are enclosed in square bracketsAre enclosed in square brackets /person[name/lastname=‘Marsh’]/person[name/lastname=‘Marsh’]

/product[@expiry < ‘20050704’]/name/product[@expiry < ‘20050704’]/name If the predicate fails then the path location If the predicate fails then the path location

failsfails

Page 11: 1/17 ITApplications XML Module Session 7: Introduction to XPath

11/17

XPath – Node relationshipsXPath – Node relationships

Nodes are related to other nodesNodes are related to other nodes Relationship types:Relationship types:

AncestorsAncestors ParentParent ChildrenChildren DescendentsDescendents SiblingsSiblings

Page 12: 1/17 ITApplications XML Module Session 7: Introduction to XPath

12/17

ExamplesExamples

<?xml version="1.0" encoding="UTF-8"?><addressbook>

<person><name>

<firstname>Alex</firstname><lastname>Ukovic</lastname>

</name></person>

</addressbook>

AncestorsAncestors <addressbook>, <person>, <name> are ancestors of <addressbook>, <person>, <name> are ancestors of

<firstname> & <lastname><firstname> & <lastname>

Page 13: 1/17 ITApplications XML Module Session 7: Introduction to XPath

13/17

ExamplesExamples

<?xml version="1.0" encoding="UTF-8"?><addressbook>

<person><name>

<firstname>Alex</firstname><lastname>Ukovic</lastname>

</name></person>

</addressbook>

ParentParent <name> is the parent of <firstname> & <lastname><name> is the parent of <firstname> & <lastname>

Page 14: 1/17 ITApplications XML Module Session 7: Introduction to XPath

14/17

ExamplesExamples

<?xml version="1.0" encoding="UTF-8"?><addressbook>

<person><name>

<firstname>Alex</firstname><lastname>Ukovic</lastname>

</name></person>

</addressbook>

ChildrenChildren <firstname> & <lastname> are children of <name><firstname> & <lastname> are children of <name>

Page 15: 1/17 ITApplications XML Module Session 7: Introduction to XPath

15/17

ExamplesExamples

<?xml version="1.0" encoding="UTF-8"?> <addressbook>

<person><name>

<firstname>Alex</firstname><lastname>Ukovic</lastname>

</name></person>

</addressbook>

DescendentsDescendents <name>, <firstname> & <lastname> are descendents <name>, <firstname> & <lastname> are descendents

of <person>of <person>

Page 16: 1/17 ITApplications XML Module Session 7: Introduction to XPath

16/17

ExamplesExamples

<?xml version="1.0" encoding="UTF-8"?><addressbook>

<person><name>

<firstname>Alex</firstname><lastname>Ukovic</lastname>

</name></person>

</addressbook>

SiblingsSiblings <firstname> and <lastname> are siblings<firstname> and <lastname> are siblings

Page 17: 1/17 ITApplications XML Module Session 7: Introduction to XPath

17/17

Download the XPath Visualiser Download the XPath Visualiser (xm_xpath_visualiser.zip) by clicking on (xm_xpath_visualiser.zip) by clicking on the link in Session 7 folder in Blackboard.the link in Session 7 folder in Blackboard.

Alternatively Download the Alternatively Download the XPath XPath Checker addon Checker addon from the following URL:from the following URL:

https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/

Then create the address book XML Then create the address book XML document and try out the XPath document and try out the XPath expressions just discussed. expressions just discussed.