Presentation is loading. Please wait.

Presentation is loading. Please wait.

Managing Data Exchange: XPath Presented By: David Harris.

Similar presentations


Presentation on theme: "Managing Data Exchange: XPath Presented By: David Harris."— Presentation transcript:

1 Managing Data Exchange: XPath Presented By: David Harris

2 Chapter Objectives: Conceptualize an XML document as a node tree Understand the differences between Abbreviated and Unabbreviated syntax Understand the differences between Absolute vs. Relative Paths Use of predicates and functions

3 XPath Overview: XML path language is used for referring to “nodes” in an XML document The node relationships are parent, child, descendent, sibling and ancestor A node precedes another node if it comes first in document order A node follows another node if it comes after it in the document order

4 XML and HTML as tree structures:

5 XML as a tree structure: Trunk = Root Element BigBranch = Parent

6 XPath Axes Examples: Axes Meaning ancestor:: Parents of the current node upto the root node ancestor-or-self:: Parents of the current node upto the root node and the current node attribute:: Attributes of the current node child:: Immediate children of the current node descendant:: Children of the current node (including children's children) descendant-or-self:: Children of the current node (including children's children) and the current node following:: Nodes after the current node (excluding children) following-sibling:: Nodes after the current node (excluding children) at the same level namespace:: XML namespace of the current nodeXML namespace parent:: Immediate parent of the current node preceding:: Nodes before the current node (excluding children) preceding-sibling:: Nodes before the current node (excluding children) at the same level self:: The current node

7 Unabbreviated Syntax: 1. All elements in this document that are children of elements that are children of elements, that are children of the trunk, which is the child of the root. Syntax: /child::trunk/child::bigBranch/child::smallBranch/child::leaf -The two colons between are called an axis 2. The elements with “name” attribute equal to “bb2”, that are the children of the trunk element, which is the child of the root. Syntax: /child::trunk/child::bigBranch[attribute::name=‘bb2’] -The predicate is in the square brackets -Predicate here is asking for the big branch node with attribute name set to “bb2”. -attribute must be spelled out and include the axis.

8 Abbreviated Syntax: 1. All elements in this document that are children of elements that are children of elements, that are children of the trunk, which is the child of the root. Syntax: /trunk/bigBranch/smallBranch/leaf 2. The elements with “name” attribute equal to “bb2”, that are the children of the trunk element, which is the child of the root. Syntax: -The predicate is in the square brackets -Predicate here is asking for the big branch node with attribute name set to “bb2”. -”attribute” is replaced with symbol

9 Relative vs. Absolute Paths Works much like when referring to other files in HTML hyperlinks by including things such as server name, folders, and file names Relative Path: Gives the file location in relation to the current working directory. Directories and sub-directories are separated by a slash ‘ / ‘, but do not begin with one. Absolute Path: Gives an exact location of a file or directory name within a computer or file system. Begins with a slash ‘ / ‘ and directories and sub-directories are separated by a slash ‘ / ‘.

10 Relative and Absolute Paths XPath Tree Tests xsl statement will execute for every node in the ‘current’ node, which is the node. -->

11 Four Types of XPath Location Paths The combining of Relative, Absolute, Abbreviated, and Unabbreviated creates four possible location paths. 1. Abbreviated Relative Location Paths 2. Abbreviated Absolute Location Paths 3. Unabbreviated Relative Location Paths 4. Unabbreviated Absolute Location Paths

12 Movie Examples: Wedding Crashers 120 PG-13 Dodge Ball 105 PG-13 Spider Man 132 R -The following XPath expression will be used to select certain elements from the movieList. 1. Show all the movie names on the movie list. /movieList/movie/movieName 2. Select all the movies that have a length greater than 115 minutes. /movieList/movie/[movieLength >115]

13 Predicates and Functions Predicates allow for further filtering of selections Predicates get evaluated in each set of nodes Examples: 1. number last() = last node in the current node set number 2. number position() = position of the context node being tested 3. number count(node-set) = the number of nodes in a node set 4. number sum(node-set) = the number of numeric values of the nodes in the node-set 5. number round(number) = the number, rounded to the nearest integer

14 Predicate and Functions Examples: Wedding Crashers 120 PG-13 Dodge Ball 105 PG-13 Spider Man 132 R 1. If you would like to get the last node the function would look like this: /movieList/movie/[last()] -It is possible to select more than one path. When doing this the ( | ) operator is used. 2. Select the movie name and how long it last. /movieList/movie/movieName | /movieList/movie/movieLength

15 Questions?

16 Exercises: Modify a previous document and stylesheet of your choice, or create a new one, so that your stylesheet uses a predicate to call for a specific item in your document. Also incorporate the use of a function of your choice in a stylesheet.


Download ppt "Managing Data Exchange: XPath Presented By: David Harris."

Similar presentations


Ads by Google