{"id":971,"date":"2005-01-27T00:36:00","date_gmt":"2005-01-27T00:36:00","guid":{"rendered":"\/blogs\/bobb\/post\/Fun-with-static-XQuery-evaluation-2.aspx"},"modified":"2005-01-27T00:36:00","modified_gmt":"2005-01-27T00:36:00","slug":"fun-with-static-xquery-evaluation-2","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/","title":{"rendered":"Fun with static XQuery evaluation &#8211; 2"},"content":{"rendered":"<p>\nReference back to the previous entry. Now that we know the rules, let&#39;s try them out:\n<\/p>\n<p>\n&#8212; snip &#8212;\n<\/p>\n<p>\nData(),text() and string() accessors\n<\/p>\n<p>\nXQuery has a function fn:data() to extract scalar, typed values from nodes, a node test text() to return text nodes, and the function fn:string() that returns the string value of a node. Their usages are sometimes confusing. Guidelines for their proper use in SQL Server 2005 are as follows. Consider the XML instance &lt;age&gt;12&lt;\/age&gt;.\n<\/p>\n<p>\nUntyped XML: The path expression \/age\/text() returns the text node &quot;12&quot;. The function fn:data(\/age) returns the string value &quot;12&quot; and so does fn:string(\/age).\n<\/p>\n<p>\nTyped XML: The expression \/age\/text() returns static error for any simple typed &lt;age&gt; element. On the other hand, fn:data(\/age) returns integer 12, while fn:string(\/age) yields the string &quot;12&quot;.\n<\/p>\n<p>\n&#8212; snip &#8212;\n<\/p>\n<p>\nTry this:\n<\/p>\n<p>\nDECLARE @x xml<br \/>\nSET @x = &#39;&lt;age&gt;12&lt;\/age&gt;&#39;<br \/>\n&#8212; works as expected<br \/>\nSELECT @x.query(&#39;data(\/age)&#39;)<br \/>\nGO\n<\/p>\n<p>\nDECLARE @x xml<br \/>\nSET @x = &#39;&lt;age&gt;12&lt;\/age&gt;&#39;<br \/>\n&#8212; fails<br \/>\n&#8212; Msg 2211, Level 16, State 1, Line 6<br \/>\n&#8212; XQuery [query()]: Singleton (or empty sequence) required, found operand of type &#39;element(age,xdt:untypedAny) *&#39;<br \/>\nSELECT @x.query(&#39;string(\/age)&#39;)<br \/>\nGO\n<\/p>\n<p>\nOh. This message looks familiar. It turns out that XQuery functions are strongly typed also. Here&#39;s the definition of fn:string and fn:data:\n<\/p>\n<p>\nfn:string($arg as item()?) as xs:string\n<\/p>\n<p>\nfn:data($arg as item()*) as xdt:anyAtomicType*\n<\/p>\n<p>\nThe &quot;item()*&quot; means that data takes a sequence of 0-n items. &quot;item()?&quot; means that string only takes a sequence of 0-1 item. Let&#39;s fix it then.\n<\/p>\n<p>\nSELECT @x.query(&#39;string(\/age[1])&#39;)\n<\/p>\n<p>\nCool. Here&#39;s the test for comprehension. Let&#39;s try this with typed XML.\n<\/p>\n<p>\n&#8212; start with a schema collection\n<\/p>\n<p>\nCREATE XML SCHEMA COLLECTION ages AS<br \/>\n&#39;&lt;xs:schema<br \/>\n&nbsp;&nbsp; xmlns:xs=&quot;<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema\" class=\"broken_link\">http:\/\/www.w3.org\/2001\/XMLSchema<\/a>&quot;<br \/>\n&nbsp;&nbsp; targetNamespace=&quot;urn:ages&quot;<br \/>\n&nbsp;&nbsp; xmlns:tns=&quot;urn:ages&quot;&gt;<br \/>\n&lt;xs:element name=&quot;age&quot; type=&quot;xs:int&quot;\/&gt;<br \/>\n&lt;\/xs:schema&gt;<br \/>\n&#39;<br \/>\nGO\n<\/p>\n<p>\nDECLARE @x xml(ages)<br \/>\nSET @x = &#39;&lt;age xmlns=&quot;urn:ages&quot;&gt;12&lt;\/age&gt;&#39;<br \/>\n&#8212; fails as expected<br \/>\nSELECT @x.query(&#39;<br \/>\ndeclare default namespace &quot;urn:ages&quot;;<br \/>\n\/age\/text()&#39;)<br \/>\nGO\n<\/p>\n<p>\nDECLARE @x xml(ages)<br \/>\nSET @x = &#39;&lt;age xmlns=&quot;urn:ages&quot;&gt;12&lt;\/age&gt;&#39;<br \/>\n&#8212; works as expected<br \/>\nSELECT @x.query(&#39;<br \/>\ndeclare default namespace &quot;urn:ages&quot;;<br \/>\ndata(\/age)&#39;)<br \/>\nGO\n<\/p>\n<p>\nDECLARE @x xml(ages)<br \/>\nSET @x = &#39;&lt;age&gt;12&lt;\/age&gt;&#39;<br \/>\n&#8212; fails ??!<br \/>\nSELECT @x.query(&#39;string(\/age)&#39;)<br \/>\nGO\n<\/p>\n<p>\nWhy does the last query (against strongly typed XML) fail, even though there is a schema? How can you fix it? There are two different &quot;right answers&quot;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reference back to the previous entry. Now that we know the rules, let&#39;s try them out: &#8212; snip &#8212; Data(),text() and string() accessors XQuery has a function fn:data() to extract scalar, typed values from nodes, a node test text() to return text nodes, and the function fn:string() that returns the string value of a node. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,37],"tags":[],"class_list":["post-971","post","type-post","status-publish","format-standard","hentry","category-sql-server-2005","category-sql-server-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Fun with static XQuery evaluation - 2 - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fun with static XQuery evaluation - 2 - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Reference back to the previous entry. Now that we know the rules, let&#039;s try them out: &#8212; snip &#8212; Data(),text() and string() accessors XQuery has a function fn:data() to extract scalar, typed values from nodes, a node test text() to return text nodes, and the function fn:string() that returns the string value of a node. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2005-01-27T00:36:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/\",\"name\":\"Fun with static XQuery evaluation - 2 - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2005-01-27T00:36:00+00:00\",\"dateModified\":\"2005-01-27T00:36:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2005\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2005\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Fun with static XQuery evaluation &#8211; 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fun with static XQuery evaluation - 2 - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/","og_locale":"en_US","og_type":"article","og_title":"Fun with static XQuery evaluation - 2 - Bob Beauchemin","og_description":"Reference back to the previous entry. Now that we know the rules, let&#39;s try them out: &#8212; snip &#8212; Data(),text() and string() accessors XQuery has a function fn:data() to extract scalar, typed values from nodes, a node test text() to return text nodes, and the function fn:string() that returns the string value of a node. [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/","og_site_name":"Bob Beauchemin","article_published_time":"2005-01-27T00:36:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/","name":"Fun with static XQuery evaluation - 2 - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2005-01-27T00:36:00+00:00","dateModified":"2005-01-27T00:36:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/fun-with-static-xquery-evaluation-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2005","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2005\/"},{"@type":"ListItem","position":3,"name":"Fun with static XQuery evaluation &#8211; 2"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=971"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/971\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}