In: Computer Science
<root>
<students>
<element>
<ID>100345</ID>
<Nationality>USA</Nationality>
<Program>ICT</Program>
<age>23</age>
<name>John</name>
</element>
<element>
<ID>100876</ID>
<Nationality>MALAYSIA</Nationality>
<Program>CS</Program>
<age>28</age>
<name>Awang</name>
</element>
<element>
<ID>100257</ID>
<Nationality>AUSTRALIA</Nationality>
<age>25</age>
<name>Alex</name>
</element>
</students>
</root
Write an XQUERY to display the information for all students who are not Malaysians or older than 25.
Xquery is like sql with respect to xml and database respetively
It is used for querying xml data and is built on xpath expressions
it comes under w3c recommendation and is supported by all major databases
It is executed step by step as
finding/ then extracting elements and its attributes from xml based documents, can be used to generate summary reports , extract content from web services, etc
For following xml file let the name be students.xml
To open this file we use doc() function
as doc("students.xml")
now to navigate through xml elements we use path expressions
doc("students.xml")/root/students/element
will navigate you through xml to element names user defined tag
now there are also option for predicates like selective retreival of elements
example doc("students.xml")/root/students/element[age<25]
for student with age less than 25
we can also use where for this conditional retreival
also there is option of
Now for given query in question "Write an XQUERY to display the information for all students who are not Malaysians or older than 25"
it is like select query with fllowing syntax format
FLWOR
For Let Where OrderBY and Return
here we do need let and order by
to return all
for $x in doc("students.xml")/root/students/element
where ($x/@Nationality!="Malaysians" and $x/@age<25)
return $x
to return id:
for $x in doc("students.xml")/root/students/element where ($x/@Nationality!="Malaysians" and $x/@age<25) return data($x/@Id)
Hope you are able to understand concept