To address data from an XML input, XQuery uses XPath.
It makes it easy to filter data and restructure it.
Given the following XML input
<?xml version="1.0" encoding="UTF-8"?>
<applications>
<application>
<id>MyApp</id>
<name>My Application</name>
<version>1.0</version>
</application>
<application>
<id>SomeApp</id>
<name>Some Application</name>
<version>4.2</version>
</application>
<application>
<id>TheOtherApp</id>
<name>That one</name>
<version>13.37</version>
</application>
</applications>
The following XQuery code will extract the application whose id is MyApp
:
/applications/application[id='MyApp']
It produces the following XML document :
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>MyApp</id>
<name>My Application</name>
<version>1.0</version>
</application>
And this code will extract the applications whose version is lower than 10, outputting them in a <oldApplications>
tag :
<oldApplications>{/applications/application[version < 10]}</oldApplications>
It procudes the following XML document :
<?xml version="1.0" encoding="UTF-8"?>
<oldApplications>
<application>
<id>MyApp</id>
<name>My Application</name>
<version>1.0</version>
</application>
<application>
<id>SomeApp</id>
<name>Some Application</name>
<version>4.2</version>
</application>
</oldApplications>