6️⃣ 🔍 XQuery Tutorial
Estimated reading: 3 minutes 25 views

🔍 XQuery Select – Extract and Filter XML Nodes Effectively

🧲 Introduction – Why Learn XQuery Select?

At its core, XQuery is all about selecting nodes from XML documents. Whether you’re pulling a list of products, filtering search results, or transforming XML feeds, learning to select exactly what you need is essential. XQuery combines XPath syntax with powerful filtering and output controls, giving you precision and flexibility when working with XML.

🎯 In this guide, you’ll learn:

  • How to select specific elements and attributes using XPath
  • How to filter selected nodes with conditions
  • Techniques for selecting text content, attributes, and nested nodes
  • Real examples using for, where, and return

📄 Sample XML: books.xml

<catalog>
  <book id="101" category="programming">
    <title>Learn XQuery</title>
    <author>Jane Doe</author>
    <price>499</price>
  </book>
  <book id="102" category="reference">
    <title>Advanced XML</title>
    <author>John Smith</author>
    <price>699</price>
  </book>
</catalog>

🧾 Example – Select All <title> Elements

doc("books.xml")//title

✅ Output:

<title>Learn XQuery</title>
<title>Advanced XML</title>

🔍 Example – Select Titles of Books Over ₹500

for $b in doc("books.xml")//book
where $b/price > 500
return $b/title

✅ Output:

<title>Advanced XML</title>

🏷️ Example – Select by Attribute

for $b in doc("books.xml")//book[@category='reference']
return $b/title

✅ Output:

<title>Advanced XML</title>

💬 Example – Select Text Only

for $b in doc("books.xml")//book
return string($b/title)

✅ Output:

Learn XQuery
Advanced XML

🔄 Example – Select Element and Attribute Together

for $b in doc("books.xml")//book
return <summary id="{$b/@id}">{$b/title/text()}</summary>

✅ Output:

<summary id="101">Learn XQuery</summary>
<summary id="102">Advanced XML</summary>

🧠 Example – Select Nested Elements

for $b in doc("books.xml")//book
return
  <book>
    {$b/title}
    {$b/author}
  </book>

✅ Outputs each book with its full nested structure.


✅ Best Practices for Selecting Nodes

  • ✔️ Use @attr to select attributes
  • ✔️ Use text() to select only the inner text
  • ✔️ Use where to apply filters based on content or attributes
  • ✔️ Use string() or data() to ensure primitive output
  • ❌ Avoid deep wildcards unless needed (e.g., //*)

🧩 Common Selection Functions

FunctionDescriptionExample
doc("file")Loads external XMLdoc("books.xml")
@idSelects attribute id$b/@id
text()Selects only the text content$b/title/text()
contains()Checks if value contains a stringcontains($b/title, 'XQuery')
starts-with()Matches beginning of valuestarts-with($b/title, 'Learn')

📌 Summary – Recap & Next Steps

XQuery Select is the backbone of all XML querying. It enables precise extraction of elements, attributes, and values—making it easy to transform, filter, and format XML data.

🔍 Key Takeaways:

  • Use XPath syntax (//, /, @, text()) to select nodes
  • Filter with where, contains(), or attribute conditions
  • Combine for + return to loop and extract structured data

⚙️ Real-world relevance: Used in XML reports, e-commerce feeds, CMS data extraction, sitemap generation, and API integrations.


❓ FAQs – XQuery Select

❓ How do I select a specific element by tag?
✅ Use //elementName, e.g., //title.

❓ How do I filter nodes by value?
✅ Use where clause: where $node/price > 500.

❓ How do I get just the text of an element?
✅ Use element/text() or wrap it in string().

❓ How do I select multiple attributes or children?
✅ Use a sequence: ($node/title, $node/author).

❓ Can I use wildcards in selection?
✅ Yes. Use //* for all elements or //@* for all attributes.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XQuery Select

Or Copy Link

CONTENTS
Scroll to Top