SlideShare a Scribd company logo
Xpath & Xquery
1
XQuery
● XQuery is an active programming language, which is used to
interact with XML data groups.
● It is primarily used to extract, transform, and combine XML
documents.
● It can read and write the data in the database which is used in
software and services integration for making analysis reports.
2
Example 1: Basic Query
An XML document with information
about books:
<library>
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<price>19.99</price>
</book>
<book>
<title>Advanced XML</title>
<author>Jane Smith</author>
<price>29.99</price>
</book>
</library>
3
To extract all book titles using XQuery, you would write:
for $book in doc("library.xml")//book
return $book/title
In this example:
● doc("library.xml") refers to an XML document.
● //book is an XPath expression that selects all <book> elements in the
document.
● The for clause iterates over each <book> element and returns the title
of each book.
4
Example 2: Filtering and Returning Data
To extract book titles where the price is greater than 20:
for $book in doc("library.xml")//book
where $book/price > 20
return $book/title
5
Example 3: Using Variables
Variables can be defined within an XQuery:
let $minPrice := 20
for $book in doc("library.xml")//book
where $book/price > $minPrice
return $book/title
6
XQuery Components:
● Variables (let): Used to assign values that can be reused.
● For loops: Iterate over sequences of data.
● Conditionals (if/then/else): Allow for conditional branching in the query.
● Functions: XQuery supports defining functions for reusability.
● XPath Expressions: Used for selecting nodes or parts of the XML
document.
7
XQuery Example Use Cases:
1. Data Extraction: Extracting specific data from XML-based files.
2. Transformation: Transforming XML documents into other formats (e.g.,
JSON or HTML).
3. Data Aggregation: Aggregating XML data, such as counting elements or
summing up values.
8
Xpath
● XPath is an XML method language which is applied for node selection in
XML dataset using queries.
9
XPath Syntax
1. Absolute Path:
An absolute path starts from the root node of the XML document.
/library/book/title
This selects all <title> elements that are children of <book>, which is a child of
<library>. The path starts from the root node (denoted by /).
10
2. Relative Path:
A relative path starts from the current node and doesn’t necessarily start from the
root.
book/title
This would select <title> elements that are children of <book>, but it starts the
search from the current context node.
11
3. / and //:
● / selects from the root of the document.
● // selects nodes at any level, not necessarily directly under the current node.
//book
This selects all <book> elements in the document, regardless of their position in
the tree.
12
4. @ for Attributes:
You can select attributes using the @ symbol.
//book/@id
This selects the id attribute of all <book> elements in the document.
13
5. Using Predicates (Conditions):
Predicates are used to filter nodes based on certain conditions. These are
enclosed in square brackets [].
//book[price > 20]
This selects all <book> elements where the <price> child element has a value
greater than 20.
● We can also use predicates to select specific occurrences of elements:
//book[2]
This selects the second <book> element.
14
XPath Examples:
1. Select all books:
//book
2. Select the title of the second book:
//book[2]/title
3. Select the price of books where the price is greater than 20:
//book[price > 20]/price
15
4. Select the id attribute of the first book:
//book[1]/@id
5. Select the text content of all titles:
//book/title/text()
6. Select all books that are written by "John Doe":
//book[author = "John Doe"]
7. Select the author of the first book:
//book[1]/author
16

More Related Content

PPT
PDF
Querring xml with xpath
PDF
Xml parsing
PPTX
X path
PPTX
X path
DOC
PDF
PPTX
Querring xml with xpath
Xml parsing
X path
X path

Similar to Xpath & Xquery in XML documents for retreving data (20)

PPTX
Xml session
PPT
PPTX
Extracting data from xml
PPTX
PPTX
Xml transformation language
PPT
03 x files
PPT
PPT
PPTX
Unit 2
PPTX
XML and Web Services
PDF
Ch23 xml processing_with_java
PDF
Ch23
PPT
Xml 215-presentation
PPT
Xml nisha dwivedi
PPTX
Applied xml programming for microsoft 2
ODP
Xml processing in scala
ODP
Xml processing in scala
PPTX
The xml
PPTX
XPATH_XSLT-1.pptx
Xml session
Extracting data from xml
Xml transformation language
03 x files
Unit 2
XML and Web Services
Ch23 xml processing_with_java
Ch23
Xml 215-presentation
Xml nisha dwivedi
Applied xml programming for microsoft 2
Xml processing in scala
Xml processing in scala
The xml
XPATH_XSLT-1.pptx
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Lesson notes of climatology university.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Lesson notes of climatology university.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
202450812 BayCHI UCSC-SV 20250812 v17.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Ad

Xpath & Xquery in XML documents for retreving data

  • 2. XQuery ● XQuery is an active programming language, which is used to interact with XML data groups. ● It is primarily used to extract, transform, and combine XML documents. ● It can read and write the data in the database which is used in software and services integration for making analysis reports. 2
  • 3. Example 1: Basic Query An XML document with information about books: <library> <book> <title>XML for Beginners</title> <author>John Doe</author> <price>19.99</price> </book> <book> <title>Advanced XML</title> <author>Jane Smith</author> <price>29.99</price> </book> </library> 3
  • 4. To extract all book titles using XQuery, you would write: for $book in doc("library.xml")//book return $book/title In this example: ● doc("library.xml") refers to an XML document. ● //book is an XPath expression that selects all <book> elements in the document. ● The for clause iterates over each <book> element and returns the title of each book. 4
  • 5. Example 2: Filtering and Returning Data To extract book titles where the price is greater than 20: for $book in doc("library.xml")//book where $book/price > 20 return $book/title 5
  • 6. Example 3: Using Variables Variables can be defined within an XQuery: let $minPrice := 20 for $book in doc("library.xml")//book where $book/price > $minPrice return $book/title 6
  • 7. XQuery Components: ● Variables (let): Used to assign values that can be reused. ● For loops: Iterate over sequences of data. ● Conditionals (if/then/else): Allow for conditional branching in the query. ● Functions: XQuery supports defining functions for reusability. ● XPath Expressions: Used for selecting nodes or parts of the XML document. 7
  • 8. XQuery Example Use Cases: 1. Data Extraction: Extracting specific data from XML-based files. 2. Transformation: Transforming XML documents into other formats (e.g., JSON or HTML). 3. Data Aggregation: Aggregating XML data, such as counting elements or summing up values. 8
  • 9. Xpath ● XPath is an XML method language which is applied for node selection in XML dataset using queries. 9
  • 10. XPath Syntax 1. Absolute Path: An absolute path starts from the root node of the XML document. /library/book/title This selects all <title> elements that are children of <book>, which is a child of <library>. The path starts from the root node (denoted by /). 10
  • 11. 2. Relative Path: A relative path starts from the current node and doesn’t necessarily start from the root. book/title This would select <title> elements that are children of <book>, but it starts the search from the current context node. 11
  • 12. 3. / and //: ● / selects from the root of the document. ● // selects nodes at any level, not necessarily directly under the current node. //book This selects all <book> elements in the document, regardless of their position in the tree. 12
  • 13. 4. @ for Attributes: You can select attributes using the @ symbol. //book/@id This selects the id attribute of all <book> elements in the document. 13
  • 14. 5. Using Predicates (Conditions): Predicates are used to filter nodes based on certain conditions. These are enclosed in square brackets []. //book[price > 20] This selects all <book> elements where the <price> child element has a value greater than 20. ● We can also use predicates to select specific occurrences of elements: //book[2] This selects the second <book> element. 14
  • 15. XPath Examples: 1. Select all books: //book 2. Select the title of the second book: //book[2]/title 3. Select the price of books where the price is greater than 20: //book[price > 20]/price 15
  • 16. 4. Select the id attribute of the first book: //book[1]/@id 5. Select the text content of all titles: //book/title/text() 6. Select all books that are written by "John Doe": //book[author = "John Doe"] 7. Select the author of the first book: //book[1]/author 16