XML/XQuery/Axis
Compare element value
<source lang="xml">
File: Data.xml <prices>
<priceList> <prod> <price>29.99</price> </prod> <prod> <price>69.99</price> </prod> </priceList>
</prices>
File: Query.xquery if (doc("Data.xml")/prices/priceList/prod[price < 30]) then <bin>{
doc("Data.xml")/*/priceList/prod[price < 30] }</bin>
else ()
Output: <?xml version="1.0" encoding="UTF-8"?> <bin>
<prod> <price>29.99</price> </prod>
</bin>
</source>
Embedded direct element constructors
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <id>0001</id> <name language="en">name 1</name> </car> <car model="B"> <id>0002</id> <name language="en">name 2</name> </car> <car model="B"> <id>0003</id> <name language="en">name 3</name> </car>
</order>
File: Query.xquery <html>
car order
A huge list of {count(doc("Data.xml")//car)} cars.
</html>
Output: <?xml version="1.0" encoding="UTF-8"?> <html>
car order
A huge list of 3 cars.
</html>
</source>
Enclosed expressions that evaluate to attributes
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <id>0001</id> <name language="en">name 1</name> </car> <car model="B"> <id>0002</id> <name language="en">name 2</name> </car> <car model="B"> <id>0003</id> <name language="en">name 3</name> </car>
</order> File: Query.xquery for $prod in doc("Data.xml")/order/car
returnOutput: <?xml version="1.0" encoding="UTF-8"?>
</source>
Enclosed expressions that evaluate to elements
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <id>0001</id> <name language="en">name 1</name> </car> <car model="B"> <id>0002</id> <name language="en">name 2</name> </car> <car model="B"> <id>0003</id> <name language="en">name 3</name> </car>
</order>
File: Query.xquery for $prod in doc("Data.xml")/order/car
returnOutput: <?xml version="1.0" encoding="UTF-8"?>
</source>
Enclosed expressions with multiple subexpressions
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <id>0001</id> <name language="en">name 1</name> </car> <car model="B"> <id>0002</id> <name language="en">name 2</name> </car> <car model="B"> <id>0003</id> <name language="en">name 3</name> </car>
</order>
File: Query.xquery for $prod in doc("Data.xml")/order/car
returnOutput: <?xml version="1.0" encoding="UTF-8"?>
</source>
Including complex elements from the input document
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <id>0001</id> <name language="en">name 1</name> </car> <car model="B"> <id>0002</id> <name language="en">name 2</name> </car> <car model="B"> <id>0003</id> <name language="en">name 3</name> </car>
</order>
File: Query.xquery for $prod in doc("Data.xml")/order/car[@model = "A"] return $prod
Output: <?xml version="1.0" encoding="UTF-8"?> <car model="A">
<id>0001</id> <name language="en">name 1</name> </car> </source>
Including elements from the input document
<source lang="xml">
File: Data.xml
<order>
<car model="A"> <name language="en">name 1</name> </car> <car model="B"> <name language="en">name 2</name> </car> <car model="B"> <name language="en">name 3</name> </car>
</order> File: Query.xquery for $prod in doc("Data.xml")/order/car[@model = "A"] return $prod/name
Output: <?xml version="1.0" encoding="UTF-8"?> <name language="en">name 1</name>
</source>