Sophie

Sophie

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

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="forxmlforsql.html" title="Rendering SQL Queries as XML (FOR XML Clause)" />
  <link rel="next" href="xmlservices.html" title="Virtuoso XML Services" />
  <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="composingxmlinsql" />
    <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="forxmlforsql.html" title="Rendering SQL Queries as XML (FOR XML Clause)">Prev</a> | <a class="link" href="xmlservices.html" title="Virtuoso XML Services">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>
      <a href="forxmlforsql.html">Rendering SQL Queries as XML (FOR XML Clause)</a>
   </div>
   <div class="selected">
      <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="composingxmlinsql" />
    <h2>13.2. XML Composing Functions in SQL Statements (SQLX)</h2>
<p>
The preferred means of constructing XML data from SQL is to use the standard SQLX SQL extension.
</p>
<p>SQLX is a collection of functions added for creating XML entities from standard
relational queries. Basically, you write a SQL statement with calls to SQLX functions in
the selection  and a piece of XML is created.
</p>
<p>
There are five XML composing functions:
</p>
<ul>
      <li>
        <a href="fn_XMLELEMENT.html">
XMLELEMENT()
</a>
creates a single XML element that can contain an arbitrary number of attributes and sub-elements.</li>
      <li>
        <a href="fn_XMLATTRIBUTES.html">
XMLATTRIBUTES()
</a>
lists XML attributes to be placed in the XML element created by an enclosing call of XMLELEMENT().</li>
      <li>
        <a href="fn_XMLCONCAT.html">
XMLCONCAT()
</a>
returns a forest of XML values by concatenating a list of XML values or forests.</li>
      <li>
        <a href="fn_XMLAGG.html">
XMLAGG()
</a>
is an aggregate function that creates a forest of XML values by concatenating the XML values that are returned from multiple rows.</li>
      <li>
        <a href="fn_XMLFOREST.html">
XMLFOREST()
</a>
is similar to XMLATTRIBUTES() but returns a forest of elements instead of list of attributes.</li>
    </ul>
<p>These functions belong
to the SQLX standard, an emerging SQL standard for XML. All the functions take a variable number of arguments.
</p>
<p>
<span class="computeroutput">XMLELEMENT</span> is used to construct XML elements from relational data.
It takes as parameters the element name, an optional collection of attributes for the element
(returned by <span class="computeroutput">XMLATTRIBUTES</span> call),
column names, strings, <span class="computeroutput">XMLELEMENT</span>, <span class="computeroutput">XMLFOREST</span>,
<span class="computeroutput">XMLCONCAT</span>, and <span class="computeroutput">XMLAGG</span> calls, and an entity objects (returned by
corresponding functions, e.g. <span class="computeroutput">xtree_doc</span>, <span class="computeroutput">xpath_eval</span>,
<span class="computeroutput">xquery_eval</span>) which will make up the content of the element (an exception from this is
an attribute entity returned by <span class="computeroutput">xquery_eval</span> - in this case it is joined to the list of the
element&#39;s attributes).
Column names, strings and attribute entities returned by <span class="computeroutput">xpath_eval</span> will make up the text content
of the element.
The others will make up the children of the element.
</p>
<p>
In the <span class="computeroutput">XMLATTRIBUTES</span> clause, the value expressions are evaluated to get the values for the
attributes.
</p>
<p>
<span class="computeroutput">XMLFOREST</span> produces a forest of XML elements from a given list of arguments. It accepts a list of
SQL value expressions as its arguments, and produces an XML element from each value returned.
</p>
<p>
<span class="computeroutput">XMLCONCAT</span> produces a forest of elements by concatenating a list of XML values. XMLCONCAT accepts
a list of XML value expressions as its arguments, and produces a forest of elements by concatenating the XML values
that are returned from the same row to make one value. If an argument of the <span class="computeroutput">XMLCONCAT</span> is an
entity object returned by <span class="computeroutput">xquery_eval</span> or <span class="computeroutput">path_eval</span>, it must not be an
attribute.
</p>
<p>
The following statements create the same result sets:
</p>
<div>
      <pre class="programlisting">
select XMLELEMENT (&#39;Person&#39;,
                        XMLELEMENT (&#39;firstname&#39;, &quot;FirstName&quot;),
                        XMLELEMENT (&#39;lastname&#39;, &quot;LastName&quot;),
  		        xquery_eval(&#39;//country&#39;, xtree_doc(&#39;&lt;a&gt;&lt;country&gt;USA&lt;/country&gt;&lt;/a&gt;&#39;)))
from &quot;Demo&quot;.&quot;demo&quot;.&quot;Employees&quot;;

select
      XMLELEMENT (&#39;Person&#39;,
                        XMLFOREST (&quot;FirstName&quot;as &quot;firstname&quot;, &quot;LastName&quot; as &quot;lastname&quot;),
  		        xquery_eval(&#39;//country&#39;, xtree_doc(&#39;&lt;a&gt;&lt;country&gt;USA&lt;/country&gt;&lt;/a&gt;&#39;)))
from &quot;Demo&quot;.&quot;demo&quot;.&quot;Employees&quot;;

select
      XMLELEMENT (&#39;Person&#39;,
                             XMLCONCAT (
                                        XMLELEMENT (&#39;firstname&#39;, &quot;FirstName&quot;),
                                        XMLELEMENT (&#39;lastname&#39;, &quot;LastName&quot;),
  		                        xquery_eval(&#39;//country&#39;, xtree_doc(&#39;&lt;a&gt;&lt;country&gt;USA&lt;/country&gt;&lt;/a&gt;&#39;))))
from &quot;Demo&quot;.&quot;demo&quot;.&quot;Employees&quot;;
</pre>
    </div>
<div class="note">
<div class="notetitle">Note:</div>
<p>
The second statement is more effective than the others.
</p>
</div>
<p>
In order to return more than one row of values, you can use <span class="computeroutput">XMLAGG</span>. <span class="computeroutput">XMLAGG</span>
is an aggregate function that produces a forest of XML elements from a collection of XML elements. It concatenates
the values returned from one column of multiple rows, unlike XMLCONCAT, which concatenates the values
returned from multiple columns in the same row.
</p>
<p>
The parameters that would be used as element names (in the <span class="computeroutput">XMLELEMENT</span> and in the
&#39;AS clause &#39; of the <span class="computeroutput">XMLFOREST</span> and <span class="computeroutput">XMLATTRIBUTES</span>)
must be valid XML names. If the &#39;AS clause &#39; is absent in a list of the parameters of the
<span class="computeroutput">XMLFOREST</span> or <span class="computeroutput">XMLATTRIBUTES</span>, Virtuoso uses the partially escaped form of
the column name as the element or attribute name. The partially escaped form means that SQL &lt;identifier&gt;
characters that are valid characters in a XML NAME are not changed, SQL &lt;identifier&gt;
character that is not valid XML NAME  character is replaced with &quot;_xHHHH_&quot;, where HHHH is character&#39;s upper case
hexadecimal code. For example, &quot;first_name&quot; is replaced with &quot;first_x005F_name&quot;, &quot;last name&quot; is replaced with
&quot;last_x0020_name&quot;.
</p>
<p>The following example creates an &#39;FullAddress&#39; element with
<ul>
<li> four attributes, three of them (&#39;PostalCode&#39;, &#39;Address&#39;, &#39;City&#39;) are
   produced by XMLATTRIBUTES, and the fourth attribute - &#39;country&#39; is calculated by
   xquery_eval </li>
<li>&#39;Region&#39; subelement, that is produced by xtree_doc</li>
<li>text content, that is produced by xpath_eval </li>
<li>&#39;emp&#39; subelement with text content from the column &quot;LastName&quot;, that is created by nested
  XMLELEMENT</li>
</ul>
</p>
<div>
      <pre class="programlisting">
select XMLELEMENT (&#39;FullAddress&#39;,
                        XMLATTRIBUTES ( &quot;PostalCode&quot;, &quot;Address&quot;, &quot;City&quot;),
                        xtree_doc (&#39;&lt;Region&gt;WA&lt;/Region&gt;&#39;),
                        xquery_eval(&#39;//@country&#39;, xtree_doc(&#39;&lt;a country=&quot;USA&quot;/&gt;&#39;)),
                        xpath_eval(&#39;//@Phone&#39;, xtree_doc(&#39;&lt;a Phone=&quot;(206) 555-9857&quot;/&gt;&#39;)),
                        XMLELEMENT(&#39;emp&#39;, &quot;LastName&quot;))
from &quot;Demo&quot;.&quot;demo&quot;.&quot;Employees&quot;

----------------------------

   &lt;FullAddress PostalCode=&quot;98122&quot; Address=&quot;507 - 20th Ave. E. Apt. 2A&quot; City=&quot;Seattle&quot; country=&quot;USA&quot;&gt;
        &lt;Region&gt;WA&lt;/Region&gt;
        (206) 555-9857
        &lt;emp&gt;Davolio&lt;/emp&gt;
    &lt;/FullAddress&gt;
    &lt;FullAddress PostalCode=&quot;98401&quot; Address=&quot;908 W. Capital Way&quot; City=&quot;Tacoma&quot; country=&quot;USA&quot;&gt;
        &lt;Region&gt;WA&lt;/Region&gt;
        (206) 555-9857
        &lt;emp&gt;Fuller&lt;/emp&gt;
    &lt;/FullAddress&gt;
  . . .

 
</pre>
    </div>
<div class="tip">
<div class="tiptitle">See Also:</div>
<p>
<a href="fn_XMLAGG.html">XMLAGG()</a>
</p>
<p>
<a href="fn_XMLATTRIBUTES.html">XMLATTRIBUTE()</a>
</p>
<p>
<a href="fn_XMLCONCAT.html">XMLCONCAT()</a>
</p>
<p>
<a href="fn_XMLELEMENT.html">XMLELEMENT()</a>
</p>
<p>
<a href="fn_XMLFOREST.html">XMLFOREST()</a>
</p>
</div>
<p>
XML composing functions deal with arguments of arbitrary type, but the result is always an XML entity that can contain only elements and strings.
Hence there is a set of type casting rules. These rules are quite common for any XML DOM model, so they&#39;re similar to those listed for
<a href="xmldom.html#xmldomtypecasting">DOM function arguments</a>:
</p>
<p>
If an instance of XMLType is passed then its internal XML entity is used.
</p>
<p>
If an array representation of an XML tree entity is passed then it is used exactly like XML entity.
</p>
<p>
If an argument is NULL then it is fully ignored, as if there is no such argument at all.
</p>
<p>
If an argument is not of a type listed above and not a string then it is cast to a string first.
</p>
<p>
A root node of some document (or of some generic XML entity) can not appear in the middle of the resulting tree.
So if a root node is passed then all child nodes of the root (i.e. every top-level node of the document) will be added.
</p>
<p>
SQL/XML standards introduce a special name &quot;forest of XML elements&quot; for an ordered list of XML elements,
like one returned by <span class="computeroutput">XMLFOREST()</span>.
In Virtuoso, forest can contain XML nodes of any sort, not only XML elements, so it can also contain strings, processing instructions and comments.
Virtuoso processes any non-empty &quot;forest&quot; as if it were the root node of a &quot;generic XML entity&quot;,
and items of the forest were top-level nodes of that entity.
Hence, a forest can be passed to any function that accepts an value of type &quot;XML entity&quot;.
The only potential problem is that this entity is well-formed if and only if the forest is non-empty.
If an empty forest is serialized to an XML text then the result is an empty string that is not an acceptable input for an XML parser.
</p>
<p>
It is important to remember that the XML document can not contain two neighbour text nodes and that the text node can not be an empty string.
If two consequent strings appear in the list of values of a forest or in the list of children of an new element then
they are replaced with a single node that is a concatenation of these string.
Similarly, if an empty string appears in the list of values of a forest or in the list of children of an new element then it is removed from the list.
</p>
<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="forxmlforsql.html" title="Rendering SQL Queries as XML (FOR XML Clause)">Previous</a>
          <br />Rendering SQL Queries as XML (FOR XML Clause)</td>
     <td align="center" width="34%">
          <a href="webandxml.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="xmlservices.html" title="Virtuoso XML Services">Next</a>
          <br />Virtuoso XML Services</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>