Predicate of XQuery in XML

In this article we will discuss about how to extract the limited Data from the XML using XQuery.
  • 2005

The XQuery uses predicate to extract the data from the XML document based on specific condition means it is used to extract the limited data from the XML Document. Along with the Path Expression we will use the predicates to extract the data from the XML Document. The following predicate will select all the employee element from the XML Document called "company.xml" under company element that have the salary element with a value greater than  55000.

Lets have a look on the following XML Document:

<?xml version="1.0" encoding="utf-8" ?>

<Company>

  <Employees>

    <id>1001</id>

    <name>Megha</name>

    <qualification>MCA</qualification>

    <age>23</age>

    <salary>50000</salary>

  </Employees>

  <Employees>

    <id>1002</id>

    <name>Richa</name>

    <qualification>MBA</qualification>

    <age>22</age>

    <salary>60000</salary>

  </Employees>

  <Employees>

    <id>1003</id>

    <name>Vishakha</name>

    <qualification>CA</qualification>

    <age>25</age>

    <salary>55000</salary>

  </Employees>

  <Employees>

    <id>1004</id>

    <name>Isha</name>

    <qualification>MCA</qualification>

    <age>25</age>

    <salary>70000</salary>

  </Employees>

</Company>

If we want to select the employee element with the salary greater than 55000 then we have to write the following XQuery against XML Document:

doc("company.xml")/Company/Employees[salary>55000]

The XQuery above will extract the following Data from XML Document:

<Employees>

    <id>1002</id>

    <name>Richa</name>

    <qualification>MBA</qualification>

    <age>22</age>

    <salary>60000</salary>

  </Employees>

<Employees>

    <id>1004</id>

    <name>Isha</name>

    <qualification>MCA</qualification>

    <age>25</age>

    <salary>70000</salary>

  </Employees>


In this output we have seen that the Employee element with the salary element having value greater than 55000 have been extracted from XML document called "company.xml" using XQuery.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here


Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.