Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Xquery Select in XML

Xquery Select in XML

XQuery Select in XML


What Does "Select" Mean in XQuery?

  • Unlike SQL, XQuery does not have a SELECT keyword.

  • Instead, selecting data is done using FLWOR expressions and XPath to navigate and retrieve XML nodes or values.

  • The for and return clauses 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/title

Output:

<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/title

Output:

<title>The Hobbit</title>

Example 3: Select Titles as Text (String)

for $b in /library/bookreturn string($b/title)

Output:

The HobbitSapiens

Summary

  • XQuery "select" is done by iterating over nodes with for and returning the required nodes or values.

  • XPath expressions inside for determine which nodes are selected.

  • Use where to filter selected nodes.

  • Use return to output the selection.


Want me to help you write specific XQuery selections?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql