{"id":1032,"date":"2004-09-06T07:49:00","date_gmt":"2004-09-06T07:49:00","guid":{"rendered":"\/blogs\/bobb\/post\/Using-the-data()-function-in-XQuery.aspx"},"modified":"2004-09-06T07:49:00","modified_gmt":"2004-09-06T07:49:00","slug":"using-the-data-function-in-xquery","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/","title":{"rendered":"Using the data() function in XQuery"},"content":{"rendered":"<p>\nSome folks have asked why I appear to use the XQuery data() function on occasion, but not always, when I&#39;m doing demos. If you want the atomic value of an attribute, rather than the attribute node itself, data() is your friend. data() makes an atomic value from any XQuery item, but with attribute nodes it&#39;s particularly useful. SQL Server&#39;s XQuery functions don&#39;t allow returning bare attributes at the root level (bare text nodes are allowed), and the value function is always looking for a singleton atomic value. The data() function helps in these situations; here&#39;s an example:\n<\/p>\n<p>\n<br \/>\ndeclare @x xml<br \/>\nset @x = &#39;&lt;foo bar=&quot;baz&quot;\/&gt;&#39;<br \/>\n&#8212; returns error: &quot;Attribute may not appear outside of an element&quot;<br \/>\nselect @x.query(<a href=\"mailto:'\/foo\/@bar'\">&#39;\/foo\/@bar&#39;<\/a>)\n<\/p>\n<p>\n<br \/>\ndeclare @x xml<br \/>\nset @x = &#39;&lt;foo bar=&quot;baz&quot;\/&gt;&#39;<br \/>\n&#8212; returns baz<br \/>\nselect @x.query(&#39;data(\/foo\/@bar)&#39;)\n<\/p>\n<p>\nUsing the data() function is different from using the string() function. data() takes a sequence of items (nodes or atomic values) and atomizes them, ie. returns a sequence of atomic values. string() returns the string value of a single item.&nbsp; Here&#39;s an example comparing data() to string():\n<\/p>\n<p>\ndeclare @x xml <br \/>\n&#8212; XML fragment<br \/>\nset @x = &#39;&lt;x&gt;hello&lt;y&gt;world&lt;\/y&gt;&lt;\/x&gt;&lt;x&gt;again&lt;\/x&gt;&#39;<br \/>\nselect @x.query(&#39;data(\/*)&#39;)\n<\/p>\n<p>\nreturns a sequence of two string values:<br \/>\nhelloworld again\n<\/p>\n<p>\n<br \/>\ndeclare @x xml <br \/>\n&#8212; XML fragment<br \/>\nset @x = &#39;&lt;x&gt;hello&lt;y&gt;world&lt;\/y&gt;&lt;\/x&gt;&lt;x&gt;again&lt;\/x&gt;&#39;<br \/>\nselect @x.query(&#39;string(\/*)&#39;)\n<\/p>\n<p>\nreturns a static typing error, because string() requires a singleton or empty sequence as input\n<\/p>\n<p>\ndeclare @x xml <br \/>\n&#8212; XML fragment<br \/>\nset @x = &#39;&lt;x&gt;hello&lt;y&gt;world&lt;\/y&gt;&lt;\/x&gt;&lt;x&gt;again&lt;\/x&gt;&#39;<br \/>\nselect @x.query(&#39;string(\/*[1])&#39;)\n<\/p>\n<p>\nreturns a single string value:<br \/>\nhelloworld\n<\/p>\n<p>\nI&#39;ll have more to say about static typing in SQL Server 2005 in the future. It can seem like it makes simple queries, where you &ldquo;know&rdquo; the data but don&#39;t use a schema&nbsp;harder to write, but I&#39;m getting to like it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some folks have asked why I appear to use the XQuery data() function on occasion, but not always, when I&#39;m doing demos. If you want the atomic value of an attribute, rather than the attribute node itself, data() is your friend. data() makes an atomic value from any XQuery item, but with attribute nodes it&#39;s [&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-1032","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>Using the data() function in XQuery - 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\/using-the-data-function-in-xquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using the data() function in XQuery - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Some folks have asked why I appear to use the XQuery data() function on occasion, but not always, when I&#039;m doing demos. If you want the atomic value of an attribute, rather than the attribute node itself, data() is your friend. data() makes an atomic value from any XQuery item, but with attribute nodes it&#039;s [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2004-09-06T07:49: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\/using-the-data-function-in-xquery\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/\",\"name\":\"Using the data() function in XQuery - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2004-09-06T07:49:00+00:00\",\"dateModified\":\"2004-09-06T07:49:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/#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\":\"Using the data() function in XQuery\"}]},{\"@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":"Using the data() function in XQuery - 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\/using-the-data-function-in-xquery\/","og_locale":"en_US","og_type":"article","og_title":"Using the data() function in XQuery - Bob Beauchemin","og_description":"Some folks have asked why I appear to use the XQuery data() function on occasion, but not always, when I&#39;m doing demos. If you want the atomic value of an attribute, rather than the attribute node itself, data() is your friend. data() makes an atomic value from any XQuery item, but with attribute nodes it&#39;s [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/","og_site_name":"Bob Beauchemin","article_published_time":"2004-09-06T07:49: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\/using-the-data-function-in-xquery\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/","name":"Using the data() function in XQuery - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2004-09-06T07:49:00+00:00","dateModified":"2004-09-06T07:49:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-the-data-function-in-xquery\/#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":"Using the data() function in XQuery"}]},{"@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\/1032","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=1032"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}