Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1/17 ITApplications XML Module Session 7: Introduction to XPath."— Presentation transcript:

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

2 2/17 Purpose of XPath Syntax for identifying nodes and groups of nodes in an XML document Syntax for identifying nodes and groups of nodes in an XML document Use it for navigating an XML document Use it for navigating an XML document Use it to query and test content Use it to query and test content XPath is a major component of XSLT XPath is a major component of XSLT XPath is a W3C standard XPath is a W3C standard

3 3/17 XPath – Definition of nodes Nodes are the components of an XML document Nodes are the components of an XML document There are 7 types of nodes but the most common are: There are 7 types of nodes but the most common are: Elements Elements Attributes Attributes Text Text Document node (root node) Document node (root node) Used in XSLT to identify content and values Used in XSLT to identify content and values Forms a tree structure with the root node at the base (or top) Forms a tree structure with the root node at the base (or top)

4 4/17 An XML tree

5 5/17 Axes 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 the current node itself ancestor-or-self: Selects all ancestors (parent, grandparent, etc.) of the current node and 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 node and the current node itself descendant-or-self: Selects all descendants (children, grandchildren, etc.) of the current 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

6 6/17 XPath – Expressions Locate or identify a node or node-set Locate or identify a node or node-set Compute a value Compute a value Include functions Include functions Test conditions Test conditions Predicates Predicates

7 7/17 XPath – Location Expressions Context node Context node Node currently processed by XSLT Node currently processed by XSLT Represented by “.” Represented by “.” Result of XSLT “select” or “match” Result of XSLT “select” or “match” Path relative to context node Path relative to context node The node you seek is the last in the path The node you seek is the last in the path

8 8/17 XPath – Location Expressions Different levels of an XML tree are separated by “/” Different levels of an XML tree are separated by “/”/addressbook/person/name/lastname Use “@” to select an attribute Use “@” 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 context node “//” identifies any or all nodes that match from context node//personperson//@type

9 9/17 XPath – Operators and Functions You can use mathematical operators for calculations on any node values that can be represented as numbers You can use mathematical operators for calculations on any node values that can be represented as numbers salary[@week=‘1’] + salary[@week=‘2’] (totalrevenue * taxrate) / capital Functions can calculate or test certain values Functions can calculate or test certain values contain() tests if a certain string contains a string position() returns the context node’s position in the node-set Use operators to test conditions Use operators to test conditions actualsalary != promisedsalary lastname <= “F” @today > @expiry

10 10/17 XPath – Predicates Expressions Part of an expression that identifies a specific node due to be tested Part of an expression that identifies a specific node due to be tested Are enclosed in square brackets Are 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 fails If the predicate fails then the path location fails

11 11/17 XPath – Node relationships Nodes are related to other nodes Nodes are related to other nodes Relationship types: Relationship types: Ancestors Ancestors Parent Parent Children Children Descendents Descendents Siblings Siblings

12 12/17 Examples Alex Ukovic Ancestors Ancestors,, are ancestors of &,, are ancestors of &

13 13/17 Examples Alex Ukovic Parent Parent is the parent of & is the parent of &

14 14/17 Examples Alex Ukovic Children Children & are children of & are children of

15 15/17 Examples Alex Ukovic Descendents Descendents, & are descendents of, & are descendents of

16 16/17 Examples Alex Ukovic Siblings Siblings and are siblings and are siblings

17 17/17 Download the XPath Visualiser (xm_xpath_visualiser.zip) by clicking on the link in Session 7 folder in Blackboard. Download the XPath Visualiser (xm_xpath_visualiser.zip) by clicking on the link in Session 7 folder in Blackboard. Alternatively Download the XPath Checker addon from the following URL: Alternatively Download the XPath Checker addon from the following URL: https://addons.mozilla.org/en- US/firefox/addon/xpath-checker/ https://addons.mozilla.org/en- US/firefox/addon/xpath-checker/ https://addons.mozilla.org/en- US/firefox/addon/xpath-checker/ https://addons.mozilla.org/en- US/firefox/addon/xpath-checker/ Then create the address book XML document and try out the XPath expressions just discussed. Then create the address book XML document and try out the XPath expressions just discussed.


Download ppt "1/17 ITApplications XML Module Session 7: Introduction to XPath."

Similar presentations


Ads by Google