Presentation is loading. Please wait.

Presentation is loading. Please wait.

Managing Data Exchange: XPath

Similar presentations


Presentation on theme: "Managing Data Exchange: XPath"— 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 node 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:
<?xml version="1.0" encoding="UTF-8"?> <trunk name="the_trunk"> <bigBranch name="bb1" thickness="thick"> <smallBranch name="sb1"> <leaf name="leaf1" color="brown" /> <leaf name="leaf2" weight="50" /> <leaf name="leaf3" /> </smallBranch> <smallBranch name="sb2"> <leaf name="leaf4" weight="90" /> <leaf name="leaf5" color="purple" /> </smallBranch> </bigBranch> <bigBranch name="bb2"> <smallBranch name="sb3"> <leaf name="leaf6" /> <smallBranch name="sb4"> <leaf name="leaf7" /> <leaf name="leaf8" /> <leaf name="leaf9" color="black" /> <leaf name="leaf10" weight="100" /> </smallBranch> </trunk> All <leaf> elements in this document that are children of <smallBranch> elements that are children of <bigBranch> 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 The <bigBranch> elements with “name” attribute equal to “bb2”, that are the children of the trunk element, which is the child of the root. /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: <?xml version="1.0" encoding="UTF-8"?> <trunk name="the_trunk"> <bigBranch name="bb1" thickness="thick"> <smallBranch name="sb1"> <leaf name="leaf1" color="brown" /> <leaf name="leaf2" weight="50" /> <leaf name="leaf3" /> </smallBranch> <smallBranch name="sb2"> <leaf name="leaf4" weight="90" /> <leaf name="leaf5" color="purple" /> </smallBranch> </bigBranch> <bigBranch name="bb2"> <smallBranch name="sb3"> <leaf name="leaf6" /> <smallBranch name="sb4"> <leaf name="leaf7" /> <leaf name="leaf8" /> <leaf name="leaf9" color="black" /> <leaf name="leaf10" weight="100" /> </smallBranch> </trunk> All <leaf> elements in this document that are children of <smallBranch> elements that are children of <bigBranch> elements, that are children of the trunk, which is the child of the root. Syntax: /trunk/bigBranch/smallBranch/leaf The <bigBranch> elements with “name” attribute equal to “bb2”, that are the children of the trunk element, which is the child of the root. -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
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:output method="html"/> <!-- Example of an absolute link. The element ‘/child::trunk'’ is being specified from the root element. --> <xsl:template match="/child::trunk"> <html> <head> <title>XPath Tree Tests</title> </head> <body> <!-- Example of a relative link. The <for-each> xsl statement will execute for every <bigBranch> node in the ‘current’ node, which is the <trunk>node. --> <xsl:for-each select="child::bigBranch"> <xsl:call-template name="print_out" /> </xsl:for-each> </body> </html> </xsl:template> <xsl:template name="print_out"> <xsl:value-of select="attribute::name" /> <br /> </xsl:stylesheet>

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: <?XML Version=“1.0” encoding=“”?> <movieList> <movie> <movieName>Wedding Crashers</movieName> <movieLength>120</movieLength> <movieRating>PG-13</movieRating> </movie> <movieName>Dodge Ball</movieName> <movieLength>105</movieLength> <movieName>Spider Man</movieName> <movieLength>132</movieLength> <movieRating>R</movieRating> </movieList> -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:
<?XML Version=“1.0” encoding=“UTF-8”?> <movieList> <movie> <movieName>Wedding Crashers</movieName> <movieLength>120</movieLength> <movieRating>PG-13</movieRating> </movie> <movieName>Dodge Ball</movieName> <movieLength>105</movieLength> <movieName>Spider Man</movieName> <movieLength>132</movieLength> <movieRating>R</movieRating> </movieList> 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"

Similar presentations


Ads by Google