SlideShare a Scribd company logo
1 of 17
Download to read offline
Prepared By: Dr.Saranya.K.G 1
XPath
What is XPath?
• XPath is a syntax used for selecting parts of an XML
document
• XPath is used to navigate through elements and
attributes in an XML document.
• XPath is almost a small programming language; it has
functions, tests, and expressions
• XPath is a language for finding information in an
XML document.
Prepared By: Dr.Saranya.K.G 2
• XPath contains a library of standard functions
• XPath is a major element in XSLT
• XPath is a W3C standard
XPath Path Expressions
• XPath uses path expressions to select nodes or node-
sets in an XML document.
XPath Standard Functions
• XPath includes over 100 built-in functions.
• There are functions for string values, numeric values,
date and time comparison, node, sequence
manipulation, Boolean values, and more.
Prepared By: Dr.Saranya.K.G 3
XPath Operators
• An XPath expression returns either a node-set, a
string, a Boolean, or a number.
Look at the following XML document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="en">Harry</title>
<author>J K. R</author>
<year>2011</year>
<price>209.99</price>
</book>
</bookstore>
XML documents are treated as trees of nodes. The
topmost element of the tree is called the root
element.
Prepared By: Dr.Saranya.K.G 4
Selecting Nodes
• XPath uses path expressions to select nodes in an XML
document. The node is selected by following a path or steps.
The most useful path expressions are listed below:
In the table below listed some path expressions and
the result of the expressions:
Prepared By: Dr.Saranya.K.G 5
Predicates
• Predicates are used to find a specific node or a node
that contains a specific value.
• Predicates are always embedded in square brackets.
In the table below listed some path expressions with
predicates and the result of the expressions:
Prepared By: Dr.Saranya.K.G 6
Selecting Unknown Nodes
• XPath wildcards can be used to select unknown XML
elements.
• In the table below we have listed some path expressions and
the result of the expressions:
Selecting Several Paths
• By using the | operator in an XPath expression you
can select several paths.
• In the table below we have listed some path
expressions and the result of the expressions:
Prepared By: Dr.Saranya.K.G 7
XPath Axes
An axis defines a node-set relative to the current node.
Examples
Prepared By: Dr.Saranya.K.G 8
Below is a list of the operators that can be used in
XPath expressions:
Terminology
<library>
<book>
<chapter>
</chapter>
<chapter>
<section>
<paragraph/>
<paragraph/>
</section>
</chapter>
</book>
</library>
• library is the parent of book; book is the
parent of the two chapters
• The two chapters are the children of book,
and the section is the child of the second
chapter
• The two chapters of the book are siblings
(they have the same parent)
• library, book, and the second chapter are
the ancestors of the section
• The two chapters, the section, and the two
paragraphs are the descendents of the book
Prepared By: Dr.Saranya.K.G 9
Brackets and last()
• A number in brackets selects a particular matching child
(counting starts from 1, except in Internet Explorer)
– Example: /library/book[1] selects the first book of the library
– Example: //chapter/section[2] selects the second section of every
chapter in the XML document
– Example: //book/chapter[1]/section[2]
– Only matching elements are counted; for example, if a book has both
sections and exercises, the latter are ignored when counting sections
• The function last() in brackets selects the last matching child
– Example: /library/book/chapter[last()]
• You can even do simple arithmetic
– Example: /library/book/chapter[last()-1]
Stars
• A star, or asterisk, is a “wild card”--it means “all
the elements at this level”
– Example: /library/book/chapter/* selects every
child of every chapter of every book in the library
– Example: //book/* selects every child of every book
(chapters, tableOfContents, index, etc.)
– Example: /*/*/*/paragraph selects every paragraph
that has exactly three ancestors
– Example: //* selects every element in the entire
document
Prepared By: Dr.Saranya.K.G 10
Attributes I
• You can select attributes by themselves, or elements that have
certain attributes
– Remember: an attribute consists of a name-value pair, for example in
<chapter num="5">, the attribute is named num
– To choose the attribute itself, prefix the name with @
– Example: @num will choose every attribute named num
– Example: //@* will choose every attribute, everywhere in the
document
• To choose elements that have a given attribute, put the
attribute name in square brackets
– Example: //chapter[@num] will select every chapter element
(anywhere in the document) that has an attribute named num
Attributes II
• //chapter[@num] selects every chapter
element with an attribute num
• //chapter[not(@num)] selects every chapter
element that does not have a num attribute
• //chapter[@*] selects every chapter element
that has any attribute
• //chapter[not(@*)] selects every chapter
element with no attributes
Prepared By: Dr.Saranya.K.G 11
Values of attributes
• //chapter[@num='3'] selects every chapter element
with an attribute num with value 3
• //chapter[not(@num)] selects every chapter element
that does not have a num attribute
• //chapter[@*] selects every chapter element that has
any attribute
• //chapter[not(@*)] selects every chapter element with
no attributes
• The normalize-space() function can be used to remove
leading and trailing spaces from a value before comparison
– Example: //chapter[normalize-space(@num)="3"]
Axes
• An axis (plural axes) is a set of nodes relative to a
given node; X::Y means “choose Y from the X
axis”
– self:: is the set of current nodes (not too useful)
• self::node() is the current node
– child:: is the default, so /child::X is the same as /X
– parent:: is the parent of the current node
– ancestor:: is all ancestors of the current node, up to and including
the root
– descendant:: is all descendants of the current node
(Note: never contains attribute or namespace nodes)
– preceding:: is everything before the current node in the entire
XML document
– following:: is everything after the current node in the entire XML
document
Prepared By: Dr.Saranya.K.G 12
Axes (outline view)
<library>
<book>
<chapter/>
<chapter>
<section>
<paragraph/>
<paragraph/>
</section>
</chapter>
<chapter/>
</book>
<book/>
</library>
• //chapter[2]/self::*
• //chapter[2]/preceding::*
• //chapter[2]/following::*
• //chapter[2]/ancestor::*
• //chapter[2]/descendant::*
Starting from a given node, the self, preceding, following,
ancestor, and descendant axes form a partition of all the nodes
(if we ignore attribute and namespace nodes)
Axes (tree view)
• Starting from a given node, the self, ancestor, descendant ,
preceding, and following axes form a partition of all the nodes (if we
ignore attribute and namespace nodes)
paragraph[1] paragraph[2]
section[1]
chapter[2]chapter[1] chapter[3]
book[1] book[2]
library
self
ancestor
descendant
preceding
following
Prepared By: Dr.Saranya.K.G 13
Axis examples
• //book/descendant::* is all descendants of every book
• //book/descendant::section is all section descendants
of every book
• //parent::* is every element that is a parent, i.e., is not a
leaf
• //section/parent::* is every parent of a section element
• //parent::chapter is every chapter that is a parent, i.e.,
has children
• /library/book[3]/following::* is everything after the
third book in the library
More axes
• ancestor-or-self:: ancestors plus the current node
• descendant-or-self:: descendants plus the current node
• attribute:: is all attributes of the current node
• namespace:: is all namespace nodes of the current node
• preceding:: is everything before the current node in the
entire XML document
• following-sibling:: is all siblings after the current node
• Note: preceding-sibling:: and following-sibling:: do not
apply to attribute nodes or namespace nodes
Prepared By: Dr.Saranya.K.G 14
Abbreviations for axes
(none) is the same as child::
@ is the same as attribute::
. is the same as self::node()
.//X is the same as self::node()/descendant-or-self::node()/child::X
.. is the same as parent::node()
../X is the same as parent::node()/child::X
// is the same as /descendant-or-self::node()/
//X is the same as /descendant-or-self::node()/child::X
Arithmetic expressions
+ add
- subtract
* multiply
div (not /) divide
mod modulo (remainder)
Prepared By: Dr.Saranya.K.G 15
Equality tests
• = “equals” (Notice it’s not ==)
• != “not equals”
• But it’s not that simple!
– value = node-set will be true if the node-set contains
any node with a value that matches value
– value != node-set will be true if the node-set contains
any node with a value that does not match value
• Hence,
– value = node-set and value != node-set may both be
true at the same time!
Other boolean operators
• and (infix operator)
• or (infix operator)
– Example: count = 0 or count = 1
• not() (function)
• The following are used for numerical comparisons only:
• < “less than” Some places may require &lt;
• <= “less than Some places may require &lt;=
or equal to”
• > “greater than” Some places may require &gt;
• >= “greater than Some places may require &gt;=
or equal to”
Prepared By: Dr.Saranya.K.G 16
Some XPath functions
• XPath contains a number of functions on node sets,
numbers, and strings; here are a few of them:
– count(elem) counts the number of selected elements
• Example: //chapter[count(section)=1] selects chapters with
exactly two section children
– name() returns the name of the element
• Example: //*[name()='section'] is the same as //section
– starts-with(arg1, arg2) tests if arg1 starts with arg2
• Example: //*[starts-with(name(), 'sec']
– contains(arg1, arg2) tests if arg1 contains arg2
• Example: //*[contains(name(), 'ect']
Examples
• 1.xml 1.xsl
• 2.xml 2.xsl
• 3.xml 3.xsl
• 4.xml 4.xsl XPath.doc
• 5.xml 5.xsl
Screen shots.doc
Prepared By: Dr.Saranya.K.G 17
• XPath is a straightforward declarative language for selecting
particular subsets of nodes from an XML document
• XPath has a syntax for the selection of nodes, unknown
elements, branches, multiple paths and attributes
• XPath queries are written in expressions which can returns
doubles or strings and allow for arithmetic and relational
operations on data types
• Location paths are an important subset of expressions, they
consist of one or more location step which in turn consists of
an axis, a node test, and (optionally) one or more predicates
• XPath also provides a core set of functions for operating on
the data types

More Related Content

What's hot

Ch08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and ArraysCh08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
 
070517 Jena
070517 Jena070517 Jena
070517 Jena
yuhana
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
Ayush Mishra
 

What's hot (20)

Introduction to XPath
Introduction to XPathIntroduction to XPath
Introduction to XPath
 
XML on SQL Server
XML on SQL ServerXML on SQL Server
XML on SQL Server
 
XPATH
XPATHXPATH
XPATH
 
XPath - XML Path Language
XPath - XML Path LanguageXPath - XML Path Language
XPath - XML Path Language
 
Ch08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and ArraysCh08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and Arrays
 
Xml session
Xml sessionXml session
Xml session
 
SQL/XML on Oracle
SQL/XML on OracleSQL/XML on Oracle
SQL/XML on Oracle
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Text processing by Rj
Text processing by RjText processing by Rj
Text processing by Rj
 
Java
JavaJava
Java
 
05io
05io05io
05io
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Session 4
Session 4Session 4
Session 4
 
070517 Jena
070517 Jena070517 Jena
070517 Jena
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java and XML
Java and XMLJava and XML
Java and XML
 
8. String
8. String8. String
8. String
 
Sax parser
Sax parserSax parser
Sax parser
 

Similar to Xpath1

Using elasticsearch with rails
Using elasticsearch with railsUsing elasticsearch with rails
Using elasticsearch with rails
Tom Z Zeng
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar
 

Similar to Xpath1 (20)

Xpath
XpathXpath
Xpath
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
X path
X pathX path
X path
 
02_Xpath.pdf
02_Xpath.pdf02_Xpath.pdf
02_Xpath.pdf
 
Querring xml with xpath
Querring xml with xpath Querring xml with xpath
Querring xml with xpath
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 
Xpath.ppt
Xpath.pptXpath.ppt
Xpath.ppt
 
03 x files
03 x files03 x files
03 x files
 
X FILES
X FILESX FILES
X FILES
 
Using elasticsearch with rails
Using elasticsearch with railsUsing elasticsearch with rails
Using elasticsearch with rails
 
XPATH_XSLT-1.pptx
XPATH_XSLT-1.pptxXPATH_XSLT-1.pptx
XPATH_XSLT-1.pptx
 
Xpath.pdf
Xpath.pdfXpath.pdf
Xpath.pdf
 
Python_Unit_III.pptx
Python_Unit_III.pptxPython_Unit_III.pptx
Python_Unit_III.pptx
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
XML Technologies
XML TechnologiesXML Technologies
XML Technologies
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
 

More from Dr.Saranya K.G (12)

complete web service1.ppt
complete web service1.pptcomplete web service1.ppt
complete web service1.ppt
 
Introduction to Web Services Protocols.ppt
Introduction to Web Services Protocols.pptIntroduction to Web Services Protocols.ppt
Introduction to Web Services Protocols.ppt
 
1.Usability Engineering.pptx
1.Usability Engineering.pptx1.Usability Engineering.pptx
1.Usability Engineering.pptx
 
CSSE375-03-framework.ppt
CSSE375-03-framework.pptCSSE375-03-framework.ppt
CSSE375-03-framework.ppt
 
SQA.ppt
SQA.pptSQA.ppt
SQA.ppt
 
Neo4 j
Neo4 jNeo4 j
Neo4 j
 
Xquery1
Xquery1Xquery1
Xquery1
 
Xsl xslt
Xsl  xsltXsl  xslt
Xsl xslt
 
Converting dt ds to xml schemas
Converting dt ds to xml schemasConverting dt ds to xml schemas
Converting dt ds to xml schemas
 
Dtd
DtdDtd
Dtd
 
Xml schema
Xml schemaXml schema
Xml schema
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
 

Recently uploaded

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Recently uploaded (20)

DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 

Xpath1

  • 1. Prepared By: Dr.Saranya.K.G 1 XPath What is XPath? • XPath is a syntax used for selecting parts of an XML document • XPath is used to navigate through elements and attributes in an XML document. • XPath is almost a small programming language; it has functions, tests, and expressions • XPath is a language for finding information in an XML document.
  • 2. Prepared By: Dr.Saranya.K.G 2 • XPath contains a library of standard functions • XPath is a major element in XSLT • XPath is a W3C standard XPath Path Expressions • XPath uses path expressions to select nodes or node- sets in an XML document. XPath Standard Functions • XPath includes over 100 built-in functions. • There are functions for string values, numeric values, date and time comparison, node, sequence manipulation, Boolean values, and more.
  • 3. Prepared By: Dr.Saranya.K.G 3 XPath Operators • An XPath expression returns either a node-set, a string, a Boolean, or a number. Look at the following XML document: <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry</title> <author>J K. R</author> <year>2011</year> <price>209.99</price> </book> </bookstore> XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
  • 4. Prepared By: Dr.Saranya.K.G 4 Selecting Nodes • XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below: In the table below listed some path expressions and the result of the expressions:
  • 5. Prepared By: Dr.Saranya.K.G 5 Predicates • Predicates are used to find a specific node or a node that contains a specific value. • Predicates are always embedded in square brackets. In the table below listed some path expressions with predicates and the result of the expressions:
  • 6. Prepared By: Dr.Saranya.K.G 6 Selecting Unknown Nodes • XPath wildcards can be used to select unknown XML elements. • In the table below we have listed some path expressions and the result of the expressions: Selecting Several Paths • By using the | operator in an XPath expression you can select several paths. • In the table below we have listed some path expressions and the result of the expressions:
  • 7. Prepared By: Dr.Saranya.K.G 7 XPath Axes An axis defines a node-set relative to the current node. Examples
  • 8. Prepared By: Dr.Saranya.K.G 8 Below is a list of the operators that can be used in XPath expressions: Terminology <library> <book> <chapter> </chapter> <chapter> <section> <paragraph/> <paragraph/> </section> </chapter> </book> </library> • library is the parent of book; book is the parent of the two chapters • The two chapters are the children of book, and the section is the child of the second chapter • The two chapters of the book are siblings (they have the same parent) • library, book, and the second chapter are the ancestors of the section • The two chapters, the section, and the two paragraphs are the descendents of the book
  • 9. Prepared By: Dr.Saranya.K.G 9 Brackets and last() • A number in brackets selects a particular matching child (counting starts from 1, except in Internet Explorer) – Example: /library/book[1] selects the first book of the library – Example: //chapter/section[2] selects the second section of every chapter in the XML document – Example: //book/chapter[1]/section[2] – Only matching elements are counted; for example, if a book has both sections and exercises, the latter are ignored when counting sections • The function last() in brackets selects the last matching child – Example: /library/book/chapter[last()] • You can even do simple arithmetic – Example: /library/book/chapter[last()-1] Stars • A star, or asterisk, is a “wild card”--it means “all the elements at this level” – Example: /library/book/chapter/* selects every child of every chapter of every book in the library – Example: //book/* selects every child of every book (chapters, tableOfContents, index, etc.) – Example: /*/*/*/paragraph selects every paragraph that has exactly three ancestors – Example: //* selects every element in the entire document
  • 10. Prepared By: Dr.Saranya.K.G 10 Attributes I • You can select attributes by themselves, or elements that have certain attributes – Remember: an attribute consists of a name-value pair, for example in <chapter num="5">, the attribute is named num – To choose the attribute itself, prefix the name with @ – Example: @num will choose every attribute named num – Example: //@* will choose every attribute, everywhere in the document • To choose elements that have a given attribute, put the attribute name in square brackets – Example: //chapter[@num] will select every chapter element (anywhere in the document) that has an attribute named num Attributes II • //chapter[@num] selects every chapter element with an attribute num • //chapter[not(@num)] selects every chapter element that does not have a num attribute • //chapter[@*] selects every chapter element that has any attribute • //chapter[not(@*)] selects every chapter element with no attributes
  • 11. Prepared By: Dr.Saranya.K.G 11 Values of attributes • //chapter[@num='3'] selects every chapter element with an attribute num with value 3 • //chapter[not(@num)] selects every chapter element that does not have a num attribute • //chapter[@*] selects every chapter element that has any attribute • //chapter[not(@*)] selects every chapter element with no attributes • The normalize-space() function can be used to remove leading and trailing spaces from a value before comparison – Example: //chapter[normalize-space(@num)="3"] Axes • An axis (plural axes) is a set of nodes relative to a given node; X::Y means “choose Y from the X axis” – self:: is the set of current nodes (not too useful) • self::node() is the current node – child:: is the default, so /child::X is the same as /X – parent:: is the parent of the current node – ancestor:: is all ancestors of the current node, up to and including the root – descendant:: is all descendants of the current node (Note: never contains attribute or namespace nodes) – preceding:: is everything before the current node in the entire XML document – following:: is everything after the current node in the entire XML document
  • 12. Prepared By: Dr.Saranya.K.G 12 Axes (outline view) <library> <book> <chapter/> <chapter> <section> <paragraph/> <paragraph/> </section> </chapter> <chapter/> </book> <book/> </library> • //chapter[2]/self::* • //chapter[2]/preceding::* • //chapter[2]/following::* • //chapter[2]/ancestor::* • //chapter[2]/descendant::* Starting from a given node, the self, preceding, following, ancestor, and descendant axes form a partition of all the nodes (if we ignore attribute and namespace nodes) Axes (tree view) • Starting from a given node, the self, ancestor, descendant , preceding, and following axes form a partition of all the nodes (if we ignore attribute and namespace nodes) paragraph[1] paragraph[2] section[1] chapter[2]chapter[1] chapter[3] book[1] book[2] library self ancestor descendant preceding following
  • 13. Prepared By: Dr.Saranya.K.G 13 Axis examples • //book/descendant::* is all descendants of every book • //book/descendant::section is all section descendants of every book • //parent::* is every element that is a parent, i.e., is not a leaf • //section/parent::* is every parent of a section element • //parent::chapter is every chapter that is a parent, i.e., has children • /library/book[3]/following::* is everything after the third book in the library More axes • ancestor-or-self:: ancestors plus the current node • descendant-or-self:: descendants plus the current node • attribute:: is all attributes of the current node • namespace:: is all namespace nodes of the current node • preceding:: is everything before the current node in the entire XML document • following-sibling:: is all siblings after the current node • Note: preceding-sibling:: and following-sibling:: do not apply to attribute nodes or namespace nodes
  • 14. Prepared By: Dr.Saranya.K.G 14 Abbreviations for axes (none) is the same as child:: @ is the same as attribute:: . is the same as self::node() .//X is the same as self::node()/descendant-or-self::node()/child::X .. is the same as parent::node() ../X is the same as parent::node()/child::X // is the same as /descendant-or-self::node()/ //X is the same as /descendant-or-self::node()/child::X Arithmetic expressions + add - subtract * multiply div (not /) divide mod modulo (remainder)
  • 15. Prepared By: Dr.Saranya.K.G 15 Equality tests • = “equals” (Notice it’s not ==) • != “not equals” • But it’s not that simple! – value = node-set will be true if the node-set contains any node with a value that matches value – value != node-set will be true if the node-set contains any node with a value that does not match value • Hence, – value = node-set and value != node-set may both be true at the same time! Other boolean operators • and (infix operator) • or (infix operator) – Example: count = 0 or count = 1 • not() (function) • The following are used for numerical comparisons only: • < “less than” Some places may require &lt; • <= “less than Some places may require &lt;= or equal to” • > “greater than” Some places may require &gt; • >= “greater than Some places may require &gt;= or equal to”
  • 16. Prepared By: Dr.Saranya.K.G 16 Some XPath functions • XPath contains a number of functions on node sets, numbers, and strings; here are a few of them: – count(elem) counts the number of selected elements • Example: //chapter[count(section)=1] selects chapters with exactly two section children – name() returns the name of the element • Example: //*[name()='section'] is the same as //section – starts-with(arg1, arg2) tests if arg1 starts with arg2 • Example: //*[starts-with(name(), 'sec'] – contains(arg1, arg2) tests if arg1 contains arg2 • Example: //*[contains(name(), 'ect'] Examples • 1.xml 1.xsl • 2.xml 2.xsl • 3.xml 3.xsl • 4.xml 4.xsl XPath.doc • 5.xml 5.xsl Screen shots.doc
  • 17. Prepared By: Dr.Saranya.K.G 17 • XPath is a straightforward declarative language for selecting particular subsets of nodes from an XML document • XPath has a syntax for the selection of nodes, unknown elements, branches, multiple paths and attributes • XPath queries are written in expressions which can returns doubles or strings and allow for arithmetic and relational operations on data types • Location paths are an important subset of expressions, they consist of one or more location step which in turn consists of an axis, a node test, and (optionally) one or more predicates • XPath also provides a core set of functions for operating on the data types