Sophie

Sophie

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

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="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)" />
  <link rel="next" href="queryingxmldata.html" title="Querying Stored XML Data" />
  <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="xmlservices" />
    <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="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)">Prev</a> | <a class="link" href="queryingxmldata.html" title="Querying Stored XML Data">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>
      <a href="composingxmlinsql.html">XML Composing Functions in SQL Statements (SQLX)</a>
   </div>
   <div class="selected">
      <a href="xmlservices.html">Virtuoso XML Services</a>
    <div>
        <a href="#xpath_sql" title="XPATH Implementation and SQL">XPATH Implementation and SQL</a>
        <a href="#XPATHopts" title="XPATH Query Options">XPATH Query Options</a>
        <a href="#xmlviews1" title="XML Views - Representing SQL Data as Dynamic and Persistent XML">XML Views - Representing SQL Data as Dynamic and Persistent XML</a>
        <a href="#xmlviewextentref" title="External Entity References in Stored XML">External Entity References in Stored XML</a>
        <a href="#xpproc" title="Using XPATH in SQL Queries and Procedures">Using XPATH in SQL Queries and Procedures</a>
        <a href="#xquery_sql" title="XQUERY and XML view">XQUERY and XML view</a>
        <a href="#mapping_schemas" title="Mapping Schemas as XML Views">Mapping Schemas as XML Views</a>
        <a href="#view4xmldifferences" title="Differences Between SQLX, FOR XML and XML Views">Differences Between SQLX, FOR XML and XML Views</a>
    </div>
   </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="xmlservices" />
    <h2>13.3. Virtuoso XML Services</h2>
	
		<a name="xpath_sql" />
    <h3>13.3.1. XPATH Implementation and SQL</h3>
		<p>
Virtuoso offers XPATH as a query language for XML views.
The statement is there converted into SQL in the context of the mapping defined by the __view XPATH option, which is mandatory.
An XPATH query string is a valid top level SQL statement.  This is interpreted as a single select or union of selects with the result columns being specified by various XPATH options.
	</p>
		<p>
The basic query string
	</p>
		<div>
      <pre class="programlisting">
XPATH [__view &quot;cat&quot;]/category
</pre>
    </div>
		<p>
will select any top level category elements from the cat XML view, defined with CREATE XML VIEW.  This has a single result column with the serialization string of the selected entity as value. This string starts with the category start tag and ends with the
corresponding end tag. As many result rows are generated as there are top level category nodes in the view.
	</p>
		<p>
This basic behaviour can be modified by XPATH options enclosed in brackets after the XPATH keyword.  These options allow specifying the output columns of the generated select statement.
	</p>
	<br />
	
		<a name="XPATHopts" />
    <h3>13.3.2. XPATH Query Options</h3>
 
		<div>
      <pre class="programlisting">
&lt;xp option&gt; ::=
    __* | __http | __key   | __view NAME | __tag NAME | __quiet | __base_uri STRING |
    xmlns:NAME &#39;=&#39; STRING | xmlns &#39;=&#39; STRING |
    __lang STRING | __enc STRING | KEYWORD &#39;=&#39; KEYWORD

&lt;option list&gt; ::=  | &lt;option list&gt; &#39;[&#39; &lt;option&gt; [...] &#39;]&#39;
</pre>
    </div>
		<p>
The option list may occur between the XPATH keyword and the start of the path,
e.g.
	</p>
		<div>
      <pre class="programlisting">
XPATH [__key __view &quot;cat&quot;] /category
</pre>
    </div>
		<p>The effects of the options are as follows:
</p>
		<div class="formalpara">
			<strong>__http</strong>
			<p>Send the serialization of the result entities to the HTTP client. This may only be used inside a VSP page context.
</p>
		</div>
		<div class="formalpara">
			<strong>__key</strong>
			<p>Select the key of the selected entities instead of the serialization text.
</p>
		</div>
		<div class="formalpara">
			<strong>__*</strong>
			<p>Select all columns of the selected entity instead of its serialization text.  This is only valid when __view is specified and the result set is homogeneous.
</p>
		</div>
		<div class="formalpara">
			<strong>__view STRING</strong>
			<p>Specify that the query be interpreted in the context of the specified CREATE XML VIEW, directly accessing the tables.
				This is mandatory since the elements referenced can only be mapped to tables in the context of an XML view.
</p>
		</div>
		<div class="formalpara">
			<strong>__quiet</strong>
			<p>Specify that the query should not signal non-fatal errors. This option is for cases when an incomplete result is anyway better than nothing.
Typical example is search in the collection of documents where not all documents are valid.
</p>
		</div>
		<div class="formalpara">
			<strong>__base_uri STRING</strong>
			<p>Specify the base URI that can be used to resolve relative URIs in calls of XPATH functions
<a href="xpf_processXQuery.html">processXQuery()</a>,
<a href="xpf_processXSLT.html">processXSLT()</a> and
<a href="xpf_processXSQL.html">processXSLT()</a>.
This is similar to the effect of &quot;declare base-uri&quot; in XQuery.
</p>
		</div>

		<div class="formalpara">
			<strong>xmlns:NAME &#39;=&#39; STRING</strong>
			<p>This declares a pair of namespace prefix and namespace URI for use in the query expression,
e.g. xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; or xmlns:ora=&quot;http://schemas.oracle.com/xpath/extension&quot;</p>
		</div>
		<div class="formalpara">
			<strong>xmlns &#39;=&#39; STRING</strong>
			<p>This declares the default namespace URI for use in the query expression,
e.g. xmlns=&quot;http://www.example.com/my-schema&quot;. This namespace will become both default element namespace and default function namespace.</p>
		</div>
		<div class="formalpara">
			<strong>__lang STRING</strong>
			<p>This declares the language that is used for text search expressions.
This is for internationalization purposes only. See subsection <a href="cinterface.html#langfuncapi">&quot;Adding New Languages And Encodings Into Virtuoso&quot;</a>
for more details.
			</p>
		</div>
		<div class="formalpara">
			<strong>__enc STRING</strong>
			<p>This declares encoding of strings that are used for text search expressions.
This is for internationalization purposes only. See subsections <a href="queryingxmldata.html#encodingxpathexp">&quot;Encoding in XPath Expressions&quot;</a>
and <a href="cinterface.html#langfuncapi">&quot;Adding New Languages And Encodings Into Virtuoso&quot;</a> for more details.
			</p>
		</div>
		<div class="formalpara">
			<strong>KEYWORD &#39;=&#39; KEYWORD</strong>
			<p>This is useful only if the expression uses XPATH functions like <a href="xpf_document.html">document()</a>
or <a href="xpf_document.html">doc()</a>  or if the XPATH expression is an argument of
<a href="">xpath_contains</a> or similar special SQL predicate.
These are configuration options that XPATH processor provides to the XML parser when the processor should read a document.
Section <a href="xmlschema.html#dtd_config">&quot;Configuration Options of the DTD Validator&quot;</a> lists all supported options.
			</p>
		</div>
	<br />
	
		<a name="xmlviews1" />
    <h3>13.3.3. XML Views - Representing SQL Data as Dynamic and Persistent XML</h3>
		<p>
The XML view mechanism allows generating XML content from relational data and to query relational
data as if it were XML without first converting it to XML.
	</p>
		
			<a name="createxmlview" />
    <h4>13.3.3.1. CREATE XML VIEW statement </h4>
			<div>
      <pre class="programlisting">
&lt;xml view&gt; ::=
     CREATE XML VIEW &lt;name&gt; as [ &lt;namespaces_def&gt; ] &lt;element list&gt; &lt;opt_public&gt;

&lt;xml view&gt; ::=
     CREATE XML VIEW &lt;name&gt; as [ &lt;namespaces_def&gt; ] &lt;query spec&gt; [ELEMENT] &lt;opt_public&gt;

&lt;namespaces_def&gt; ::= &#39;[&#39; &lt;id_namespace&gt; &#39;=&#39;( name | &lt;path string&gt; ) [ ...]  &#39;]&#39;

&lt;element list&gt; ::= &lt;element&gt; [, ...]*

&lt;element&gt; ::=
	 &lt;table&gt; &lt;correlation name&gt; AS [ &lt;id_namespace&gt; &#39;:&#39; ] &lt;element&gt; &lt;columns&gt;
	 [ ON &#39;(&#39;  &lt;search condition&gt; &#39;)&#39;]
	[primary key &#39;(&#39; column_commalist &#39;)&#39; ]
	[ELEMENT]
	 [ &#39;{&#39; &lt;element list&gt;&#39;}&#39; ]

&lt;opt_public&gt; ::=  PUBLIC &lt;path string&gt; OWNER &lt;DAV owner name&gt; [PERSISTENT] [INTERVAL &lt;minutes&gt;]

&lt;columns&gt; ::= &#39;(&#39; &lt;column&gt; [, ...] &#39;)&#39;

&lt;column&gt; ::=   &lt;column name&gt; | &lt;column name&gt; AS [ &lt;id_namespace&gt; &#39;:&#39; ] &lt;attribute name&gt;

&lt;id_namespace&gt; :: = identifier
</pre>
    </div>
			<p>
The XML view declaration establishes a &#39;virtual document&#39; a context within
which XML hierarchy relationships can be translated into arbitrary joins. The virtual
document can be then materialized into an actual set of persistent XML elements or used to generate
SQL from XPATH.
	</p>
			<p>
Each table in the declaration generates an element into the result
document. SQL views can be used as tables to accommodate for hidden joins, sub-queries, ordering and
aggregates. If a view is used, which by nature has no primary key, the primary key
clause should be used to define a uniquely identifying set of view columns.
	</p>
			<p>
The only restriction on the XML view declaration is that each branch has a fixed
depth.
	</p>
			<p>
The structure of joins used to make the text can be specified in two ways: As a SQL query specification,
that is a SELECT from a list of tables with a WHERE clause specifying the joins, or as a tree of
join elements.  The first form is called automatic and the second is called explicit. The automatic form allows
generating a tree with as many levels as there are tables in the join, with elements derived from the rows at each
level occupying each level of the hierarchy
</p>
			<p>
With both forms the columns of the tables are mapped into either attributes or child elements
of the element representing each row. In the explicit mode, the attribute / element choice can be made for
each table, in the automatic mode for the entire view.  The explicit mode also allows specifying a different
element / attribute name whereas the automatic mode takes the name from the column name.  Even if the
columns are presented as child elements in the output text, they should be referenced as attributes in
XPATH queries evaluated in the context of the view.
</p>
			<p>
An XML namespaces can be used in XML view with two restrictions -
all namespace names must be different and
the first three letters of namespace name must not be &#39;xml&#39;, except for
default namespace with name &#39;xmlns&#39;.
</p>
		<br />
		
			<a name="explicitxmlviews" />
    <h4>13.3.3.2. Explicit XML Views </h4>
			<p>
In the explicit form each level of the hierarchy is declared as a list of child elements. Each such element maps
one table or view into an entity according to a join condition.  The join conditions
can reference columns from the associated table and columns from tables in parent elements.
The join condition can also have scalar filtering conditions. A top element&#39;s join
condition may only specify scalar conditions.
</p>
			<p>
Each set of sibling child nodes is delimited by braces {}.  The top
level of the view typically consists of one element in the outermost braces.  This element
has itself a child list delimited by braces.  Each such list can have more than
one different element.
</p>
			<p>
Each element specifies:
</p>
			<ul>
				<li>
					<p>SQL table</p>
				</li>
				<li>
					<p>Correlation name for use in subsequent joins for this table</p>
				</li>
				<li>
					<p>XML element name to use for delimiting a row of this table</p>
				</li>
				<li>
					<p>List of columns, with optional XML element or attribute names</p>
				</li>
				<li>
					<p>join condition - will relate rows of this table to rows of the table in the enclosing element.
  If this element is at the top level, this can only consist of scalar conditions</p>
				</li>
				<li>
					<p>Optional PRIMARY KEY clause, needed if the table in this element is a view, does not
  have a primary key or if a non-primary key unique identity is desired</p>
				</li>
				<li>
					<p>Optional ELEMENT flag</p>
				</li>
				<li>
					<p>Optional list of child elements, delimited by braces</p>
				</li>
			</ul>
			<div class="note">
				<div class="notetitle">Note:</div>
				<p>A correlation name is mandatory for all the tables.</p>
			</div>
			<p>
The column list can mention a single column or a single column renamed into an
XML attribute of a different name. If a column of a table is referenced in a subsequent join
condition it must appear in the output columns list.  Expressions are not directly allowed
but a view with expression columns can be used.
	</p>
			<p>
The opt_public clause, when present, offers a shorthand for calling xml_view_publish
at the same time as making the definition.  This makes a DAV resource reflecting the contents
of the view.  The contents may either be generated on demand or persisted as a DAV accessible XML document.
In the latter case the document may be regenerated at a fixed interval. The interval is
expressed in minutes.
	</p>
			<p>
The path is expressed as an absolute path from the root collection of the DAV server.
	</p>
			<div class="note">
				<div class="notetitle">Note:</div>
				<p>This root collection may be mapped into various places in the web server&#39;s URL space.
</p>
			</div>
			<div>
      <pre class="programlisting">
create xml view xx ... public &#39;/xx.xml&#39; owner &#39;dav&#39; persistent interval 1;
</pre>
    </div>
			<p>
is equivalent to:
	</p>
			<div>
      <pre class="programlisting">
create xml view xx ...;
xml_view_publish (xx, /xx.xml&#39;, &#39;dav&#39;, 1, 1);
</pre>
    </div>
			<p>
A DAV resource created in this manner can be deleted as any DAV resource.  The XML view
itself is not affected but a possibly existing refresh job will be automatically deleted.
	</p>
			<p>
One XML view can be published several times with different names and owners.  There may also exist
persistent and non-persistent publications of the same view.
	</p>
			<p>
The CREATE XML VIEW statement defines stored procedures for generating an XML text fragment corresponding
to each element declared in the view.
	</p>
			<p>
The names of the procedures are composed as follows:
	</p>
			<div>
      <pre class="programlisting">
create procedure http_view_&lt;view name&gt;
	(inout output_mode)
</pre>
    </div>
			<div>
      <pre class="programlisting">
create procedure http_&lt;view name&gt;_&lt;element name&gt;_&lt;correlation name&gt;
	(in pk1 any, ..., in output_mode integer)
</pre>
    </div>
			<p>
An http output procedure is created for each &lt;element&gt; in the create xml view declaration. It takes
the primary key columns of the table in question in key order plus a mode flag. It then outputs the
serialization of the specified element and any child elements. For an output mode of 0 the result
goes directly to an HTTP client. For an output_mode of 1 the procedure returns the serialization as a string.
Note that for this to work the tables in question must be real tables and the join conditions must
only reference the next higher table in the create xml view tree.  Further, the primary
key columns of each table should be mentioned in the columns list for that table along
with any foreign keys referenced in subsequent join conditions.
</p>
			<a name="vht01" />
    <div class="example">
				<div class="exampletitle">Examples</div>
				<div>
        <pre class="programlisting">
create xml view &quot;cat&quot; as
{
  &quot;Demo&quot;.&quot;demo&quot;.&quot;Categories&quot; &quot;C&quot; as &quot;category&quot;
	(&quot;CategoryID&quot;, &quot;Description&quot; as &quot;description&quot;)
    {
      &quot;Demo&quot;.&quot;demo&quot;.&quot;Products&quot; &quot;P&quot; as &quot;product&quot;  (&quot;ProductName&quot;)
	on (&quot;P&quot;.&quot;CategoryID&quot; = &quot;C&quot;.&quot;CategoryID&quot;)
    }
}
</pre>
      </div>
				<p>
This declares a two level hierarchy with a category node for each category
and a product child node for each product in the category.
</p>
				<div>
        <pre class="programlisting">
create xml view &quot;cats_e&quot; as
  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; element;
</pre>
      </div>
				<p>Here is a similar example, this time using the element option.</p>
			</div>
			<p>
The procedures are
</p>
			<div>
      <pre class="programlisting">
xmlg_cat
http_cat_category_C (in categoryid any, in _out integer);
http_cat_product_P (in productid any, in _out integer);
</pre>
    </div>
			<p>
In the following example the function returns the selected items as an XML fragment.
Consecutive elements are separated by new-lines for readability.
</p>
			<a name="vht02" />
    <div class="example">
				<div class="exampletitle">Example</div>
				<div>
        <pre class="programlisting">
SQL&gt; call &quot;http_cat_category_C&quot; (1, 1);

1 sets? Done. -- 5 msec.
RESULT=
&lt;category CategoryID=&quot;1&quot; description=&quot;Soft drinks, coffees, teas, beers, and ales&quot; &gt;
&lt;product ProductName=&quot;Chai&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Chang&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Guarana Fantastica&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Sasquatch Ale&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Steeleye Stout&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Côte de Blaye&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Chartreuse verte&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Ipoh Coffee&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Laughing Lumberjack Lager&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Outback Lager&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Rhönbrooou Klosterbier&quot; &gt;&lt;/product&gt;
&lt;product ProductName=&quot;Lakkalikööri&quot; &gt;&lt;/product&gt;
&lt;/category&gt;
</pre>
      </div>
			</div>
			<p>
The below example shows how to use a SQL view for hiding a join.
The below view generates for each table a set of column children and a set of
index children, which in turn have column children.
</p>
			<div>
      <pre class="programlisting">
create view KEY_COLS as select KP_KEY_ID, KP_NTH, C.*
	from SYS_KEY_PARTS, SYS_COLS C where COL_ID = KP_COL;

create xml view &quot;schema&quot; as
{
  DB.DBA.SYS_KEYS k as &quot;table&quot; (&quot;KEY_TABLE&quot; as &quot;name&quot;,
	KEY_ID as &quot;key_id&quot;, KEY_TABLE as &quot;table&quot;)
	on (k.KEY_IS_MAIN = 1 and k.KEY_MIGRATE_TO is null)
	{ DB.DBA.KEY_COLS  c as &quot;column&quot; (COLUMN as name)
		on (k.KEY_ID = c.KP_KEY_ID)
		primary key (COL_ID),
	DB.DBA.SYS_KEYS i as &quot;index&quot; (KEY_NAME
	  as &quot;name&quot;, KEY_ID as &quot;key_id&quot;, KEY_N_SIGNIFICANT as &quot;n_parts&quot;)
	  on (i.KEY_TABLE = k.KEY_TABLE and i.KEY_IS_MAIN = 0 and i.KEY_MIGRATE_TO is null)
	  {
	    DB.DBA.KEY_COLS ic as &quot;column&quot; (COLUMN as &quot;name&quot;)
	      on (ic.KP_NTH &lt; i.KEY_N_SIGNIFICANT and ic.KP_KEY_ID = i.KEY_ID)
	      primary key (COL_ID)
	      }
	}
};
</pre>
    </div>
			<p>
The following query will return  the subtree describing the Customers table
in the demo database:
</p>
			<div>
      <pre class="programlisting">
XPATH [__view &#39;schema&#39;]
	/table[@name = &#39;Demo.demo.Customers&#39;];

&lt;table name=&quot;0&quot; key_id=&quot;1011&quot; table=&quot;Demo.demo.Customers&quot; &gt;
&lt;column name=&quot;CustomerID&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;CompanyName&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;ContactName&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;ContactTitle&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;Address&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;City&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;Region&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;PostalCode&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;Country&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;Phone&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;Fax&quot; &gt;&lt;/column&gt;
&lt;index name=&quot;City&quot; key_id=&quot;1012&quot; n_parts=&quot;2&quot; &gt;
&lt;column name=&quot;City&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;CustomerID&quot; &gt;&lt;/column&gt;
&lt;/index&gt;
&lt;index name=&quot;CompanyName2&quot; key_id=&quot;1013&quot; n_parts=&quot;2&quot; &gt;
&lt;column name=&quot;CompanyName&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;CustomerID&quot; &gt;&lt;/column&gt;
&lt;/index&gt;
&lt;index name=&quot;PostalCode2&quot; key_id=&quot;1014&quot; n_parts=&quot;2&quot; &gt;
&lt;column name=&quot;PostalCode&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;CustomerID&quot; &gt;&lt;/column&gt;
&lt;/index&gt;
&lt;index name=&quot;Region&quot; key_id=&quot;1015&quot; n_parts=&quot;2&quot; &gt;
&lt;column name=&quot;Region&quot; &gt;&lt;/column&gt;
&lt;column name=&quot;CustomerID&quot; &gt;&lt;/column&gt;
&lt;/index&gt;
&lt;/table&gt;
</pre>
    </div>
		<br />
		
			<a name="xmlviewfromselect" />
    <h4>13.3.3.3. Automatic XML Views - Creating XML Views from SELECT Statements</h4>
			<p>
The automatic form of CREATE XML VIEW will take a select statement
and infer a hierarchy from it, based on the order of tables in the from
clause. The parent table should be to the left of its children.  This is practical
if the tables form a hierarchy in application terms, like orders and order lines or
departments and employees.  This notation allows arbitrary depth but all siblings
at the same level will be of the same type.  Elements of child rows will be
child elements of the element of their parent row, where the join condition
identifies the child rows for one parent row.
</p>
			<p>
The columns in the selection will appear as attributes or child elements of the rows selected.
The names of the attributes will be the names of the columns.  The names of the siblings will be the
names of the tables in the from clause, without qualifiers or owners. Expressions should not
appear in the selection.  If the use of expressions is required then you may create a SQL view
first to facilitate this.
</p>
			<p>
The ELEMENT keyword may be present at the end of the select, before
the publishing keywords.  This will cause all columns to be represented as child
elements of the element corresponding to the row.  Note that even if the element
switch is present, the values will appear like attributes in an XPATH query inside the view.
</p>
			<div class="note">
				<div class="notetitle">Note</div>
				<p>SQL views or derived tables may not appear directly in the select.  The reason
for this is that a procedure is generated for each level of the generated XML tree and
that this must take unique identifying column values for the element in question.
If one desires to use a view, the explicit form should be used, with the primary key
option specified where appropriate.
</p>
			</div>
			
				<a name="addtxt4xmlview" />
    <h5>13.3.3.3.1. Add to text for explicit XML views</h5>
				<p>
Each set of sibling child nodes is delimited by braces {}.  The top
level of the view typically consists of one element in the outermost braces.  This element
has itself a child list delimited by braces.  Each such list can have more than
one different element.
</p>
				<p>
Each element specifies:
</p>
				<ul>
					<li>
						<p>SQL table</p>
					</li>
					<li>
						<p>Correlation name for use in subsequent joins for this table</p>
					</li>
					<li>
						<p>XML element name to use for delimiting a row of this table</p>
					</li>
					<li>
						<p>List of columns, with optional XML element or attribute names</p>
					</li>
					<li>
						<p>join condition - will relate rows of this table to rows of the table in the enclosing element.
  If this element is at the top level, this can only consist of scalar conditions</p>
					</li>
					<li>
						<p>Optional PRIMARY KEY clause, needed if the table in this element is a view, does not
  have a primary key or if a non-primary key unique identity is desired</p>
					</li>
					<li>
						<p>Optional ELEMENT flag</p>
					</li>
					<li>
						<p>Optional list of child elements, delimited by braces</p>
					</li>
				</ul>
				<a name="" />
    <div class="example">
					<div class="exampletitle">Add to examples:</div>
					<div>
        <pre class="programlisting">
create xml view &quot;cats_e&quot; as
  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; element;

Add to text: after &#39;free text and xml&#39;
</pre>
      </div>
				</div>
			<br />
		<br />
		
			<a name="xmlviewpublish" />
    <h4>13.3.3.4. xml_view_publish</h4>
			<div>
      <pre class="programlisting">
DB.DBA.xml_view_publish (in view_name varchar, in dav_path varchar,
	in dav_owner varchar, in is_persistent integer, in refresh_interval integer)
</pre>
    </div>
			<p>
This presents an XML view as a DAV resource.
The view name is the name in the create xml view statement, note that this is case sensitive
and is never converted since it is a string, not an identifier.
The path must be absolute and is interpreted as relative from the DAV root collection.
The DAV user is the owner of the resource.
If is_persistent is non-zero the resource will be materialized from the view&#39;s description.
The refresh interval is only applicable if the resource is materialized. If so,
this is an interval in minutes.  A value of 0 means no automatic refresh.
</p>
			<p>
The reverse operation of xml_view_publish is deleting the DAV resource.  xml_view_publish
may be called several times to alter the owner or refresh interval.
</p>
		<br />
	<br />
  
	
		<a name="xmlviewextentref" />
    <h3>13.3.4. External Entity References in Stored XML</h3>
		<p>
When an XML document is stored as either text, long xml, xmltype or in the persistent XML format it can
contain references to external parsed entities with the &lt;!entity ...&gt; declaration and
the &amp;xx; syntax.  These are stored as references and not expanded at storage
time if the entity is external.
</p>
		<p>
Such references are transparently followed by XPATH and XSLT.  A run time
error occurs if the referenced resource cannot be accessed when needed.  The reference is only
followed if the actual subtree is selected by XPATH or XSLT.  The resource is retrieved at most once
for each XPATH or XSLT operation referencing it, regardless of the number of times the link is traversed.
This is transparent, so that the document node of the referenced entity appears as if it were in the place of the
reference.
</p>
		<p>
External entity references have an associated URI, which is either absolute with protocol identifier and full
path or relative.  Relative references must be resolved with respect to the base URI
of the referencing document.  If the document is stored as a column value in a table
it does not have a natural base URI, hence the application must supply one if relative references are to
be supported.  This is done by specifying an extra column of the same table to contain a path, in the
form of collections delimited by slashes, just as the path of a DAV resource or a Unix file system path.
</p>
		<p>
This base URI is associated with an XML column with the IDENTIFIED BY declaration:
</p>
		<div>
      <pre class="programlisting">
create table XML_TEXT (
	XT_ID integer,
	XT_FILE varchar,
	XT_TEXT long varchar identified by xt_file,
		primary key (XT_ID)
	);

create index XT_FILE on XML_TEXT (XT_FILE);
</pre>
    </div>
		<p>
Thus, each time the value of xt_text is retrieved for XML processing by XPATH_CONTAINS or XCONTAINS
the base URI is taken from xt_file.
</p>
		<p>
The complete URI for the xt_text of a column of the sample table will be:
</p>
		<div>
      <pre class="programlisting">
virt://&lt;qualified table name&gt;.&lt;uri column&gt;.&lt;text column&gt;:&lt;uri column value&gt;
</pre>
    </div>
		<p>
An example would be:
</p>
		<div>
      <pre class="programlisting">
&quot;virt://DB.DBA.XML_TEXT.XT_FILE.XT_TEXT:sqlreference.xml&quot;
</pre>
    </div>
		<p>
The .. and . in relative paths are treated as with file names when
combining relative references to base URI&#39;s. A relative reference without a path just
replaces the last part of the path in the base URI.
</p>
		<div class="tip">
			<div class="tiptitle">See</div>
			<p>
				<a href="fn_xml_uri_get.html">xml_uri_get and xml_uri_merge</a> for more details.
</p>
		</div>
	<br />
	
		<a name="xpproc" />
    <h3>13.3.5. Using XPATH in SQL Queries and Procedures</h3>
		<p>
An XPATH expression can appear as a SQL query expression,
that is, as a derived table or subquery predicate or scalar subquery.
This means that the XPATH expression is expanded compile time to the corresponding SQL.
The mapping of the XPATH hierarchy to tables and joins is given by the __view XPATH option, which is mandatory.
	</p>
		<p>
The XPATH keyword introduces an embedded XPATH expression.  The XPATH
text is presented as a string literal.  Note that the tokenization rules are different
for XPATH and SQL, so having XPATH as a string makes it clear which rules apply
to parsing which part of the composite query.
	</p>
		<a name="xp01" />
    <div class="example">
			<div class="exampletitle">Example</div>
			<div>
        <pre class="programlisting">
select * from (XPATH &#39;[__* __view &quot;cat&quot;]
	//product&#39;) P order by &quot;P.&quot;ProductName&quot;&quot;;
</pre>
      </div>
			<p>
will evaluate the //product query in the context of the cat XML view
and produce a result set consisting of all the attributes of the
product entity as defined in the view.
	</p>
		</div>
		<div class="note">
			<div class="notetitle">Note:</div>
			<p>
The __key and __* XPATH options are central here in
defining the result columns of the XPATH.  The default result column of an XPATH
expression is the serialization of the selected entity or scalar, which is most
of the time impractical in a SQL context.
	</p>
		</div>
		
			<a name="paramsinxp" />
    <h4>13.3.5.1. Parameters in XPATH</h4>
			<p>
The &#39;$&#39; sign introduces a parameter in XPATH.  The identifier following the dollar sign should reference a SQL column or variable defined in the surrounding context. The name of the parameter can contain a dot for referencing a column with a correlation name.
	</p>
			<p>
For instance, to make a VSP page that outputs the category tree which contains a specific product, one may write:
	</p>
			<div>
      <pre class="programlisting">
&lt;HTML&gt;
&lt;?vsp
      declare N varchar;
      N := {?&#39;name&#39;};
      for (XPATH &#39;[__http __view &#39;&#39;cat&#39;&#39;]
	    /category[product/@ProductName = $N]&#39; do ; ?&gt;
&lt;/HTML&gt;
</pre>
    </div>
			<p>
This will iterate over the categories containing a product with ProductName equal to
the URL parameter &#39;name&#39;. Note the __http option that causes the text of the selected entities to go directly to the HTTP client.
Note the double &#39;&#39; escape for the XML view name inside the SQL string literal forming the name.
	</p>
			<p>
Also note that the N parameter is in upper case to work in all case modes. In some
modes SQL identifiers will be converted automatically to upper case but this conversion does not apply inside XPATH.
	</p>
			<div>
      <pre class="programlisting">
select * from &quot;Demo&quot;..&quot;Categories&quot; C
	where exists (XPATH &#39;[__view &quot;ord&quot;]
	//products[@CategoryID = $C.CategoryID]&#39;);
</pre>
    </div>
			<p>
This example selects the categories of products that have been mentioned in the ord
XML view.
	</p>
		<br />
		<div class="note">
			<div class="notetitle">Syntax Notes</div>
			<p>
The main difference of SQL and XPATH is that the &#39;-&#39; is not a breaking
character in XPATH and that XPATH is case sensitive without any implicit
identifier case conversion.
</p>
		</div>
	<br />
	
	
		<a name="xquery_sql" />
    <h3>13.3.6. XQUERY and XML view</h3>
		<p>
Similarly to XPATH, XQUERY may also  be used as a query language for XML views.
Virtuoso offers a special case of FLWR expression for this purpose. It is possible
to use the  <a href="xpf_xmlview.html">xmlview()</a> function
in FOR clause expressions for querying XML views.
This function is similar to document () in the sense that it sets the
source of the path to be the logical root of the referenced XML view.
The XML view must be a constant known at compile time.  A SQL query
against the appropriate tables of the XML view is internally
constructed and evaluated at run time, producing XML fragments from
the selected rows.  At no point will non-selected parts of the
evaluation of the XML view be physically created.  The path expression
following from <a href="xpf_xmlview.html">xmlview()</a> may contain filters
involving XQuery variables bound in the scope of the path expression, thus allowing joining SQL data to
XQuery variable values.

</p>
		<p>
The XQUERY string
</p>
		<div>
      <pre class="programlisting">
for $cat in xmlview(&quot;cat&quot;)/category return {$cat}
</pre>
    </div>
		<p>
in the query
</p>
		<div>
      <pre class="programlisting">
select xquery_eval(&#39; {for $cat in xmlview(&quot;cat&quot;)/category return &lt;q&gt;{$cat}&lt;/q&gt;}&#39;,
                     xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
		<p>
is equivalent to the XPATH query string
</p>

		<div>
      <pre class="programlisting">
XPATH [__view &quot;cat&quot;]/category
</pre>
    </div>
<p>described above.</p>
<p> The expression xmlview(&quot;viewname&quot;)/path is not a valid top level SQL statement, but may be
used by xquery_eval() function.
The path statement is translated into SQL query in the context of the &quot;viewname&quot; (i.e. the
necessary table names are taken from &quot;viewname&quot; XML view), so that only the desired relational data will be queried.
Functionality of this kind of SQL queries is
similar to functionality of the SQL fetch statement, i.e. such a query provides iteration over the result set
of a cursor. The query is executed once for each row in the query expression&#39;s result set.
Thus using the CREATE XML VIEW statement and a XQUERY FOR clause expression with
<a href="xpf_xmlview.html">xmlview()</a> function  allows you to query the database
and to return the results in the form of an XML document and to avoid redundant data access.
This kind of queries also allows computing joins between two or more documents and restructuring data.
</p>
		<div class="note">
			<div class="notetitle">Note</div>
			<p>
The &lt;dummy_tag/&gt; tag is not used and it is necessary only as an arbitrary argument for xtree_doc()
functions.

</p>
		</div>

			<a name="vht03" />
    <div class="example">
				<div class="exampletitle">Example 1</div>
				<div>
        <pre class="programlisting">
create xml view &quot;product&quot; as
{
  &quot;Demo&quot;.&quot;demo&quot;.&quot;Products&quot; p as &quot;product&quot;
      (&quot;ProductID&quot;, &quot;ProductName&quot; as &quot;product_name&quot;,&quot;UnitPrice&quot; as &quot;price&quot;,
      &quot;SupplierID&quot;,&quot;CategoryID&quot;)
    {
      &quot;Demo&quot;.&quot;demo&quot;.&quot;Suppliers&quot; s as &quot;supplier&quot; (&quot;SupplierID&quot;,&quot;CompanyName&quot;)
	on (s.&quot;SupplierID&quot; = p.&quot;SupplierID&quot;)
       ,
      &quot;Demo&quot;.&quot;demo&quot;.&quot;Categories&quot; c as &quot;category&quot; (&quot;Description&quot;)
	on (c.&quot;CategoryID&quot; = p.&quot;CategoryID&quot;)

    }
}
</pre>
      </div>
</div>
<p>
This declares a two level hierarchy with a product node for each product
and a supplier child node and a category child node of the product.
</p>
<p>The following query will return the XML document in which each category node will contain all suppliers supplying products of the given category.
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $cat in xmlview(&quot;cat&quot;)/category
      return (
        &lt;category description={$cat/@description}&gt;
          {
            distinct (
              for $prod in xmlview(&quot;product&quot;)/product
              where $cat/@CategoryID=$prod/category/@CategoryID
              return $prod/supplier )
          }
        &lt;/category&gt; )
    }
  &lt;/document&gt;
  &#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;)
  );
</pre>
    </div>
<p>
returns the XML document in which each category node contains all suppliers supplying products of the given category.
</p>
			<a name="vht04" />
    <div class="example">
				<div class="exampletitle">Example 2</div>
<p>
Let a document named suppliers.xml contains supplier elements; each supplier element in turn contains
supplier_id and supplier_name subelements.
The following query
</p>
<div>
        <pre class="programlisting">
select xquery_eval(&#39;
  &lt;supplier_product&gt;
    {
      for $supp in document(&quot;suppliers.xml&quot;)/supplier
      return (
        &lt;supplier&gt;{$supp/supplier_name }
          &lt;product_name&gt;
            {
              for $prod in xmlview(&quot;product&quot;)/product
              where string($supp/supplier_id)=$prod/supplier/@SupplierID
              return string($prod/@product_name)
            }
          &lt;/product_name&gt;
        &lt;/supplier&gt;)
    }
  &lt;/supplier_product&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;)
  );
</pre>
      </div>
<p> returns the XML document that contains  supplier elements; each supplier element in turn contains
supplier_name and product_name elements.
</p>
</div>
<p> The previous query and the following one show that
it is possible to use variables in a XPATH expression following <a href="xpf_xmlview.html">xmlview()</a>
 functions.
</p>
			<a name="vht05" />
    <div class="example">
				<div class="exampletitle">Example 3</div>
<p>The query </p>
<div>
        <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      distinct(
        let $ex:= &quot;Ex%&quot;
        for $prod in xmlview(&quot;product&quot;)//supplier[@CompanyName like $ex]
        return &lt;supp_id&gt;{$prod/@SupplierID}&lt;/supp_id&gt;)
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
      </div>
<p> is equivalent to </p>
<div>
        <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      distinct(
        for $prod in xmlview(&quot;product&quot;)//supplier [@CompanyName like &quot;Ex%&quot;]
        return &lt;supp_id&gt;{$prod/@SupplierID}&lt;/supp_id&gt;)
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
      </div>
<p> and selects all suppliers having attribute &quot;CompanyName&quot; starting with &quot;Ex&quot;.</p>
</div>

		
			<a name="optimizationinxq" />
    <h4>13.3.6.1. Optimization in the queries with xmlview() function</h4>

<p> At least two methods may be used to accelerate the execution of queries with xmlview() function.
The first method assumes that a path statement following xmlview() function should contain maximum conditions
to reduce the result set. For example, the query
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;&lt;w&gt;
  {
    for $prod in xmlview(&quot;product&quot;)/product[@ProductID=&quot;1&quot;]
    return &lt;q&gt;{$prod}&lt;/q&gt;
  }&lt;/w&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>will be executed faster than </p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;&lt;w&gt;
  {
    for $prod in xmlview(&quot;product&quot;)/product where $prod[@ProductID=&quot;1&quot;]
    return &lt;q&gt;{$prod}&lt;/q&gt;
  }&lt;/w&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>due to the SQL query produced from the path expression &#39;product[@ProductID=&quot;1&quot;]&#39; reduces
the result set in comparison with the SQL query produced from the path expression &#39;product&#39; in the second
query.
</p>
<p>
If we execute a join of two (or more) XML views (or XML document and XML view), i.e. the query
consists of the nested loops, the second method proposes to carry out a piece of query of the nested loop
which is independent of the outer loop outside the outer loop and uses LET clause for it. For example, the
query
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;&lt;document&gt;
  {
    let $prod_set:=(for $prod in xmlview(&quot;product&quot;)/product return $prod)
    for $cat in xmlview(&quot;cat&quot;)/category
    return (&lt;category description={$cat/@description}$gt;
      {distinct(for $prod in $prod_set
                where $cat/@CategoryID=$prod/category/@CategoryID
                return $prod/supplier)}&lt;/category&gt;)}&lt;/document&gt;&#39;,
   xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p> is equivalent to the query in the example 1, but it is about 5 times faster than original one.
This method is especially useful for full joins. In this case we do not have  a full join and this
query may be optimized without the use of temporary result sets if
the &#39;where&#39; clause is replaced with proper filter:
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;&lt;document&gt;
  {
    for $cat in xmlview(&quot;cat&quot;)/category
    let $catID := $cat/@CategoryID
    return (&lt;category description={$cat/@description}$gt;
      {distinct(
        for $supp in xmlview(&quot;product&quot;)/product[category/@CategoryID=$catID]/supplier
        return $supp)
      }&lt;/category&gt;)
  }
  &lt;/document&lt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>
This variant requires no memory for storing $prod_set and it never fetches redundant
fields from &quot;Demo&quot;.&quot;demo&quot;.&quot;Products&quot; table but it heavily needs index for &quot;Demo&quot;.&quot;demo&quot;.&quot;Products&quot;
on &quot;CategoryID&quot; field. If such index is built the last variant is about 10 times faster than the
query in example 1. Similarly, the query in example 2 may be optimized as follows:
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;supplier_product&gt;
   {
     for $supp in document(&quot;suppliers.xml&quot;)/supplier
     let $supp_id:=string($supp/@supplier_id)
     return (
      &lt;supplier&gt;{$supp/supplier_name}
        &lt;product_name&gt;
          {
            for $prod in xmlview(&quot;product&quot;)/product[supplier/@SupplierID=$supp_id]
            return string($prod/@product_name)
          }
        &lt;/product_name&gt;
      &lt;/supplier&gt;)
   }
  &lt;/supplier_product&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;)
  );
</pre>
    </div>
<p> and it speeds up the operation by more than 15 times.
</p>
<br />

        		
			<a name="restictionsinxq" />
    <h4>13.3.6.2. Restrictions in XPATH expressions following the xmlview() function</h4>
<p>Virtuoso does not support certain kinds of XPATH expressions applied to the xmlview() function.
</p>
<p>
1. A path expression must not contain any functions, because it is impossible to translate most of the functions
to SQL queries.
</p>
<p>
2. A path expression must not contain numeric XQUERY variables in the arithmetic expressions.
Let a document named products.xml contains product elements; each product element has numeric
attribute ProductPrice. A run time error occurs if the following query would be used
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
   {
     for $prod_doc in document(&quot;products.xml&quot;)/products/product/@ProductPrice
       for $prod_view in xmlview(&quot;product&quot;)/product[@price&gt;$prod_doc+1]
     return &lt;q&gt;{$prod_doc, $prod_view/@product_name}&lt;/q&gt;
   }
   &lt;/document&gt;&#39;,
   xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p> because the type of $prod_doc is considered as string. As it is mentioned in the previous restriction
the using a function in a path expression (e.g. xmlview(&quot;product&quot;)/product[@price&gt;number($prod_doc)+1])
is not allowed.
The correct query is as follows:
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $prod_doc in document(&quot;products.xml&quot;)/products/product/@ProductPrice
        let $prod_doc2:=number($prod_doc)
        for $prod_view in xmlview(&quot;product&quot;)/product[@price&gt;$prod_doc2+1]
      return &lt;q&gt;{$prod_doc, $prod_view/@product_name}&lt;/q&gt;
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>
3. A path expression must not contain XQUERY variables with the following paths. The following query will not be
executed
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $cat in xmlview(&quot;cat&quot;)/category,
          $prod in xmlview(&quot;product&quot;)/product[@CategoryID=$cat/@CategoryID]
      return &lt;q&gt;{$prod/supplier}&lt;/q&gt;
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>
The correct query may be given as
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $cat in xmlview(&quot;cat&quot;)/category
      let $cat_id:=$cat/@CategoryID
      for $prod in xmlview(&quot;product&quot;)/product[@CategoryID=$cat_id]
      return &lt;q&gt;{$prod/supplier}&lt;/q&gt;
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>
4. Virtuoso does not support a selection of n-th element in a path expression. The following query will not be executed
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $cat in xmlview(&quot;cat&quot;)//product[1] return &lt;q&gt;{$cat}&lt;/q&gt;
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>

<p>
5. Virtuoso does not support a dereference (=&gt;) in a path expression.
</p>

<p>
6. It is not recommended to use the long varchar, long varbinary and long nvarchar data types with the logical and
boolean operations in a filter of the path expression.
For example, the execution of the following query
</p>
<div>
      <pre class="programlisting">
select xquery_eval(&#39;
  &lt;document&gt;
    {
      for $prod in
        xmlview(&quot;product&quot;)/product[@SupplierID&lt;5]/category[@Description like &quot;Sw%&quot;]
      return &lt;q&gt;{$prod}&lt;/q&gt;
    }
  &lt;/document&gt;&#39;,
  xtree_doc(&#39;&lt;dummy_tag/&gt;&#39;));
</pre>
    </div>
<p>
may return an error, because the field &quot;Description&quot; has LONG VARCHAR type in the table &quot;demo&quot;.&quot;Categories&quot;.
</p>

<br />

<br />

	
		<a name="mapping_schemas" />
    <h3>13.3.7. Mapping Schemas as XML Views</h3>
		<p>
Virtuoso supports creating XML views by using annotated XSD
schemas referred to as mapping schemas.
A file containing a mapping schema may be loaded by  calling the
<a href="fn_xml_load_mapping_schema_decl.html">xml_load_mapping_schema_decl()</a>
function. A name (without extension .xsd) of the file containing a mapping schema
is considered to be the name of the xml view, defined by the given mapping schema.</p>

<p>A loaded mapping schema may be queried in the same way as one would
query XML views defined using the CREATE XML VIEW statement with XPATH:</p>

    <div>
      <pre class="programlisting">
XPATH [__view &quot;xml_view_name&quot;]/xpath_query
</pre>
    </div>

<a name="ex_loadingmapxmlsch" />
    <div class="example">
      <div class="exampletitle">Loading and Querying a Mapping Schema</div>

<p> The XML view &quot;Catmp&quot; from the file &quot;Catmp.xsd&quot; may be loaded
using the following statement:</p>
			<div>
        <pre class="programlisting">
   select  xml_load_mapping_schema_decl (
      &#39;http://localhost.localdomain/xmlrepository/&#39;,
      &#39;Catmp.xsd&#39;,
      &#39;UTF-8&#39;,
      &#39;x-any&#39; ) ) );
</pre>
      </div>

      <p>where the contents of &quot;Catmp.xsd&quot; is</p>

      <div>
        <pre class="programlisting">
&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
            xmlns:sql=&quot;urn:schemas-microsoft-com:mapping-schema&quot;&gt;
&lt;xsd:annotation&gt;
  &lt;xsd:appinfo&gt;
    &lt;sql:relationship name=&quot;CategoryProduct&quot;
          parent=&quot;Demo.demo.Categories&quot;
          parent-key=&quot;CategoryID&quot;
          child=&quot;Demo.demo.Products&quot;
          child-key=&quot;CategoryID&quot; /&gt;
  &lt;/xsd:appinfo&gt;
&lt;/xsd:annotation&gt;

  &lt;xsd:element name=&quot;category&quot; sql:relation=&quot;Demo.demo.Categories&quot; type=&quot;CategoryType&quot; /&gt;
   &lt;xsd:complexType name=&quot;CategoryType&quot; &gt;
     &lt;xsd:sequence&gt;
        &lt;xsd:element name=&quot;product&quot;
                     sql:relation=&quot;Demo.demo.Products&quot;
                     sql:relationship=&quot;CategoryProduct&quot; &gt;
           &lt;xsd:complexType&gt;
              &lt;xsd:attribute name=&quot;ProductName&quot; type=&quot;xsd:string&quot; /&gt;
           &lt;/xsd:complexType&gt;
        &lt;/xsd:element&gt;
     &lt;/xsd:sequence&gt;
        &lt;xsd:attribute name=&quot;CategoryID&quot;  type=&quot;xsd:integer&quot; /&gt;
        &lt;xsd:attribute name=&quot;description&quot;  sql:field=&quot;Description&quot;  type=&quot;xsd:string&quot; /&gt;
    &lt;/xsd:complexType&gt;
&lt;/xsd:schema&gt;
			</pre>
      </div>

<p>The XML view &quot;Catmp&quot; loaded from the file &quot;Catmp.xsd&quot; is similar to XML view &quot;cat&quot;
defined by CREATE XML VIEW in the section <a href="xmlservices.html#explicitxmlviews">Explicit Xml Views</a>.
</p>

<p>The query</p>
			<div>
        <pre class="programlisting">
XPATH [__view &#39;Catmp&#39;] /category[@* = 1];
			</pre>
      </div>
<p>will now return the following result:</p>

      <div>
        <pre class="programlisting">
&lt;category CategoryID=&quot;1&quot; description=&quot;Soft drinks, coffees, teas, beers, and ales&quot; &gt;
  &lt;product ProductName=&quot;Chai&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Chang&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Guarana Fantastica&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Sasquatch Ale&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Steeleye Stout&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Cote de Blaye&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Chartreuse verte&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Ipoh Coffee&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Laughing Lumberjack Lager&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Outback Lager&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Rhonbrau Klosterbier&quot; &gt;&lt;/product&gt;
  &lt;product ProductName=&quot;Lakkalikoori&quot; &gt;&lt;/product&gt;
&lt;/category&gt;
</pre>
      </div>
</div>

<p>
Mapping schemas provide more flexibility than XML views defined by
the <span class="computeroutput">CREATE XML VIEW</span> statement.
In the following mapping schema a constant element, &quot;CustomerOrders&quot;, an
element that does not map to any database table or column but may appear in the
resulting XML as a container element of other subelements, is specified
by the <span class="computeroutput">sql:is-constant</span> annotation.</p>

		<div>
      <pre class="programlisting">
&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
            xmlns:sql=&quot;urn:schemas-microsoft-com:mapping-schema&quot;&gt;
&lt;xsd:annotation&gt;
  &lt;xsd:appinfo&gt;
    &lt;sql:relationship name=&quot;CustOrders&quot;
        parent=&quot;Demo.demo.Customers&quot;
        parent-key=&quot;CustomerID&quot;
        child=&quot;Demo.demo.Orders&quot;
        child-key=&quot;CustomerID&quot; /&gt;
  &lt;/xsd:appinfo&gt;
&lt;/xsd:annotation&gt;

  &lt;xsd:element name=&quot;Customer&quot; sql:relation=&quot;Demo.demo.Customers&quot; &gt;
    &lt;xsd:complexType&gt;
      &lt;xsd:sequence&gt;
        &lt;xsd:element name=&quot;CustomerOrders&quot; sql:is-constant=&quot;1&quot; &gt;
          &lt;xsd:complexType&gt;
            &lt;xsd:sequence&gt;
              &lt;xsd:element name=&quot;Order&quot; sql:relation=&quot;Demo.demo.Orders&quot;
                           sql:relationship=&quot;CustOrders&quot;
                           maxOccurs=&quot;unbounded&quot; &gt;
                &lt;xsd:complexType&gt;
                   &lt;xsd:attribute name=&quot;OrderID&quot; type=&quot;xsd:integer&quot; /&gt;
                   &lt;xsd:attribute name=&quot;OrderDate&quot; type=&quot;xsd:date&quot; /&gt;
                   &lt;xsd:attribute name=&quot;CustomerID&quot; type=&quot;xsd:string&quot; /&gt;
                &lt;/xsd:complexType&gt;
              &lt;/xsd:element&gt;
            &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
        &lt;/xsd:element&gt;
      &lt;/xsd:sequence&gt;
          &lt;xsd:attribute name=&quot;CustomerID&quot; type=&quot;xsd:string&quot; /&gt;
    &lt;/xsd:complexType&gt;
  &lt;/xsd:element&gt;
&lt;/xsd:schema&gt;
</pre>
    </div>

<p>After loading the &quot;Cust_constant.xsd&quot; file containing the given
mapping schema, the xpath query:</p>

      <div>
      <pre class="programlisting">
XPATH [__view &#39;Cust_constant&#39;] /category[@* = 1];
			</pre>
    </div>

<p>will return the following result:</p>

      <div>
      <pre class="programlisting">
&lt;Customer CustomerID=&quot;ALFKI&quot; &gt;
  &lt;CustomerOrders &gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1995-09-25 00:00:00.000000&quot; OrderID=&quot;10643&quot; &gt;&lt;/Order&gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1995-11-03 00:00:00.000000&quot; OrderID=&quot;10692&quot; &gt;&lt;/Order&gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1995-11-13 00:00:00.000000&quot; OrderID=&quot;10702&quot; &gt;&lt;/Order&gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1996-02-15 00:00:00.000000&quot; OrderID=&quot;10835&quot; &gt;&lt;/Order&gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1996-04-15 00:00:00.000000&quot; OrderID=&quot;10952&quot; &gt;&lt;/Order&gt;
    &lt;Order CustomerID=&quot;ALFKI&quot; OrderDate=&quot;1996-05-09 00:00:00.000000&quot; OrderID=&quot;11011&quot; &gt;&lt;/Order&gt;
  &lt;/CustomerOrders&gt;
&lt;/Customer&gt;
    . . .
</pre>
    </div>

<p>
Virtuoso does not support all mapping schema annotations at this time.
The following  are  currently unsupported:</p>

<ul>
    <li>
        <span class="computeroutput">sql:encode</span>
    </li>
    <li>
        <span class="computeroutput">sql:use-cdata</span>
    </li>
    <li>
        <span class="computeroutput">sql:overflow-field</span>
    </li>
    <li>
        <span class="computeroutput">sql:inverse</span>
    </li>
    <li>
        <span class="computeroutput">sql:hide</span>
    </li>
    <li>
        <span class="computeroutput">sql:guid</span>
    </li>
    <li>
        <span class="computeroutput">sql:max-depth</span>
    </li>
    </ul>

<p>Also, there are some restrictions to the structure   of mapping schemas: </p>

<ul>
      <li>Attributes can not contain sql:relationship annotation.</li>
      <li>Subelement having no sql:is-constant annotation and not mapped
  to any table can not contain subelements and attributes.</li>
      <li>Recursive relationships is not supported.</li>
    </ul>

    <div class="note">
			<div class="notetitle">Note:</div>
			<p>
The XML views, defined by mapping schemas may not be queried
using XQUERY.   They can however be referenced with the xmlview XPATH functions in path expressions inside an XQuery query.</p>
</div>

<div class="tip">
      <div class="tiptitle">See Also:</div>
<p>SQLXML 3.0 documentation:
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqlxml3/htm/ssxsdannotations_0gqb.asp">
Creating XML Views by Using Annotated XSD Schemas. </a>
</p>
    </div>

<br />

	
		<a name="view4xmldifferences" />
    <h3>13.3.8. Differences Between SQLX, FOR XML and XML Views</h3>
		<p>
A SQLX or FOR XML query has no effect on the database  schema. It is a
transient event and does not generate procedures or other schema elements.
</p>
		<p>
These define an ad hoc mapping of a result set to XML.
There is no possibility of using XPATH to specify a search without first
constructing the whole tree. An XML view on the other hand
provides a mapping context in which one can make XQUERY or XPATH queries that are
mapped into SQL and the XML is only generated after applying the conditions.
</p>
		<p>
XML views will usually be more efficient in complex cases and the notation there
may be simpler than the EXPLICIT notation in FOR XML.
For simple cases SQLX or FOR XML is the  more convenient of the two.
SQLX or FOR XML does not restrict the SQL being used and will allow free use of
subqueries, expressions, derived tables, qualified joins etc.
</p>
	<br />

<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="composingxmlinsql.html" title="XML Composing Functions in SQL Statements (SQLX)">Previous</a>
          <br />XML Composing Functions in SQL Statements (SQLX)</td>
     <td align="center" width="34%">
          <a href="webandxml.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="queryingxmldata.html" title="Querying Stored XML Data">Next</a>
          <br />Querying Stored XML Data</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>