{"id":2044,"date":"2014-06-19T20:34:10","date_gmt":"2014-06-19T18:34:10","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=2044"},"modified":"2014-06-19T21:03:24","modified_gmt":"2014-06-19T19:03:24","slug":"xquery-namespaces-mini-guide","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=2044","title":{"rendered":"Xquery Namespaces Tips and tools Mini guide"},"content":{"rendered":"<h2>Namespace declarations : example of catalog (cat) and products (prod) :<\/h2>\n<pre>&lt;<strong>cat:<\/strong>catalog <strong>xmlns:cat<\/strong>=\"http:\/\/datypic.com\/cat\"\r\n             <strong>xmlns:prod<\/strong>=\"http:\/\/datypic.com\/prod\"&gt;\r\n  &lt;<strong>cat:<\/strong>number&gt;1446&lt;\/cat:number&gt;\r\n  &lt;<strong>prod:<\/strong>product&gt;\r\n    &lt;<strong>prod:<\/strong>number&gt;565&lt;\/prod:number&gt;\r\n    &lt;prod:name prod:language=\"en\"&gt;Sun Hat red&lt;\/prod:name&gt;\r\n  &lt;\/prod:product&gt;\r\n&lt;<strong>\/cat:<\/strong>catalog&gt;<\/pre>\n<h2>Xqurery and Namespaces declarations in xquery file :<\/h2>\n<p>How to Declare a name space and xquery loop function (in your file.xquery)<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/xquery-namespace.jpg\"><img loading=\"lazy\" class=\"aligncenter size-large wp-image-2049\" alt=\"xquery-namespace\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/xquery-namespace-1024x345.jpg\" width=\"600\" height=\"202\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/xquery-namespace-1024x345.jpg 1024w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/xquery-namespace-300x101.jpg 300w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/xquery-namespace.jpg 1086w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h2>\u00a0BaseX as an xquery tool :<\/h2>\n<p><em>BaseX<\/em> is a very light-weight, high-performance and scalable XML Database engine and XPath\/XQuery Processor with Full Text and Update extensions (<a href=\"http:\/\/www.stylusstudio.com\/examples\/videos.xml\" target=\"_blank\">Sample File XML<\/a>)<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/basex-xquery.jpg\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-2055\" alt=\"basex-xquery\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/basex-xquery-300x166.jpg\" width=\"300\" height=\"166\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/basex-xquery-300x166.jpg 300w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/basex-xquery.jpg 793w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Some useful Xpath script notations :<\/h2>\n<p>The &#8220;\/&#8221; in the path informally means &#8220;go down one level&#8221;,<br \/>\nwhile the &#8220;\/\/&#8221; means &#8220;go down any number of levels&#8221;.<br \/>\nIf the path starts with &#8220;.\/&#8221; or &#8220;.\/\/&#8221; you can leave out the initial &#8220;.&#8221; (this assumes that the selection starts from the top of the tree, which is always the case in our examples).<br \/>\nYou can also use constructs like &#8220;\/..&#8221; to go up one level, and &#8220;\/@id&#8221; to select an attribute.<br \/>\nAgain, this will all be familiar if you already know XPath.<\/p>\n<p>&nbsp;<\/p>\n<h2><b>Accessing XML Documents with XQuery with Stylus Studio one of the best editors :<br \/>\n<\/b><\/h2>\n<p>XQuery allows you to access the file directly from either of these locations, using a suitable URL as an argument to its doc() function. Here&#8217;s an XQuery that simply retrieves and displays the whole document :<\/p>\n<p><strong>doc(&#8216;http:\/\/www.stylusstudio.com\/examples\/videos.xml&#8217;)<\/strong><\/p>\n<p>The file contains a number of sections. One of them is an &lt;actors&gt; element, which we can select like this:<\/p>\n<p><strong>.\/\/actors<\/strong><\/p>\n<p>This produces the result:<\/p>\n<p>&lt;actors&gt;<br \/>\n&lt;actor id=&#8221;00000015&#8243;&gt;Anderson, Jeff&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000030&#8243;&gt;Bishop, Kevin&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;0000000f&#8221;&gt;Bonet, Lisa&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000024&#8243;&gt;Bowz, Eddie&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;0000002d&#8221;&gt;Curry, Tim&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000033&#8243;&gt;Dullea, Keir&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000042&#8243;&gt;Fisher, Carrie&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000006&#8243;&gt;Ford, Harrison&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;00000045&#8243;&gt;Foster, Jodie&lt;\/actor&gt;<br \/>\n<i>&#8230;etc&#8230;<\/i><br \/>\n&lt;\/actors&gt;<\/p>\n<p>If you&#8217;re working in Stylus Studio\u00ae, click on <b>XQuery<\/b> \/ <b>Scenario Properties<\/b>, and under <b>Main Input (optional)<\/b>, browse to the input file and select it. You can now refer to this document in your query simply as &#8220;.&#8221; (dot).<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/accessing_xquery.gif\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-2058\" alt=\"accessing_xquery\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/06\/accessing_xquery-300x243.gif\" width=\"300\" height=\"243\" \/><\/a><\/p>\n<p>In the scrennshot above we can also write more complex XPath expressions like this one:<\/p>\n<p><strong>.\/\/actors\/actor[ends-with(., &#8216;Lisa&#8217;)]<\/strong><\/p>\n<p>which gives the following output :<\/p>\n<p>&lt;actor id=&#8221;0000000f&#8221;&gt;Bonet, Lisa&lt;\/actor&gt;<br \/>\n&lt;actor id=&#8221;0000001b&#8221;&gt;Spoonhauer, Lisa&lt;\/actor&gt;<\/p>\n<h2>XQuery FLWOR Expressions<\/h2>\n<p>If you&#8217;ve used SQL, then you will have recognized the last example as a join between two tables, the videos table and the actors table. It&#8217;s not quite the same in XML, because the data is hierarchic rather than tabular, but XQuery allows you to write join queries in a similar way to the familiar SQL approach. Its equivalent of SQL&#8217;s SELECT expression is called the FLWOR expression, named after its five clauses: for, let, where, order by, return. Here&#8217;s the last example, rewritten this time as a FLWOR expression:<\/p>\n<p>let $doc := .<br \/>\nfor $v in $doc\/\/video,<br \/>\n$a in $doc\/\/actors\/actor<br \/>\nwhere ends-with($a, &#8216;Lisa&#8217;)<br \/>\nand $v\/actorRef = $a\/@id<br \/>\nreturn $v\/title<\/p>\n<p>And of course, we get the same result<\/p>\n<h2>Prolog namespace declarations<\/h2>\n<pre>declare default element namespace \"http:\/\/datypic.com\/report\";\r\ndeclare namespace cat = \"http:\/\/datypic.com\/cat\";\r\ndeclare namespace prod = \"http:\/\/datypic.com\/prod\";\r\n&lt;report&gt; {\r\n  for $product in doc(\"prod_ns.xml\")\/prod:product\r\n  return &lt;lineItem&gt;\r\n           {$product\/prod:number}\r\n           {$product\/prod:name}\r\n         &lt;\/lineItem&gt;\r\n} &lt;\/report&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Some related links :<\/strong><\/span><\/p>\n<p>http:\/\/www.xpathtester.com\/xquery<br \/>\nhttp:\/\/www.datypic.com\/books\/xquery\/examples.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Namespace declarations : example of catalog (cat) and products (prod) : &lt;cat:catalog xmlns:cat=&#8221;http:\/\/datypic.com\/cat&#8221; xmlns:prod=&#8221;http:\/\/datypic.com\/prod&#8221;&gt; &lt;cat:number&gt;1446&lt;\/cat:number&gt; &lt;prod:product&gt; &lt;prod:number&gt;565&lt;\/prod:number&gt; &lt;prod:name prod:language=&#8221;en&#8221;&gt;Sun Hat red&lt;\/prod:name&gt; &lt;\/prod:product&gt; &lt;\/cat:catalog&gt; Xqurery and Namespaces declarations in xquery file : How to Declare&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2848,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[296,26],"tags":[357,297,298,299,300],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2044"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2044"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2044\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2848"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}