Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 71d40963b505df4524269198e237b3e3 > files > 763

virtuoso-opensource-doc-6.1.4-2.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head profile="http://internetalchemy.org/2003/02/profile">
  <link rel="foaf" type="application/rdf+xml" title="FOAF" href="http://www.openlinksw.com/dataspace/uda/about.rdf" />
  <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" />
  <meta name="dc.title" content="13. XML Support" />
  <meta name="dc.subject" content="13. XML Support" />
  <meta name="dc.creator" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="dc.copyright" content="OpenLink Software, 1999 - 2009" />
  <link rel="top" href="index.html" title="OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="search" href="/doc/adv_search.vspx" title="Search OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="parent" href="webandxml.html" title="Chapter Contents" />
  <link rel="prev" href=".html" title="" />
  <link rel="next" href="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)" />
  <link rel="shortcut icon" href="../images/misc/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" href="doc.css" />
  <link rel="stylesheet" type="text/css" href="/doc/translation.css" />
  <title>13. XML Support</title>
  <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
  <meta name="author" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="copyright" content="OpenLink Software, 1999 - 2009" />
  <meta name="keywords" content="" />
  <meta name="GENERATOR" content="OpenLink XSLT Team" />
 </head>
 <body>
  <div id="header">
    <a name="forxmlforsql" />
    <img src="../images/misc/logo.jpg" alt="" />
    <h1>13. XML Support</h1>
  </div>
  <div id="navbartop">
   <div>
      <a class="link" href="webandxml.html">Chapter Contents</a> | <a class="link" href="webandxml.html" title="XML Support">Prev</a> | <a class="link" href="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)">Next</a>
   </div>
  </div>
  <div id="currenttoc">
   <form method="post" action="/doc/adv_search.vspx">
    <div class="search">Keyword Search: <br />
        <input type="text" name="q" /> <input type="submit" name="go" value="Go" />
    </div>
   </form>
   <div>
      <a href="http://www.openlinksw.com/">www.openlinksw.com</a>
   </div>
   <div>
      <a href="http://docs.openlinksw.com/">docs.openlinksw.com</a>
   </div>
    <br />
   <div>
      <a href="index.html">Book Home</a>
   </div>
    <br />
   <div>
      <a href="contents.html">Contents</a>
   </div>
   <div>
      <a href="preface.html">Preface</a>
   </div>
    <br />
   <div class="selected">
      <a href="webandxml.html">XML Support</a>
   </div>
    <br />
   <div class="selected">
      <a href="forxmlforsql.html">Rendering SQL Queries as XML (FOR XML Clause)</a>
    <div>
        <a href="#forxmlexplicmode" title="FOR XML EXPLICIT Mode">FOR XML EXPLICIT Mode</a>
        <a href="#examplesofforxml" title="Examples of FOR XML">Examples of FOR XML</a>
        <a href="#forxmlfunc" title="Functions">Functions</a>
        <a href="#forxmlsyntax" title="FOR XML Syntax">FOR XML Syntax</a>
    </div>
   </div>
   <div>
      <a href="composingxmlinsql.html">XML Composing Functions in SQL Statements (SQLX)</a>
   </div>
   <div>
      <a href="xmlservices.html">Virtuoso XML Services</a>
   </div>
   <div>
      <a href="queryingxmldata.html">Querying Stored XML Data</a>
   </div>
   <div>
      <a href="updategrams.html">Using UpdateGrams to Modify Data</a>
   </div>
   <div>
      <a href="xmltemplates.html">XML Templates</a>
   </div>
   <div>
      <a href="xmlschema.html">XML DTD and XML Schemas</a>
   </div>
   <div>
      <a href="xq.html">XQuery 1.0 Support</a>
   </div>
   <div>
      <a href="xslttrans.html">XSLT Transformation</a>
   </div>
   <div>
      <a href="xmltype.html">XMLType</a>
   </div>
   <div>
      <a href="xmldom.html">Changing XML entities in DOM style</a>
   </div>
    <br />
  </div>
  <div id="text">
<a name="forxmlforsql" />
    <h2>13.1. Rendering SQL Queries as XML (FOR XML Clause)</h2>

	<p>
Virtuoso extends SQL-92 with the FOR XML clause that allows any SQL
result set to be turned into XML according to some simple rules. The
notation and functionality are similar to those offered by Microsoft
SQL Server and IIS.
</p>
	<p>
The FOR XML clause has 3 variants:
</p>
<div class="formalpara">
      <strong>RAW</strong>
<p>
Make an XML entity from each row of the result set; do not attempt to
construct hierarchies.  Each row&#39;s data is enclosed in a &lt;ROW/&gt;
element and each column is either an attribute or child element.
</p>
</div>
<div class="formalpara">
      <strong>AUTO</strong>
<p>
A hierarchy is constructed with one level for each table of the join
for which at least one column is selected. The table whose column is
first mentioned in the selection will be the topmost element, the next
table its child, etc.  Each level of the tree will consist of one type
of element.  A parent element will have multiple children if
consecutive rows do not differ in the column values coming from the
parent element.  When a table&#39;s column values differ from the previous
row, the element and all children thereof are closed and a new element
is started, with children filled out from other columns of the result
set.
</p>
</div>
<div class="formalpara">
      <strong>EXPLICIT</strong>
<p>
This mode gives more control on the resulting tree&#39;s structure while
requiring a more elaborate query structure.  In this mode, the query
will be a UNION ALL of many joins and each row will specify exactly
one element.  Which type of element this is and where in the tree it
will be placed are determined by the values of the first two columns,
TAG and PARENT.
</p>
</div>
	<p>
In all modes, columns may either be attributes or sub-elements.  The
<strong>ELEMENT</strong> keyword after the FOR XML clause forces
all columns to be rendered as sub-elements; attribute are the
default.
</p>
	<p>
In all modes except explicit, the names of elements are the unprefixed
table names and the names of attributes are the columns&#39; names in the
result set.  If tables have correlation names the correlation names
are used in the output instead of the table names.  Expressions are
allowed in the selections but these should be named using AS.  In AUTO
mode Virtuoso assumes expressions belong to the topmost element.
</p>
	<p>
The FOR XML clause is generally allowed in SELECT statements in place
of the FOR UPDATE clause.  However it only has an effect when the
statement is executed through the <a href="fn_xml_auto.html">xml_auto()</a> function.
</p>

<div class="tip">
      <div class="tiptitle">See Also:</div> <p>The <a href="htmlconductorbar.html#sqlxmlstmts">SQL-XML Statements</a> page described in the
Visual Server Administration Interface section provides a fast
graphical way of supplying an SQL statement to Virtuoso and saving the
view as a resource accessible from the WebDAV store.</p>
    </div>


<a name="forxmlexplicmode" />
    <h3>13.1.1. FOR XML EXPLICIT Mode</h3>

<p>
This mode gives the developer the most control over the generated
result tree but requires a verbose query formulation.  Each row must
begin with two integer columns, the first identifying the element
represented by the row and the second the parent element type of this
element.  Consider:
</p>

<div>
      <pre class="programlisting">
select 1 as tag, null as parent,
       &quot;CategoryID&quot; as [category!1!cid],
       &quot;CategoryName&quot; as [category!1!name],
       NULL as [product!2!pid],
       NULL as [product!2!name!element]
from &quot;Demo&quot;..&quot;Categories&quot;
union all
select 2, 1, &quot;category&quot; .&quot;CategoryID&quot;, NULL, &quot;ProductID&quot;, &quot;ProductName&quot;
    from &quot;Demo&quot;..&quot;Categories&quot; &quot;category&quot;, &quot;Demo&quot;..&quot;Products&quot; as &quot;product&quot;
    where &quot;product&quot;.&quot;CategoryID&quot; = &quot;category&quot;.&quot;CategoryID&quot;
order by [category!1!cid], 5
for xml explicit;
</pre>
    </div>

<p>
This query makes a two level tree where Categories have Product
children.  The selection in the first UNION term specifies the element
types in the result set.  The two first columns, TAG and PARENT are
required in all EXPLICIT queries.  Subsequent columns have an extended
AS declaration that specifies which element they belong to, what that
element is called in XML and what the column will be called.  A row
where TAG has a value of 1 will pick the columns which has [xxx!1!yyy]
as their alias; rows with a TAG of 2 will pick columns with an alias
with [xxx!2!yyy] and so on.
</p>
	<p>
If consecutive rows have a different TAG but the same PARENT, these
will be siblings of different types.  This possibility does not exist
with the other FOR XML modes.
</p>
	<p>
If the PARENT is 0 or NULL, then any previously open elements in the
result are closed and the element of the row becomes a top-level
element.  When PARENT refers to the TAG of a presently open element in
the set, all children of that element are closed and the row&#39;s element
is inserted as the next child of the last element with the TAG equal
to the new row&#39;s PARENT.  All open tags are closed at the end of the
result set.
</p>
<div class="note">
<div class="notetitle">Note</div>
<p>Since each level of the tree is generated by a different term in
the UNION ALL, an ORDER BY will invariably be needed to group the
children after their parents.  If the parent rows have NULLs in
place of the child row&#39;s key values, the parent gets sorted first
because NULL collates first.
</p>
</div>
<br />

<a name="examplesofforxml" />
    <h3>13.1.2. Examples of FOR XML</h3>

<p>This section gives one example of each mode of FOR XML combined
with the <span class="computeroutput">xml_auto()</span> function to help us display
the results simply. First we create a procedure that enables us to
supply SQL and return XML using the <span class="computeroutput">xml_auto()</span>
function.</p>

<div>
      <pre class="programlisting">
create procedure xmla (in q varchar)
{
  declare st any;
  st := string_output ();
  xml_auto (q, vector (), st);
  result_names (q);
  result (string_output_string (st));
}
</pre>
    </div>

<p>Now we can apply this to a couple of examples:</p>

<a name="" />
    <div class="example">
      <div class="exampletitle">XML RAW</div>
<div>
        <pre class="programlisting">
xmla (&#39;select &quot;category&quot;.&quot;CategoryID&quot;, &quot;CategoryName&quot;,
    &quot;ProductName&quot;, &quot;ProductID&quot;
    from &quot;Demo&quot;..&quot;Categories&quot; &quot;category&quot;, &quot;Demo&quot;..&quot;Products&quot; as &quot;product&quot;
    where &quot;product&quot;.&quot;CategoryID&quot; = &quot;category&quot;.&quot;CategoryID&quot; FOR XML RAW&#39;);
</pre>
      </div>
<div>
        <pre class="screen">
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Chai&quot; ProductID=&quot;1&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Chang&quot; ProductID=&quot;2&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Guaraná Fantástica&quot; ProductID=&quot;24&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Sasquatch Ale&quot; ProductID=&quot;34&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Steeleye Stout&quot; ProductID=&quot;35&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Côte de Blaye&quot; ProductID=&quot;38&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Chartreuse verte&quot; ProductID=&quot;39&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Ipoh Coffee&quot; ProductID=&quot;43&quot;&gt;
&lt;/ROW&gt;
&lt;ROW CategoryID=&quot;1&quot; CategoryName=&quot;Beverages&quot; ProductName=&quot;Laughing Lumberjack Lager&quot; ProductID=&quot;67&quot;&gt;
&lt;/ROW&gt;
.....
</pre>
      </div>
</div>

<p>As we can see, RAW mode produces a simple row-by-row account
of the data encased within the &lt;ROW.../&gt; tags.  This is the simplest
mode.</p>

<a name="" />
    <div class="example">
<div class="exampletitle">XML AUTO</div>
<div>
        <pre class="programlisting">
xmla (&#39;select &quot;category&quot;.&quot;CategoryID&quot;, &quot;CategoryName&quot;,
    &quot;ProductName&quot;, &quot;ProductID&quot;
    from &quot;Demo&quot;..&quot;Categories&quot; &quot;category&quot;, &quot;Demo&quot;..&quot;Products&quot; as &quot;product&quot;
    where &quot;product&quot;.&quot;CategoryID&quot; = &quot;category&quot;.&quot;CategoryID&quot; FOR XML AUTO ELEMENT&#39;);
</pre>
      </div>

<div>
        <pre class="screen">
&lt;category&gt;
 &lt;CategoryID&gt;1&lt;/CategoryID&gt; &lt;CategoryName&gt;Beverages&lt;/CategoryName&gt;&lt;product&gt;
 &lt;ProductName&gt;Chai&lt;/ProductName&gt; &lt;ProductID&gt;1&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Chang&lt;/ProductName&gt; &lt;ProductID&gt;2&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Guaraná Fantástica&lt;/ProductName&gt; &lt;ProductID&gt;24&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Sasquatch Ale&lt;/ProductName&gt; &lt;ProductID&gt;34&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Steeleye Stout&lt;/ProductName&gt; &lt;ProductID&gt;35&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Côte de Blaye&lt;/ProductName&gt; &lt;ProductID&gt;38&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Chartreuse verte&lt;/ProductName&gt; &lt;ProductID&gt;39&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Ipoh Coffee&lt;/ProductName&gt; &lt;ProductID&gt;43&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
 &lt;ProductName&gt;Laughing Lumberjack Lager&lt;/ProductName&gt; &lt;ProductID&gt;67&lt;/ProductID&gt;&lt;/product&gt;
&lt;product&gt;
.....
</pre>
      </div>
</div>

<p>In contrast to RAW mode, AUTO produces results that are more
tree-like.  Only one category element is used for each category, and
that contains all the children of the category.</p>
<a name="" />
    <div class="example">
<div class="exampletitle">XML EXPLICIT</div>
<div>
        <pre class="programlisting">
xmla (&#39;
select 1 as tag, null as parent,
       &quot;CategoryID&quot; as [category!1!cid],
       &quot;CategoryName&quot; as [category!1!name],
       NULL as [product!2!pid],
       NULL as [product!2!name!element]
from &quot;Demo&quot;..&quot;Categories&quot;
union all
select 2, 1, &quot;category&quot; .&quot;CategoryID&quot;, NULL, &quot;ProductID&quot;, &quot;ProductName&quot;
    from &quot;Demo&quot;..&quot;Categories&quot; &quot;category&quot;, &quot;Demo&quot;..&quot;Products&quot; as &quot;product&quot;
    where &quot;product&quot;.&quot;CategoryID&quot; = &quot;category&quot;.&quot;CategoryID&quot;
order by [category!1!cid], 5
FOR XML EXPLICIT&#39;);
</pre>
      </div>
<div>
        <pre class="screen">
&lt;CATEGORY CID=&quot;1&quot; NAME=&quot;Beverages&quot;&gt;
&lt;PRODUCT PID=&quot;1&quot;&gt;
 &lt;NAME&gt;Chai&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;2&quot;&gt;
 &lt;NAME&gt;Chang&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;24&quot;&gt;
 &lt;NAME&gt;Guaraná Fantástica&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;34&quot;&gt;
 &lt;NAME&gt;Sasquatch Ale&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;35&quot;&gt;
 &lt;NAME&gt;Steeleye Stout&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;38&quot;&gt;
 &lt;NAME&gt;Côte de Blaye&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;39&quot;&gt;
 &lt;NAME&gt;Chartreuse verte&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;43&quot;&gt;
 &lt;NAME&gt;Ipoh Coffee&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;67&quot;&gt;
 &lt;NAME&gt;Laughing Lumberjack Lager&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;70&quot;&gt;
 &lt;NAME&gt;Outback Lager&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;75&quot;&gt;
 &lt;NAME&gt;Rhönbräu Klosterbier&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;PRODUCT PID=&quot;76&quot;&gt;
 &lt;NAME&gt;Lakkalikööri&lt;/NAME&gt;&lt;/PRODUCT&gt;
&lt;/CATEGORY&gt;
&lt;CATEGORY CID=&quot;2&quot; NAME=&quot;Condiments&quot;&gt;
&lt;PRODUCT PID=&quot;3&quot;&gt;
.....
</pre>
      </div>
</div>
<p>In this example, we specify precisely the tree structure we
wish, and construct the EXPLICIT query to produce that tree.  Many
times programmers know what the resulting XML should look
like but do not know how to get exactly what they want.  FOR XML
EXPLICIT can be very useful in these cases.</p>
<br />


<a name="forxmlfunc" />
    <h3>13.1.3. Functions</h3>

<a href="fn_xml_auto.html">xml_auto()</a>

<br />

<a name="forxmlsyntax" />
    <h3>13.1.4. FOR XML Syntax</h3>

<div>
      <pre class="programlisting">
for__xml ::= FOR XML &lt;mode&gt; [ ELEMENT ]

&lt;mode&gt; ::= RAW | AUTO | EXPLICIT

&lt;explicit column&gt; ::=  scalar_exp AS &#39;[&#39; &lt;element&gt; &#39;!&#39; &lt;tag no&gt; &#39;!&#39;
                   &lt;column name&gt; [ &#39;!&#39; &lt;option&gt; ] &#39;]&#39;

&lt;tag no&gt; ::= INTNUM

&lt;column name&gt; ::= IDENTIFIER

&lt;element&gt; ::= IDENTIFIER

&lt;option&gt; ::= IDENTIFIER

</pre>
    </div>
<p>
The &lt;explicit column&gt; should be used in the selection list of
the first term of the UNION ALL construct in a FOR XML EXPLICIT query.
Virtuoso provides this functionality separately from any Web server
context, although these are principally expected to be used inside VSP
pages.
</p>
<p>
The text of &lt;option&gt; part of the &lt;explicit column&gt; is
ignored but if it is present then the value is placed into
a sub-element of the element for the row, not into an attribute.
</p>
<br />

<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="webandxml.html" title="XML Support">Previous</a>
          <br />Contents of XML Support</td>
     <td align="center" width="34%">
          <a href="webandxml.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)">Next</a>
          <br />XML Composing Functions in SQL Statements (SQLX)</td>
    </tr>
    </table>
  </div>
  <div id="footer">
    <div>Copyright© 1999 - 2009 OpenLink Software All rights reserved.</div>
   <div id="validation">
    <a href="http://validator.w3.org/check/referer">
        <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
    </a>
    <a href="http://jigsaw.w3.org/css-validator/">
        <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
    </a>
   </div>
  </div>
 </body>
</html>