XQuery Maps and Arrays 🎯

beginner
11 min

XQuery Maps and Arrays 🎯

Welcome back to CodeYourCraft! Today, we're diving into the exciting world of XQuery Maps and Arrays. These powerful tools are essential for manipulating and querying XML data efficiently. Let's get started! 💡

What are XQuery Maps?

In XQuery, a Map is a collection of key-value pairs. It's similar to JavaScript objects or dictionaries in other programming languages. Here's a simple example:

xml
<map xmlns="http://www.w3.org/2005/xquery-built-ins"> <binding as="item(*)" from="$doc/item"> <sequence> <map-entry key="id" as="xs:integer*" select="id"/> <map-entry key="name" as="xs:string*" select="name"/> </sequence> </binding> </map>

In this example, we're creating a Map from the item nodes in an XML document. The Map has two key-value pairs: id and name. 📝 Note: Always specify the namespace for xquery-built-ins.

What are XQuery Arrays?

An Array in XQuery is a sequence of items of the same data type. It's useful when you need to work with multiple items of the same type. Here's a simple example:

xml
<array xmlns="http://www.w3.org/2005/xquery-built-ins"> <sequence> <item>{1, 2, 3, 4, 5}</item> </sequence> </array>

In this example, we're creating an Array with the numbers from 1 to 5. 💡 Pro Tip: You can specify Array items directly using {...}.

Manipulating Maps and Arrays

You can manipulate Maps and Arrays in XQuery using various functions. Here are a few examples:

Accessing Map Values

To access a Map value, use the map:get($map, $key) function.

xml
<map xmlns="http://www.w3.org/2005/xquery-built-ins"> <binding as="item(*)" from="$doc/item"> <sequence> <map-entry key="id" as="xs:integer*" select="id"/> <map-entry key="name" as="xs:string*" select="name"/> </sequence> </binding> </map> <output> {map:get($doc/item[1]/map/map-entry[1]/@key, $doc/item[1]/map/map-entry[1]/@value)} </output>

In this example, we're accessing the value of the first id key in the Map.

Accessing Array Items

To access an Array item, use the array:item($array, $index) function.

xml
<array xmlns="http://www.w3.org/2005/xquery-built-ins"> <sequence> <item>{1, 2, 3, 4, 5}</item> </sequence> </array> <output> {array:item($doc/array/sequence/item[1], 2)} </output>

In this example, we're accessing the second item in the Array.

Practical Usage

Maps and Arrays can be incredibly useful in real-world XML projects. Here's a practical example:

xml
<items> <item id="1" name="Apple"> <color>Red</color> </item> <item id="2" name="Banana"> <color>Yellow</color> </item> <!-- More items... --> </items> <xquery> <output> { for $item in $doc/items/item let $map := map:from-sequence(map:entry($item/id, $item/name), map:entry($item/name, $item/color)) return map:get($map, "Apple") } </output> </xquery>

In this example, we're creating a Map from the item nodes, storing the id and name as keys, and the color as values. Then, we're accessing the color of the Apple.

Quiz 📝

Quick Quiz
Question 1 of 1

What function is used to access a value in a Map?

That's it for today! We've covered the basics of XQuery Maps and Arrays. Stay tuned for more advanced lessons on XQuery! 🎯