Example of XQuery in XML
In this article we will discuss about how we would extract the elements which we want from the XML Document using XQuery.
To show the example of XQuery against XML document firstly we will create a XML document against which we will run the XQuery and show the output. In our XML document we will create the record of the college student and in it <college> node will be the root node in the XML document.
XML file "college.xml"
<?xml version="1.0" encoding="utf-8" ?>
<college>
<student>
<id>101</id>
<name>Megha</name>
<age>23</age>
</student>
<student>
<id>102</id>
<name>Richa</name>
<age>22</age>
</student>
<student>
<id>103</id>
<name>Vishakha</name>
<age>25</age>
</student>
<student>
<id>104</id>
<name>Isha</name>
<age>25</age>
</student>
</college>
|
XQuery uses some functions to extract data from the XML Document and one of this is doc() function to open the XML Document which is as follows:
Now we will use the following path expression in XQuery to navigate through all the element which we want to extract from the XML document which is as follows:
docs("college.xml")/college/student/id
|
Here /college selects the college element , /student selects all the student element resides within the college element and /id selects all the id elements under each student element and the output will be like this.
<id>101</id>
<id>102</id>
<id>103</id>
<id>104</id>
|
In the above output it is clear that the XQuery docs("college.xml")/college/student/id has extract all the id element from the XML document "college.xml".
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here