<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.mycado.fr &#187; XQuery</title>
	<atom:link href="http://blog.mycado.fr/tag/xquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mycado.fr</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 28 Oct 2009 12:21:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Queries on XML data with XQuery</title>
		<link>http://blog.mycado.fr/2009/08/queries-on-xml-data-with-xquery/</link>
		<comments>http://blog.mycado.fr/2009/08/queries-on-xml-data-with-xquery/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 14:09:32 +0000</pubDate>
		<dc:creator>Stéphane Journot</dc:creator>
				<category><![CDATA[IBM DB2]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XQuery]]></category>

		<guid isPermaLink="false">http://blog.mycado.fr/?p=84</guid>
		<description><![CDATA[We know how to store native XML data in our DB2 tables, and now we will see how we can access to these data. We can choose between standard SQL queries and XQuery.. or both ! The first solution, with a SQL query, only query at the column level of your table; this query will [...]]]></description>
			<content:encoded><![CDATA[<p>We know how to <a title="Store native XML in DB2 table" href="http://blog.mycado.fr/2009/08/stocker-du-xml-natif-dans-un-table-db2/" target="_blank">store native XML data in our DB2 tables</a>, and now we will see how we can access to these data. We can choose between standard SQL queries and <strong>XQuery</strong>.. or both ! The first solution, with a SQL query, only query at the column level of your table; this query will return the full XML data. The second solution, with XQuery, allow us to make a &#8220;query&#8221; inside our XML data.</p>
<p>For the SQL query, nothing more than a <strong>SELECT</strong>:</p>
<pre>SELECT id, info from client</pre>
<p>Easy, but not really powerful for XML data, let&#8217;s try with XQuery, who give us two functions for DB2. <strong>db2-fn:sqlquery</strong> and <strong>db2-fn:xmlcolumn</strong>. The first function r<span>etrieves a sequence that is the result of an SQL fullselect and the second </span><span>retrieves a sequence from a column</span><span>. One important thing you should keep in mind, SQL is not a case-sensitive language alors que XQuery is a case-sensitive language.<br />
</span></p>
<p>An example which return all the XML data from the &#8220;<strong>info</strong>&#8221; column:</p>
<pre>XQUERY db2-fnxmlcolumn ('CLIENT.INFO')</pre>
<p>Which is same as this SQL query:</p>
<pre>SELECT info FROM client</pre>
<p>Let&#8217;s see something more nice. This query will return all the elements in &lt;<strong>name</strong>&gt; , inside the &#8220;info&#8221; column, and with the &lt;<strong>city</strong>&gt; element which containt &#8220;Paris&#8221;:</p>
<pre>XQUERY declare defaut element namespace "http://posample.org";
for $d in db2-fn:xmlcolumn('CLIENT.INFO')/clientinfo
  where $d/addr/city="Paris"
return &lt;out&gt;{$d/name}&lt;/out&gt;</pre>
<p>db2-fn:xmlcolumn retrieves the data from the &#8220;info&#8221; column in the &#8220;client&#8221; table. We add a $d variable, for each element of &lt;clientinfo&gt;, and we use a <strong>where</strong> to filter the &lt;<strong>city</strong>&gt; element which should be &#8220;Paris. To finish, we use &lt;<strong>out</strong>&gt; to output the data:</p>
<pre>&lt;out xmlns="http://posample.org"&gt;
  &lt;name&gt;Sophie Bool&lt;/name&gt;
&lt;/out&gt;</pre>
<div>
<p>To finish, the same example with a SQL query inside the XQuery:</p>
<pre>XQUERY declare default element namespace "http://posample.org";
for $d in db2-fn:sqlquery('SELECT info FROM client')/clientinfo
  where $d/addr/city="Paris
return &lt;out&gt;{$d/name}&lt;/out&gt;</pre>
<p>Enough for today, we will see next time how to do more complex and more powerful query with XQuery !</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.mycado.fr/2009/08/queries-on-xml-data-with-xquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
