<?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>FireTeam &#187; basex</title>
	<atom:link href="http://www.fireteam.it/tag/basex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fireteam.it</link>
	<description>#fuoco@AzzurraNet</description>
	<lastBuildDate>Tue, 16 Mar 2010 15:55:56 +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>Interrogazioni XQuery in Java</title>
		<link>http://www.fireteam.it/2009/08/interrogazioni-xquery-in-java/</link>
		<comments>http://www.fireteam.it/2009/08/interrogazioni-xquery-in-java/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 08:28:25 +0000</pubDate>
		<dc:creator>saverio</dc:creator>
				<category><![CDATA[Programmazione Java]]></category>
		<category><![CDATA[basex]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xquery]]></category>

		<guid isPermaLink="false">http://www.fireteam.it/?p=574</guid>
		<description><![CDATA[XQuery, una abbrevazione per XML Query Language, è un linguaggio di programmazione specificato dal W3C e destinato ad interrogare documenti e basi di dati XML. Questo perché XML si sta proponendo come la tecnologia per rimpiazzare i vecchi DBMS relazionali  
Il w3c ha definito il linguaggio XQuery 1.0; usa la sintassi delle espressioni di [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><a href="http://www.fireteam.it/wp-content/uploads/2009/08/xquery.gif"><img class="alignright size-full wp-image-577" title="xquery" src="http://www.fireteam.it/wp-content/uploads/2009/08/xquery.gif" alt="xquery" width="160" height="160" /></a>XQuery, una abbrevazione per XML Query Language, è un linguaggio di programmazione specificato dal W3C e destinato ad interrogare documenti e basi di dati XML. Questo perché XML si sta proponendo come la tecnologia per rimpiazzare i vecchi DBMS relazionali <img src='http://www.fireteam.it/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p style="text-align: justify;">Il w3c ha definito il linguaggio XQuery 1.0; usa la sintassi delle espressioni di XPath  2.0, con l&#8217;aggiunta delle cosiddette espressioni FLWOR per la formulazione di query complesse. Il risultato è un linguaggio di programmazione funzionale, dichiarativo, con somiglianze con il vecchio SQL.</p>
<p style="text-align: justify;">Per effettuare delle query xquery su un file XML possiamo usare delle librerie come BaseX e Saxon. Purtroppo attualmente Saxon non è un prodotto del tutto gratuito, quindi scegliamo di usare BaseX, un processore Xquery-XPath open source.</p>
<p style="text-align: justify;"><span id="more-574"></span>In realtà BaseX, oltre ad essere utlilizzabile come libreria all&#8217;interno del linguaggio Java, ci mette a disposizione un&#8217;interfaccia grafica dalla quale possiamo effettuare query su file XML ben formati arbitrari.</p>
<h2 style="text-align: justify;">Primo esempio di query</h2>
<p style="text-align: justify;">La prima cosa da fare è inserire i file jar di BaseX nel build path del nostro progetto; la seconda è assicurarsi di avere java 1.6 (non funziona con java 1.5) perché BaseX utilizza il package javax.xml.stream.</p>
<p style="text-align: justify;">Questo è un sempio dove si esegue la query <strong>//li </strong>sul file &#8216;<strong>input</strong>&#8216;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.xquery.XQConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.xquery.XQDataSource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.xquery.XQPreparedExpression</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.xquery.XQResultSequence</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> XQueryAPIExample <span style="color: #009900;">&#123;</span>
 <span style="color: #008000; font-style: italic; font-weight: bold;">/** Database Driver. */</span>
 <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DRIVER <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;org.basex.api.xqj.BXQDataSource&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Gets the XQDataSource for the specified Driver.</span>
 XQDataSource source <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>XQDataSource<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>DRIVER<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Creates an XQConnection</span>
 XQConnection conn <span style="color: #339933;">=</span> source.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Prepares the Expressionwith the Document and the Query.</span>
 XQPreparedExpression expr <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareExpression</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;doc('input')//li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Executes the XQuery query.</span>
 XQResultSequence result <span style="color: #339933;">=</span> expr.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Gets all results of the execution.</span>
 <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">// Prints the results to the console.</span>
 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getItemAsString</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Questo esempio funziona solo se il file XML interrogato non dichiara nessun namespace.</p>
<h2 style="text-align: justify;">Query in presenza di namespace</h2>
<p style="text-align: justify;">Supponiamo adesso di interrogare un file XML con namespace&#8230; ad esempio un file in formato XML Mpeg-7 che usa i seguenti namespace:<br />
xmlns=&#8221;urn:mpeg:mpeg7:schema:2001&#8243;<br />
xmlns:mpeg7=&#8221;urn:mpeg:mpeg7:schema:2001&#8243;<br />
xmlns:xsi=&#8221;http://www.w3.org/2001/XMLSchema-instance&#8221;</p>
<p style="text-align: justify;">Per far riconoscere i namespace dobbiamo dichiararli nel prologo della xquery in questo modo:<br />
declare default element namespace &#8220;urn:mpeg:mpeg7:schema:2001&#8243;<br />
declare  namespace mpeg7 = &#8220;urn:mpeg:mpeg7:schema:2001&#8243;<br />
declare  namespace xsi = &#8220;http://www.w3.org/2001/XMLSchema-instance&#8221;
</p>
<p style="text-align: justify;">Questo è il codice che esegue la query sul file file/video/extract.xml:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> prologoQuery <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">+</span>
 <span style="color: #0000ff;">&quot;declare default element namespace <span style="color: #000099; font-weight: bold;">\&quot;</span>urn:mpeg:mpeg7:schema:2001<span style="color: #000099; font-weight: bold;">\&quot;</span>; &quot;</span> <span style="color: #339933;">+</span>
 <span style="color: #0000ff;">&quot;declare  namespace mpeg7 = <span style="color: #000099; font-weight: bold;">\&quot;</span>urn:mpeg:mpeg7:schema:2001<span style="color: #000099; font-weight: bold;">\&quot;</span>; &quot;</span> <span style="color: #339933;">+</span>
 <span style="color: #0000ff;">&quot;declare  namespace xsi = <span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span style="color: #000099; font-weight: bold;">\&quot;</span>; &quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Gets the XQDataSource for the specified Driver.</span>
 XQDataSource source <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>XQDataSource<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>DRIVER<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Creates an XQConnection</span>
 XQConnection conn <span style="color: #339933;">=</span> source.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Prepares the Expressionwith the Document and the Query.</span>
 <span style="color: #666666; font-style: italic;">//XQPreparedExpression expr = conn.prepareExpression(&quot;&lt;xml&gt;1 + 2 = { 1+2 }&lt;/xml&gt;/text()&quot;);</span>
 <span style="color: #666666; font-style: italic;">//XQPreparedExpression expr = conn.prepareExpression(&quot;doc('file/video/20090201_video_15213221.xml')//meta&quot;);</span>
 XQPreparedExpression expr <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareExpression</span><span style="color: #009900;">&#40;</span>prologoQuery <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; doc('file/video/extract.xml')//Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Executes the XQuery query.</span>
 XQResultSequence result <span style="color: #339933;">=</span> expr.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Gets all results of the execution.</span>
 <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">//element = (org.w3c.dom.Element) result.getObject();</span>
 <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getItemAsString</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Nella riga 18 è commentato un altro modo per usare gli oggetti restiuiti dalla query, che ci restituisce il risultato come elementi della struttura DOM del file XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fireteam.it/2009/08/interrogazioni-xquery-in-java/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
