February 2012
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
272829  

XQuery vs SQL/XML

XQuery 1.0 is the W3C language designed for querying XML data. It is similar to SQL in many ways, but just as SQL is designed for querying structured, relational data, XQuery is designed especially for querying semistructured, XML data from a variety of data sources. You can use XQuery to query XML data wherever it is found, whether it is stored in database tables, available through Web Services, or otherwise created on the fly. In addition to querying XML data, XQuery can be used to construct XML data. In this regard, XQuery can serve as an alternative or a complement to both XSLT and the other SQL/XML publishing functions, such as XMLElement.

Everything in XQuery is an "expression", one most important expression is the FLWOR expression, composed of the following, in order, from which FLWOR takes its name: for, let, where , order by, return.

SQL functions XMLQuery and XMLTable are defined by the SQL/XML standard as a general interface between the SQL and XQuery languages. As is the case for the other SQL/XML functions, XMLQuery and XMLTable let you take advantage of the power and flexibility of both SQL and XML. Using these functions, you can construct XML data using relational data, query relational data as if it were XML, and construct relational data from XML data.

XMLQuery is the logical evolution of Oracle’s extract() operator, and XMLTable is the logical evolution of Oracle’s table(xmlsequence(extract(…)))

Below is a simple comparison between XML/SQL and XQuery.

create table test (KEY number, XML_INFO xmltype);
insert into test
   (KEY, XML_INFO)
 values
   (1,
    '<?xml version="1.0" encoding="UTF-8"?>
    <lists>
      <list name="String">
                <display-name >test</display-name>
     </list>
   </lists>');

--XQuery version
SELECT test.KEY AS KEY, v.name, v.display
  FROM test, XMLTABLE('
  $xml_info/lists/list'
PASSING test.XML_INFO AS "xml_info"
COLUMNS name VARCHAR2(100) PATH '@name',
display VARCHAR2(100) PATH 'display-name')  v;

--SQL/XML version
select test.key,
extractValue(value(v), '/list/@name'),
extractValue(value(v), '/list/display-name')
from test,
table(XMLSequence(extract(test.xml_info,'/resource-lists/list'))) v;

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>