Xquery Select in XML
XQuery Select in XML
What Does "Select" Mean in XQuery?
Unlike SQL, XQuery does not have a
SELECTkeyword.Instead, selecting data is done using FLWOR expressions and XPath to navigate and retrieve XML nodes or values.
The
forandreturnclauses are typically used to select nodes or values from XML.
How to Select Data in XQuery
You use XPath expressions inside the for loop (or standalone) to pick XML elements or attributes.
Example XML
<library> <book category="fiction"> <title>The Hobbit</title> </book> <book category="history"> <title>Sapiens</title> </book></library>Example 1: Select All Book Titles
for $b in /library/bookreturn $b/titleOutput:
<title>The Hobbit</title><title>Sapiens</title>Example 2: Select Titles of Books in Fiction Category
for $b in /library/bookwhere $b/@category = "fiction"return $b/titleOutput:
<title>The Hobbit</title>Example 3: Select Titles as Text (String)
for $b in /library/bookreturn string($b/title)Output:
The HobbitSapiensSummary
XQuery "select" is done by iterating over nodes with
forand returning the required nodes or values.XPath expressions inside
fordetermine which nodes are selected.Use
whereto filter selected nodes.Use
returnto output the selection.
Want me to help you write specific XQuery selections?