SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
Querying XML with XPATH
Basics and understanding
Malintha Adikari
Software Engineer
What is Xpath
● XPath is an XML query language.
● XPath is a building block for other XML
● XPath is a syntax for defining parts of an XML document
● XPath uses path expressions to navigate in XML
documents
● XPath contains a library of standard functions
● XPath is a major element in XSLT
Xpath is All About
● Nodes
● element
● attribute
● text
● namespace
● processing-instruction
● comment
● document (root) nodes
Relationship Of Nodes
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Relationship Of Nodes
● Parent: Each element and attribute
has one parent.
● Children: Element nodes may have
zero, one or more children.
● Siblings: Nodes that have the same
parent.
● Ancestors: A node's parent, parent's
parent, etc.
● Descendants: A node's children,
children's children, etc.
</bookstore>
<book>
<title>Harry Potter</title>
<author>J K.
Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Basics -Slashes (/)
● A path that begins with a / represents an absolute path, starting from the top
of the document
○ Example: /email/message/header/from
○ Note that even an absolute path can select more than one element
○ A slash by itself means “the whole document”
● A path that does not begin with a / represents a path starting from the current
element
○ Example: header/from
● A path that begins with // can start from anywhere in the document
○ Example: //header/from selects every element from that is a child of an element header
○ This can be expensive, since it involves searching the entire document
Basics -Brackets and last()
● A number in brackets selects a particular matching child
○ 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]
Basics -Wild cards
● 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
Get This XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book isbn= “111111” cat=“fiction”>
<title lang=“chn”>Harry Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
Selecting element of XML- Basics
Node Selection
<?xml version="1.0" encoding="ISO-8859-
1"?>
<bookstore>
<book isbn= <book isbn= 111111 cat=
fiction > “111111” cat=“fiction”>
<title lang=“chn”>Harry
Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning
XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to
Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
Node Selection Solutions
Xpath Axes
An axis defines a node-set relative to the current node.
• self‐‐the context node itself
• child‐‐the children of the context node
• descendant‐-all descendants (children+)
• parent‐‐the parent (empty if at the root)
• ancestor‐‐all ancestors from the parent to the root
• descendant descendant‐or‐self ‐‐the union of descendant descendant and self
• ancestor‐or‐self ‐‐the union of ancestor and self
• following‐sibling‐‐siblings to the right
• preceding‐sibling‐‐siblings to the left
• following‐‐all following nodes in the document, excluding descendants
• attribute‐‐the attributes of the context node
Xpath Axes sample
<?xml version="1.0" encoding="ISO-8859-
1"?>
<bookstore>
<book isbn= “111111 cat= “fiction”>
<title lang=“chn”>Harry
Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning
XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to
Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
Xpath Operators
Predicates
[position() op #],[last()]
–op: =, !=, <, >, <=, >=
–test position among siblings
•[attribute::name [attribute::name op “value ]"
–op: =, !=, <, >, <=, >=
–test equality equality of an attribute attribute
•[axis:nodeSelector]
–test pattern
Predicates Samples
<?xml version="1.0" encoding="ISO-8859-
1"?>
<bookstore>
<book isbn=“111111” cat=“fiction”>
<title lang=“chn”>Harry
Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning
XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to
Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
XPath Standard Functions
● number position()
Return the position of the context node among the list of nodes that
are currently being evaluated.
● count()
Return the number of nodes in the argument node-set
number count(node-set)
● number last()
Return the index of the last node in the list that is currently being
evaluated
http://www.w3schools.com/xpath/xpath_functions.asp
XPath: exercise
1. Find the title and price of non fiction
books with a price more than 50 USD.
2. Find average price of textbooks.
3. Find the titles of textbooks on XML.
<?xml version="1.0" encoding="ISO-8859-
1"?>
<bookstore>
<book isbn=“111111” cat=“fiction”>
<title lang=“chn”>Harry
Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning
XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to
Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
Answers
1. Find the title and price of non “COOKING” books with a price more than 25
USD.
/bookstore/book[attribute::category!="COOKING" and price > 25]/title/text() | /bookstore/book
[attribute::category!="COOKING" and price > 25]/price/text())
2. Find average price of “WEB” books.
sum(//book[attribute::category="WEB"]/price/text()) div count(//book[attribute::category="WEB"])
3. Find the titles of textbooks on XML.
//book[@category="textbook" and contains(title,"XML")]/title/text()
More Exercises
● Setup Eclipse with plugins for xml, XSD and XSLT
● Try Xpath samples at
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
● Take sample xml and try out Xpath functions
http://www.w3schools.com/xpath/xpath_functions.asp
● Take automation.xml from following svn location and try to implement xsd for
that. We will discuss this in next session
https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/platform-
integration/test-automation-framework-2/org.wso2.carbon.automation.engine/4.3.0
/src/main/resources/automation.xml
Questions?
Contact us !

Contenu connexe

Tendances (20)

3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
XML
XMLXML
XML
 
XSLT
XSLTXSLT
XSLT
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Apuntes de XSD
Apuntes de XSDApuntes de XSD
Apuntes de XSD
 
Xslt
XsltXslt
Xslt
 
Xml dtd
Xml dtd Xml dtd
Xml dtd
 
Ejercicios de XSD
Ejercicios de XSDEjercicios de XSD
Ejercicios de XSD
 
Html intro
Html introHtml intro
Html intro
 
XSLT presentation
XSLT presentationXSLT presentation
XSLT presentation
 
Html project
Html projectHtml project
Html project
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
 
Xslt elements
Xslt elementsXslt elements
Xslt elements
 
Html training slide
Html training slideHtml training slide
Html training slide
 
Xml
XmlXml
Xml
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Html Intro2
Html Intro2Html Intro2
Html Intro2
 
XPATH
XPATHXPATH
XPATH
 
XML Introduction
XML IntroductionXML Introduction
XML Introduction
 

Similaire à Querring xml with xpath (20)

03 x files
03 x files03 x files
03 x files
 
X path
X pathX path
X path
 
X path
X pathX path
X path
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xpath1
Xpath1Xpath1
Xpath1
 
Xpath
XpathXpath
Xpath
 
Xpath.ppt
Xpath.pptXpath.ppt
Xpath.ppt
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
02_Xpath.pdf
02_Xpath.pdf02_Xpath.pdf
02_Xpath.pdf
 
Xml session
Xml sessionXml session
Xml session
 
XML XSLT
XML XSLTXML XSLT
XML XSLT
 
Chapter4
Chapter4Chapter4
Chapter4
 
XML
XMLXML
XML
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
Xml nisha dwivedi
Xml nisha dwivediXml nisha dwivedi
Xml nisha dwivedi
 
XML
XMLXML
XML
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 
Xml
XmlXml
Xml
 
Xquery basics tutorial
Xquery basics  tutorialXquery basics  tutorial
Xquery basics tutorial
 
Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
 

Dernier

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Dernier (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

Querring xml with xpath

  • 1. Querying XML with XPATH Basics and understanding Malintha Adikari Software Engineer
  • 2. What is Xpath ● XPath is an XML query language. ● XPath is a building block for other XML ● XPath is a syntax for defining parts of an XML document ● XPath uses path expressions to navigate in XML documents ● XPath contains a library of standard functions ● XPath is a major element in XSLT
  • 3. Xpath is All About ● Nodes ● element ● attribute ● text ● namespace ● processing-instruction ● comment ● document (root) nodes
  • 4. Relationship Of Nodes <bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
  • 5. Relationship Of Nodes ● Parent: Each element and attribute has one parent. ● Children: Element nodes may have zero, one or more children. ● Siblings: Nodes that have the same parent. ● Ancestors: A node's parent, parent's parent, etc. ● Descendants: A node's children, children's children, etc. </bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
  • 6. Basics -Slashes (/) ● A path that begins with a / represents an absolute path, starting from the top of the document ○ Example: /email/message/header/from ○ Note that even an absolute path can select more than one element ○ A slash by itself means “the whole document” ● A path that does not begin with a / represents a path starting from the current element ○ Example: header/from ● A path that begins with // can start from anywhere in the document ○ Example: //header/from selects every element from that is a child of an element header ○ This can be expensive, since it involves searching the entire document
  • 7. Basics -Brackets and last() ● A number in brackets selects a particular matching child ○ 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]
  • 8. Basics -Wild cards ● 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
  • 9. Get This XML <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book isbn= “111111” cat=“fiction”> <title lang=“chn”>Harry Potter</title> <price unit=“us”>79.99</price> </book> <book isbn=“222222” cat=“textbook”> <title lang=“eng”>Learning XML</title> <price unit=“us”>69.95</price> </book> <book isbn "333333" cat "textbook"> <title lang="eng">Intro. to Databases</title> <price unit="usd">39.00</price> </book> </bookstore>
  • 10. Selecting element of XML- Basics
  • 11. Node Selection <?xml version="1.0" encoding="ISO-8859- 1"?> <bookstore> <book isbn= <book isbn= 111111 cat= fiction > “111111” cat=“fiction”> <title lang=“chn”>Harry Potter</title> <price unit=“us”>79.99</price> </book> <book isbn=“222222” cat=“textbook”> <title lang=“eng”>Learning XML</title> <price unit=“us”>69.95</price> </book> <book isbn "333333" cat "textbook"> <title lang="eng">Intro. to Databases</title> <price unit="usd">39.00</price> </book> </bookstore>
  • 13. Xpath Axes An axis defines a node-set relative to the current node. • self‐‐the context node itself • child‐‐the children of the context node • descendant‐-all descendants (children+) • parent‐‐the parent (empty if at the root) • ancestor‐‐all ancestors from the parent to the root • descendant descendant‐or‐self ‐‐the union of descendant descendant and self • ancestor‐or‐self ‐‐the union of ancestor and self • following‐sibling‐‐siblings to the right • preceding‐sibling‐‐siblings to the left • following‐‐all following nodes in the document, excluding descendants • attribute‐‐the attributes of the context node
  • 14. Xpath Axes sample <?xml version="1.0" encoding="ISO-8859- 1"?> <bookstore> <book isbn= “111111 cat= “fiction”> <title lang=“chn”>Harry Potter</title> <price unit=“us”>79.99</price> </book> <book isbn=“222222” cat=“textbook”> <title lang=“eng”>Learning XML</title> <price unit=“us”>69.95</price> </book> <book isbn "333333" cat "textbook"> <title lang="eng">Intro. to Databases</title> <price unit="usd">39.00</price> </book> </bookstore>
  • 16. Predicates [position() op #],[last()] –op: =, !=, <, >, <=, >= –test position among siblings •[attribute::name [attribute::name op “value ]" –op: =, !=, <, >, <=, >= –test equality equality of an attribute attribute •[axis:nodeSelector] –test pattern
  • 17. Predicates Samples <?xml version="1.0" encoding="ISO-8859- 1"?> <bookstore> <book isbn=“111111” cat=“fiction”> <title lang=“chn”>Harry Potter</title> <price unit=“us”>79.99</price> </book> <book isbn=“222222” cat=“textbook”> <title lang=“eng”>Learning XML</title> <price unit=“us”>69.95</price> </book> <book isbn "333333" cat "textbook"> <title lang="eng">Intro. to Databases</title> <price unit="usd">39.00</price> </book> </bookstore>
  • 18. XPath Standard Functions ● number position() Return the position of the context node among the list of nodes that are currently being evaluated. ● count() Return the number of nodes in the argument node-set number count(node-set) ● number last() Return the index of the last node in the list that is currently being evaluated http://www.w3schools.com/xpath/xpath_functions.asp
  • 19. XPath: exercise 1. Find the title and price of non fiction books with a price more than 50 USD. 2. Find average price of textbooks. 3. Find the titles of textbooks on XML. <?xml version="1.0" encoding="ISO-8859- 1"?> <bookstore> <book isbn=“111111” cat=“fiction”> <title lang=“chn”>Harry Potter</title> <price unit=“us”>79.99</price> </book> <book isbn=“222222” cat=“textbook”> <title lang=“eng”>Learning XML</title> <price unit=“us”>69.95</price> </book> <book isbn "333333" cat "textbook"> <title lang="eng">Intro. to Databases</title> <price unit="usd">39.00</price> </book> </bookstore>
  • 20. Answers 1. Find the title and price of non “COOKING” books with a price more than 25 USD. /bookstore/book[attribute::category!="COOKING" and price > 25]/title/text() | /bookstore/book [attribute::category!="COOKING" and price > 25]/price/text()) 2. Find average price of “WEB” books. sum(//book[attribute::category="WEB"]/price/text()) div count(//book[attribute::category="WEB"]) 3. Find the titles of textbooks on XML. //book[@category="textbook" and contains(title,"XML")]/title/text()
  • 21. More Exercises ● Setup Eclipse with plugins for xml, XSD and XSLT ● Try Xpath samples at http://www.zvon.org/xxl/XPathTutorial/General/examples.html ● Take sample xml and try out Xpath functions http://www.w3schools.com/xpath/xpath_functions.asp ● Take automation.xml from following svn location and try to implement xsd for that. We will discuss this in next session https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/platform- integration/test-automation-framework-2/org.wso2.carbon.automation.engine/4.3.0 /src/main/resources/automation.xml