XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 24: Maps, Arrays, and JSON
Please note that the book contains many inline examples and informal tables that are not provided here.
xquery version "3.1"; map { "ACC": "Accessories", "WMN": "Women's", "MEN": "Men's" }
xquery version "3.1"; map { "ACC": map { "name": "Accessories", "code": 300 }, "WMN": map { "name": "Women's", "code": 310 }, "MEN": map { "name": "Men's", "code": 320 } }
xquery version "3.1"; declare namespace map = "http://www.w3.org/2005/xpath-functions/map"; map:merge(for $p in doc("catalog.xml")//product return map:entry(string($p/number), string($p/name)) )
xquery version "3.1"; declare variable $deptnames := map { "ACC": "Accessories", "WMN": "Women's", "MEN": "Men's" }; declare variable $deptinfo := map { "ACC": map { "deptname": "Accessories", "deptnum": 300 }, "WMN": map { "deptname": "Women's", "deptnum": 310 }, "MEN": map { "deptname": "Men's", "deptnum": 320 } };
xquery version "3.1"; declare variable $deptnames := map { "ACC": "Accessories", "WMN": "Women's", "MEN": "Men's" }; for $prod in doc("catalog.xml")//product return <product num="{$prod/number}" dept-name="{$deptnames($prod/@dept)}"/>
<product num="557" dept-name="Women's"/> <product num="563" dept-name="Accessories"/> <product num="443" dept-name="Accessories"/> <product num="784" dept-name="Men's"/>
xquery version "3.1"; declare variable $deptinfo := map { "ACC": map { "deptname": "Accessories", "deptnum": 300 }, "WMN": map { "deptname": "Women's", "deptnum": 310 }, "MEN": map { "deptname": "Men's", "deptnum": 320 } }; for $prod in doc("catalog.xml")//product return <product num="{$prod/number}" dept-name="{$deptinfo($prod/@dept)("deptname")}" dept-code="{$deptinfo($prod/@dept)("deptnum")}"/>
<product num="557" dept-name="Women's" dept-code="310"/> <product num="563" dept-name="Accessories" dept-code="300"/> <product num="443" dept-name="Accessories" dept-code="300"/> <product num="784" dept-name="Men's" dept-code="320"/>
xquery version "3.1"; declare variable $deptinfo := map { "ACC": map { "deptname": "Accessories", "deptnum": 300 }, "WMN": map { "deptname": "Women's", "deptnum": 310 }, "MEN": map { "deptname": "Men's", "deptnum": 320 } }; for $prod in doc("catalog.xml")//product return <product num="{$prod/number}" dept-name="{$deptinfo?($prod/@dept)?deptname}" dept-code="{$deptinfo?($prod/@dept)?deptnum}"/>
xquery version "3.1"; declare namespace map = "http://www.w3.org/2005/xpath-functions/map"; declare variable $deptnames := map { "ACC": "Accessories", "WMN": "Women's", "MEN": "Men's" }; let $f := function($k, $v) {concat('Key: ', $k, ', value: ', $v)} return map:for-each($deptnames, $f)
xquery version "3.1"; declare namespace map = "http://www.w3.org/2005/xpath-functions/map"; declare function local:large-keys ($maparg as map(xs:integer, item()*)) as xs:integer* { map:keys($maparg)[. > 50] }; local:large-keys(map {10:"a", 55:"b", 60:"c"})
xquery version "3.1"; declare variable $array-of-ints := [10, 20, 30]; declare variable $array-of-arrays := [ ["a", "b", "c"], ["d", "e", "f"] ];
xquery version "3.1"; declare namespace array = "http://www.w3.org/2005/xpath-functions/array"; declare function local:larger-values ($arrayarg as array(xs:integer))as xs:integer* { array:flatten($arrayarg)[. > 15] }; local:larger-values([10, 20, 30])
xquery version "3.1"; declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization"; declare option output:method "json"; declare option output:indent "yes"; map { "number": 557, "props": <props> <length>31</length> </props> }
<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 key="priceInfo"> <number key="price">19.99</number> <number key="discount">10.00</number> </map> </map>