Xquery Example in XML
XQuery Example in XML
Sample XML Document
<library> <book category="fiction"> <title>The Hobbit</title> <author>J.R.R. Tolkien</author> </book> <book category="history"> <title>Sapiens</title> <author>Yuval Noah Harari</author> </book></library>Example 1: Retrieve all book titles
for $b in /library/bookreturn $b/titleResult:
<title>The Hobbit</title><title>Sapiens</title>Example 2: Retrieve titles of books in the fiction category
for $b in /library/bookwhere $b/@category = "fiction"return $b/titleResult:
<title>The Hobbit</title>Example 3: Create new XML output with title and author
for $b in /library/bookreturn <bookInfo> <title>{ $b/title/text() }</title> <author>{ $b/author/text() }</author> </bookInfo>Result:
<bookInfo> <title>The Hobbit</title> <author>J.R.R. Tolkien</author></bookInfo><bookInfo> <title>Sapiens</title> <author>Yuval Noah Harari</author></bookInfo>Summary
XQuery uses
for,where, andreturnclauses.It allows you to filter and reshape XML data.
You can generate new XML structures as output.
Want me to help you build more complex XQuery examples?