You are on page 1of 10

XPath is a language for finding information in an XML document.

XPath is used to navigate through elements and attributes in an XML document. What You Should Already Know Before you continue you should have a basic understanding of the following: HTML / XHTML XML / XML Namespaces f you want to study these sub!ects first" find the tutorials on our Home page# What is XPath X$ath is a synta% for defining parts of an XML document X$ath uses path e%pressions to navigate in XML documents X$ath contains a library of standard functions X$ath is a ma!or element in X&LT X$ath is a '() &tandard XPath Path !"#ressions X$ath uses path e%pressions to select nodes or node*sets in an XML document# These path e%pressions loo+ very much li+e the e%pressions you see when you wor+ with a traditional computer file system# XPath Standard $unctions X$ath includes over ,-- built*in functions# There are functions for string values" numeric values" date and time comparison" node and .Name manipulation" se/uence manipulation" Boolean values" and more# XPath is %sed in XSL& X$ath is a ma!or element in the X&LT standard# 'ithout X$ath +nowledge you will not be able to create X&LT documents# 0ou can read more about X&LT in our X&LT tutorial# X.uery and X$ointer are both built on X$ath e%pressions# X.uery ,#- and X$ath 1#- share the same data model and support the same functions and operators# 0ou can read more about X.uery in our X.uery tutorial# XPath is a W'( Standard X$ath became a '() 2ecommendation ,3# November ,444# X$ath was designed to be used by X&LT" X$ointer and other XML parsing software# 0ou can read more about the X$ath standard in our '() tutorial# XPath )odes *n XPath+ there are seven ,inds of nodes- element+ attribute+ te"t+ names#ace+ #rocessing.instruction+ comment+ and document /root0 nodes. XPath &erminology )odes n X$ath" there are seven +inds of nodes: element" attribute" te%t" namespace" processing*instruction" comment" and document 5root6 nodes# XML documents are treated as trees of nodes# The root of the tree is called the document node 5or root node6# Loo+ at the following XML document: 78%ml version9:,#-: encoding9: &;*<<=4*,:8> 7boo+store> 7boo+> 7title lang9:en:>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> 7/boo+store>

A%ample of nodes in the XML document above: 7boo+store> 5document node6 7author>? @# 2owling7/author> 5element node6 lang9:en: 5attribute node6 Atomic values Btomic values are nodes with no children or parent# A%ample of atomic values: ? @# 2owling :en: *tems tems are atomic values or nodes# 1elationshi# of )odes Parent Aach element and attribute has one parent# n the following e%ampleC the boo+ element is the parent of the title" author" year" and price: 7boo+> 7title>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> (hildren Alement nodes may have Dero" one or more children# n the following e%ampleC the title" author" year" and price elements are all children of the boo+ element: 7boo+> 7title>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> Siblings Nodes that have the same parent# n the following e%ampleC the title" author" year" and price elements are all siblings: 7boo+> 7title>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> Ancestors B nodeEs parent" parentEs parent" etc# n the following e%ampleC the ancestors of the title element are the boo+ element and the boo+store element: 7boo+store> 7boo+> 7title>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price>

7/boo+> 7/boo+store> 2escendants B nodeEs children" childrenEs children" etc# n the following e%ampleC descendants of the boo+store element are the boo+" title" author" year" and price elements: 7boo+store> 7boo+> 7title>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> 7/boo+store> XPath Synta" XPath uses #ath e"#ressions to select nodes or node.sets in an XML document. &he node is selected by following a #ath or ste#s. &he XML !"am#le 2ocument 'e will use the following XML document in the e%amples below# 78%ml version9:,#-: encoding9: &;*<<=4*,:8> 7boo+store> 7boo+> 7title lang9:eng:>Harry $otter7/title> 7price>14#447/price> 7/boo+> 7boo+> 7title lang9:eng:>Learning XML7/title> 7price>(4#4=7/price> 7/boo+> 7/boo+store> Selecting )odes X$ath uses path e%pressions to select nodes in an XML document# The node is selected by following a path or steps# The most useful path e%pressions are listed below: !"#ression 2escri#tion nodename &elects all child nodes of the named node / &elects from the root node // &elects nodes in the document from the current node that match the selection no matter where they are # &elects the current node ## &elects the parent of the current node F &elects attributes !"am#les n the table below we have listed some path e%pressions and the result of the e%pressions: Path !"#ression 1esult

boo+store /boo+store

boo+store/boo+ //boo+ boo+store//boo+ //Flang

&elects all the child nodes of the boo+store element &elects the root element boo+store )ote- f the path starts with a slash 5 / 6 it always represents an absolute path to an elementG &elects all boo+ elements that are children of boo+store &elects all boo+ elements no matter where they are in the document &elects all boo+ elements that are descendant of the boo+store element" no matter where they are under the boo+store element &elects all attributes that are named lang

Predicates $redicates are used to find a specific node or a node that contains a specific value# $redicates are always embedded in s/uare brac+ets# !"am#les n the table below we have listed some path e%pressions with predicates and the result of the e%pressions: Path !"#ression 1esult /boo+store/boo+H,I &elects the first boo+ element that is the child of the boo+store element# )ote- A= and later has implemented that H-I should be the first node" but according to the '() standard it should have been H,IGG /boo+store/boo+Hlast56I &elects the last boo+ element that is the child of the boo+store element /boo+store/boo+Hlast56*,I &elects the last but one boo+ element that is the child of the boo+store element /boo+store/boo+Hposition567(I &elects the first two boo+ elements that are children of the boo+store element //titleHFlangI &elects all the title elements that have an attribute named lang //titleHFlang9EengEI &elects all the title elements that have an attribute named lang with a value of EengE /boo+store/boo+Hprice>(=#--I &elects all the boo+ elements of the boo+store element that have a price element with a value greater than (=#-/boo+store/boo+Hprice>(=#--I/title &elects all the title elements of the boo+ elements of the boo+store element that have a price element with a value greater than (=#-Selecting %n,nown )odes X$ath wildcards can be used to select un+nown XML elements# Wildcard 2escri#tion J Matches any element node FJ Matches any attribute node node56 Matches any node of any +ind !"am#les

n the table below we have listed some path e%pressions and the result of the e%pressions: Path !"#ression 1esult /boo+store/J &elects all the child nodes of the boo+store element //J &elects all elements in the document //titleHFJI &elects all title elements which have any attribute Selecting Several Paths By using the K operator in an X$ath e%pression you can select several paths# !"am#les n the table below we have listed some path e%pressions and the result of the e%pressions: Path !"#ression 1esult //boo+/title K //boo+/price &elects all the title BNL price elements of all boo+ elements //title K //price &elects all the title BNL price elements in the document /boo+store/boo+/title K //price &elects all the title elements of the boo+ element of the boo+store element BNL all the price elements in the document XPath A"es &he XML !"am#le 2ocument 'e will use the following XML document in the e%amples below# 78%ml version9:,#-: encoding9: &;*<<=4*,:8> 7boo+store> 7boo+> 7title lang9:eng:>Harry $otter7/title> 7price>14#447/price> 7/boo+> 7boo+> 7title lang9:eng:>Learning XML7/title> 7price>(4#4=7/price> 7/boo+> 7/boo+store> XPath A"es Bn a%is defines a node*set relative to the current node# A"is)ame 1esult ancestor &elects all ancestors 5parent" grandparent" etc#6 of the current node ancestor*or*self &elects all ancestors 5parent" grandparent" etc#6 of the current node and the current node itself attribute &elects all attributes of the current node child &elects all children of the current node descendant &elects all descendants 5children" grandchildren" etc#6 of the current node descendant*or*self &elects all descendants 5children" grandchildren" etc#6 of the current node and the current node itself following &elects everything in the document after the closing tag of the

following*sibling namespace parent preceding preceding*sibling self

current node &elects all siblings after the current node &elects all namespace nodes of the current node &elects the parent of the current node &elects everything in the document that is before the start tag of the current node &elects all siblings before the current node &elects the current node

Location Path !"#ression B location path can be absolute or relative# Bn absolute location path starts with a slash 5 / 6 and a relative location path does not# n both cases the location path consists of one or more steps" each separated by a slash: Bn absolute location path: /step/step/### B relative location path: step/step/### Aach step is evaluated against the nodes in the current node*set# B step consists of: an a%is 5defines the tree*relationship between the selected nodes and the current node6 a node*test 5identifies a node within an a%is6 Dero or more predicates 5to further refine the selected node*set6 The synta% for a location step is: a%isname::nodetestHpredicateI !"am#les !"am#le 1esult child::boo+ attribute::lang child::J attribute::J child::te%t56 child::node56 descendant::boo+ ancestor::boo+ ancestor*or*self::boo+ child::J/child::price XPath 3#erators An XPath e"#ression returns either a node.set+ a string+ a 4oolean+ or a number. XPath 3#erators Below is a list of the operators that can be used in X$ath e%pressions: &elects all boo+ nodes that are children of the current node &elects the lang attribute of the current node &elects all children of the current node &elects all attributes of the current node &elects all te%t child nodes of the current node &elects all child nodes of the current node &elects all boo+ descendants of the current node &elects all boo+ ancestors of the current node &elects all boo+ ancestors of the current node * and the current as well if it is a boo+ node &elects all price grandchildren of the current node

3#erator K M * J div 9 G9 7 79 > >9 or and mod

2escri#tion )omputes two node*sets Bddition &ubtraction Multiplication Livision A/ual Not e/ual Less than Less than or e/ual to Oreater than Oreater than or e/ual to or and Modulus 5division remainder6

!"am#le //boo+ K //cd 3MN 3*N 3JN < div N price94#<priceG94#<price74#<price794#<price>4#<price>94#<price94#<- or price94#Pprice>4#-- and price74#4= mod 1

1eturn value 2eturns a node*set with all boo+ and cd elements ,1 1N 1 true if price is 4#<false if price is 4#4true if price is 4#4false if price is 4#<true if price is 4#-false if price is 4#<true if price is 4#-false if price is 4#4true if price is 4#4false if price is 4#<true if price is 4#4false if price is 4#Ptrue if price is 4#<false if price is 4#=true if price is 4#<false if price is <#=,

XPath !"am#les Let5s try to learn some basic XPath synta" by loo,ing at some e"am#les. &he XML !"am#le 2ocument 'e will use the following XML document in the e%amples below# :boo+s#%ml:: 78%ml version9:,#-: encoding9: &;*<<=4*,:8> 7boo+store> 7boo+ category9:);;@ NO:> 7title lang9:en:>Averyday talian7/title> 7author>Oiada Le Laurentiis7/author> 7year>1--=7/year> 7price>(-#--7/price> 7/boo+> 7boo+ category9:)H LL2AN:> 7title lang9:en:>Harry $otter7/title> 7author>? @# 2owling7/author> 7year>1--=7/year> 7price>14#447/price> 7/boo+> 7boo+ category9:'AB:>

7title lang9:en:>X.uery @ic+ &tart7/title> 7author>?ames McOovern7/author> 7author>$er Bothner7/author> 7author>@urt )agle7/author> 7author>?ames Linn7/author> 7author>Qaidyanathan Nagara!an7/author> 7year>1--(7/year> 7price>N4#447/price> 7/boo+> 7boo+ category9:'AB:> 7title lang9:en:>Learning XML7/title> 7author>Ari+ T# 2ay7/author> 7year>1--(7/year> 7price>(4#4=7/price> 7/boo+> 7/boo+store> Qiew the :boo+s#%ml: file in your browser# Selecting )odes Rnfortunately" there are different ways of dealing with XML and X$ath in nternet A%plorer based browsers and other browsers 5li+e MoDilla based browsers6# n our e%amples we have included code that should wor+ with most ma!or browsers# Select nodes for *nternet !"#lorer based browsers Rsing the Microsoft XMLL;M ob!ect to load the XML document and the selectNodes56 method to select nodes from the XML document: %mlLoc9new BctiveX;b!ect5:Microsoft#XMLL;M:6C %mlLoc#async9falseC %mlLoc#load5:boo+s#%ml:6C %mlLoc#selectNodes5xpath6C Select nodes for other browsers Rsing the implementation56 method of the document ob!ect to load the XML document and the evaluate56 method to select nodes from the XML document: %mlLoc9document#implementation#createLocument5::"::"null6C %mlLoc#async9falseC %mlLoc#load5:boo+s#%ml:6C document#evaluate5xpath" %mlLoc" null" X$ath2esult#BN0ST0$A"null6C Select titles from all boo, )odes The following path synta% selects all the title nodes: /boo+store/boo+/title Try it yourself# Select the title of the first boo, )ode The following path synta% selects only the title of the first boo+ node under the boo+store element: /boo+store/boo+H,I/title Try it yourself There is a problem with this# The e%ample above shows different results in A and other browsers# A= and later has implemented that H-I should be the first node" but according to the '() standard it should have been H,IGG A Wor,around6

To solve the H-I and H,I problem in A=M" you can set the &electionLanguage to X$ath# The following path synta% selects only the title of the first boo+ node under the boo+store element: xml#set$roperty5:&electionLanguage:":X$ath:6C xml#selectNodes5:/boo+store/boo+H,I/title:6C Try it yourself Select the #rices The following path synta% selects the te%t from all the price nodes: /boo+store/boo+/price/te%t56 Try it yourself# Selecting #rice )odes with Price7'8 The following path synta% selects all the price nodes with a price higher than (=: /boo+store/boo+Hprice>(=I/price Try it yourself# Selecting title )odes with Price7'8 The following path synta% selects all the title nodes with a price higher than (=: /boo+store/boo+Hprice>(=I/title Try it yourself# !"am#le (ode to chech the above document7html> 7body> 7script type9:te%t/!avascript:> function loadXMLLoc5fname6 T var %mlLocC // code for A if 5window#BctiveX;b!ect6 T %mlLoc9new BctiveX;b!ect5:Microsoft#XMLL;M:6C U // code for MoDilla" Virefo%" ;pera" etc# else if 5document#implementation WW document#implementation#createLocument6 T %mlLoc9document#implementation#createLocument5::"::"null6C U else T alert5E0our browser cannot handle this scriptE6C U %mlLoc#async9falseC %mlLoc#load5fname6C return5%mlLoc6C U %ml9loadXMLLoc5:boo+s#%ml:6C path9:/boo+store/boo+H,I/title:C // code for A

if 5window#BctiveX;b!ect6 T var nodes9%ml#selectNodes5path6C for 5i9-Ci7nodes#lengthCiMM6 T document#write5nodesHiI#childNodesH-I#nodeQalue6C document#write5:7br />:6C U U // code for MoDilla" Virefo%" ;pera" etc# else if 5document#implementation WW document#implementation#createLocument6 T var nodes9document#evaluate5path" %ml" null" X$ath2esult#BN0ST0$A"null6C var result9nodes#iterateNe%t56C while5result6 T document#write5result#childNodesH-I#nodeQalue6C document#write5:7br />:6C result9nodes#iterateNe%t56C U U 7/script> 7/body> 7/html>

You might also like