XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Appendix A: Built-in Function Reference
Please note that the book contains many inline examples and informal tables that are not provided here.
<catalogs> <catalog name="ACC" xml:base="http://datypic.com/ACC/"> <product number="443" href="prod443.html"/> <product number="563" href="prod563.html"/> </catalog> <catalog name="WMN" xml:base="http://datypic.com/WMN/"> <product number="557" href="prod557.html"/> </catalog> </catalogs>
Example | Return value | Return type |
---|---|---|
data($cat//product[1]/number)
|
557
|
xs:untypedAtomic
|
data($cat//number)
|
(557, 563, 443, 784)
|
xs:untypedAtomic*
|
data($cat//product[1]/@dept)
|
WMN
|
xs:untypedAtomic
|
data($cat//product[1]/colorChoices)
|
navy black
|
xs:untypedAtomic
|
data($cat//product[1])
|
557 Fleece Pullover navy black
|
xs:untypedAtomic
|
data($cat//product[4]/desc)
|
Our favorite shirt!
|
xs:untypedAtomic
|
Example | Return value | Return type |
---|---|---|
data($cat//product[1]/number)
|
557
|
xs:integer
|
data($cat//number)
|
(557, 563, 443, 784)
|
xs:integer*
|
data($cat//product[1]/@dept)
|
WMN
|
xs:string
|
data($cat//product[1]/colorChoices)
|
(navy, black)
|
xs:string*
|
data($cat//product[1])
| Error FOTY0012
| N/A |
data($cat//product[4]/desc)
|
Our favorite shirt!
|
xs:untypedAtomic
|
Example | Return value | Return type |
---|---|---|
data(1)
|
1
|
xs:integer
|
data( (1, 2, 3) )
|
(1, 2, 3)
|
xs:integer*
|
data( [1, 2, 3] )
|
(1, 2, 3)
|
xs:integer*
|
data( [1, 2, ['a', 'b', 'c']] )
|
(1, 2, 'a', 'b', 'c')
|
xs:anyAtomicType*
|
data( () )
|
()
| |
data( concat#3 )
| Error FOTY0013
|
<catalog> <product dept="WMN"> <id>A1</id> <number>557</number> <name language="en">Fleece Pullover</name> </product> <product dept="ACC"> <id>A2</id> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> </catalog>
<html> <body> <h1>Table of Contents</h1> {for $prod in doc("catalog.xml")//product return <p><a href="#{generate-id($prod)}">{data($prod/number)}</a></p>} <h1>Product Info</h1> {for $prod in doc("catalog.xml")//product return (<h2 id="{generate-id($prod)}">{data($prod/number)}</h2>, <p>Name: {data($prod/name)}, Dep: {data($prod/@dept)}</p>)} </body> </html>
<html> <body> <h1>Table of Contents</h1> <p><a href="#d0e3">557</a></p> <p><a href="#d0e15">563</a></p> <p><a href="#d0e24">443</a></p> <p><a href="#d0e33">784</a></p> <h1>Product Info</h1> <h2 id="d0e3">557</h2> <p>Name: Fleece Pullover, Dep: WMN</p> <h2 id="d0e15">563</h2> <p>Name: Floppy Sun Hat, Dep: ACC</p> <h2 id="d0e24">443</h2> <p>Name: Deluxe Travel Bag, Dep: ACC</p> <h2 id="d0e33">784</h2> <p>Name: Cotton Dress Shirt, Dep: MEN</p> </body> </html>
<book> <section id="preface">This book introduces XQuery... The examples are downloadable<fnref ref="fn1"/>... </section> <section id="context">...</section> <section id="language">...Expressions, introduced in <secRef refs="context"/>, are... </section> <section id="types">...As described in <secRef refs="context language"/>, you can... </section> <fn fnid="fn1">See http://datypic.com.</fn> </book>
xquery version "3.1"; json-to-xml('{ "number": 557, "name": "Fleece Pullover", "colorChoices": ["navy", "black"], "is-current": true, "other": null }')
<map xmlns="http://www.w3.org/2005/xpath-functions"> <number key="number">557</number> <string key="name">Fleece Pullover</string> <array key="colorChoices"> <string>navy</string> <string>black</string> </array> <boolean key="is-current">true</boolean> <null key="other"/> </map>
xquery version "3.1"; json-to-xml('{ "number": 557, "name": "Fleece Pullover", "name": "Fleece Pullover Redux", "colorChoices": ["navy \u00E9", "black \uFFFF"], "is-current": true, "other": null }', map {"duplicates": "use-first", "liberal": false(), "validate": false(), "escape": false(), "fallback": function($s){"ERROR!!"}})
<map xmlns="http://www.w3.org/2005/xpath-functions"> <number key="number">557</number> <string key="name">Fleece Pullover</string> <array key="colorChoices"> <string>navy é</string> <string>black ERROR!!</string> </array> <boolean key="is-current">true</boolean> <null key="other"/> </map>
<desclist xml:lang="en"> <desc xml:lang="en-US"> <line>The first line of the description.</line> </desc> <desc xml:lang="fr"> <line>La première ligne de la déscription.</line> </desc> </desclist>
let $catalog := doc("catalog.xml")/catalog for $prod in $catalog/product return concat($prod/number, (if ($prod is $catalog/product[last()]) then (".") else (", ")) )
module namespace strings = "http://datypic.com/strings"; declare variable $strings:maxStringLength := 32; declare function strings:trim($arg as xs:string?) as xs:string? { replace(replace($arg,'^\s+',''),'\s+$','') };
xquery version "3.1"; declare namespace strings = "http://datypic.com/strings"; let $library := load-xquery-module("http://datypic.com/strings") let $trimFunction := $library?functions?(xs:QName("strings:trim"))?1 return $trimFunction(" x y ")
map { "variables": map{ xs:QName("strings:maxStringLength"): 32 }, "functions": map{ xs:QName("strings:trim"): map{ 1: strings:trim#1 } } }
xquery version "3.1"; module namespace prod = "http://datypic.com/prod"; declare context item as element(catalog) external; declare variable $prod:label as xs:string external; declare variable $prod:prods as element(product)* := product; declare function prod:countProds ($prods as element(product)*) as xs:string {concat($prod:label, ': ', count($prods))}; declare function prod:countProds ($prods as element(product)*, $dept as xs:string) as xs:string {concat($prod:label, ': ', count($prods[@dept = $dept]))};
xquery version "3.1"; declare namespace prod = "http://datypic.com/prod"; let $library := load-xquery-module("http://datypic.com/prod", map {"context-item": doc("catalog.xml")/catalog, "location-hints": "lib2.xqm", "variables": map{ xs:QName("prod:label") : "Product Count"} }) let $prodsVariableValue := $library?variables?(xs:QName("prod:prods")) let $countProdArity1 := $library?functions?(xs:QName("prod:countProds"))?1 return $countProdArity1($prodsVariableValue)
map { "variables": map{ xs:QName("prod:label"): "Product Count", xs:QName("prod:prods"): doc("catalog.xml")/catalog/product }, "functions": map{xs:QName("prod:countProds"): map{ 1: prod:countProds#1, 2: prod:countProds#2 } } }
<noNamespace> <pre:prefixed xmlns="http://datypic.com/unpre" xmlns:pre="http://datypic.com/pre"> <unprefixed pre:prefAttr="a" noNSAttr="b">123</unprefixed> </pre:prefixed> </noNamespace>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <child>12</child> <child xsi:nil="true"></child> <child></child> <child/> <child xsi:nil="false"></child> </root>
xquery version "3.1"; parse-json('{ "number": 557, "name": "Fleece Pullover", "colorChoices": ["navy", "black"], "is-current": true, "other": null }')
map { "number": xs:double(557), "name": "Fleece Pullover", "colorChoices": ["navy", "black"], "is-current": true(), "other": () }
xquery version "3.1"; parse-json('{ "number": 557, "name": "Fleece Pullover", "name": "Fleece Pullover Redux", "colorChoices": ["navy \u00E9", "black \uFFFF"], "is-current": true, "other": null }', map {"duplicates": "use-first", "liberal": false(), "validate": false(), "escape": false(), "fallback": function($s){"ERROR!!"} })
map { "number": xs:double(557), "name": "Fleece Pullover", "colorChoices": ["navy é", "black ERROR!!"], "is-current": true(), "other": () }
xquery version "3.1"; serialize(map { "number": xs:double(557), "name": "Fleece Pullover", "colorChoices": ["navy", "black"], "is-current": true(), "other": () }, <output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization"> <output:method value="json"/> <output:indent value="yes"/> </output:serialization-parameters>)
<xquery version "3.1"; serialize(map { "number": xs:double(557), "name": "Fleece Pullover", "colorChoices": ["navy", "black"], "is-current": true(), "other": () }, map { "method": "json", "indent"> true() })
xquery version "3.1"; let $result := transform( map { "stylesheet-location": "render.xsl", "source-node": doc("catalog.xml") }) return $result?output
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="catalog"> <p>There are <xsl:value-of select="count(product)"/> products.</p> </xsl:template> </xsl:stylesheet>
<p>There are 4 products.</p>
xquery version "3.1"; let $result := transform( map { "stylesheet-location": "render2.xsl", "source-node": doc("catalog.xml"), "stylesheet-params": map { QName("", "label"): "Note", QName("", "msg"): "Hi!"} }) return $result?output
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="label"/> <xsl:param name="msg"/> <xsl:template match="catalog"> <p> <xsl:value-of select="$label"/> <xsl:text>: </xsl:text> <xsl:value-of select="$msg"/> <xsl:text>. There are </xsl:text> <xsl:value-of select="count(product)"/> <xsl:text> products.</xsl:text> </p> </xsl:template> </xsl:stylesheet>
<p>Note: Hi! There are 4 products.</p>