15.3.7.Mapping Schemas as XML Views

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 xml_load_mapping_schema_decl() 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.

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:

XPATH [__view "xml_view_name"]/xpath_query

Example15.11.Loading and Querying a Mapping Schema

The XML view "Catmp" from the file "Catmp.xsd" may be loaded using the following statement:

   select  xml_load_mapping_schema_decl (
      'x-any' ) ) );

where the contents of "Catmp.xsd" is

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    <sql:relationship name="CategoryProduct"
          child-key="CategoryID" />

  <xsd:element name="category" sql:relation="Demo.demo.Categories" type="CategoryType" />
   <xsd:complexType name="CategoryType" >
        <xsd:element name="product"
                     sql:relationship="CategoryProduct" >
              <xsd:attribute name="ProductName" type="xsd:string" />
        <xsd:attribute name="CategoryID"  type="xsd:integer" />
        <xsd:attribute name="description"  sql:field="Description"  type="xsd:string" />

The XML view "Catmp" loaded from the file "Catmp.xsd" is similar to XML view "cat" defined by CREATE XML VIEW in the section Explicit Xml Views .

The query

XPATH [__view 'Catmp'] /category[@* = 1];

will now return the following result:

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

Mapping schemas provide more flexibility than XML views defined by the CREATE XML VIEW statement. In the following mapping schema a constant element, "CustomerOrders", 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 sql:is-constant annotation.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    <sql:relationship name="CustOrders"
        child-key="CustomerID" />

  <xsd:element name="Customer" sql:relation="Demo.demo.Customers" >
        <xsd:element name="CustomerOrders" sql:is-constant="1" >
              <xsd:element name="Order" sql:relation="Demo.demo.Orders"
                           maxOccurs="unbounded" >
                   <xsd:attribute name="OrderID" type="xsd:integer" />
                   <xsd:attribute name="OrderDate" type="xsd:date" />
                   <xsd:attribute name="CustomerID" type="xsd:string" />
          <xsd:attribute name="CustomerID" type="xsd:string" />

After loading the "Cust_constant.xsd" file containing the given mapping schema, the xpath query:

XPATH [__view 'Cust_constant'] /category[@* = 1];

will return the following result:

<Customer CustomerID="ALFKI" >
  <CustomerOrders >
    <Order CustomerID="ALFKI" OrderDate="1995-09-25 00:00:00.000000" OrderID="10643" ></Order>
    <Order CustomerID="ALFKI" OrderDate="1995-11-03 00:00:00.000000" OrderID="10692" ></Order>
    <Order CustomerID="ALFKI" OrderDate="1995-11-13 00:00:00.000000" OrderID="10702" ></Order>
    <Order CustomerID="ALFKI" OrderDate="1996-02-15 00:00:00.000000" OrderID="10835" ></Order>
    <Order CustomerID="ALFKI" OrderDate="1996-04-15 00:00:00.000000" OrderID="10952" ></Order>
    <Order CustomerID="ALFKI" OrderDate="1996-05-09 00:00:00.000000" OrderID="11011" ></Order>
    . . .

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


Also, there are some restrictions to the structure of mapping schemas:

Attributes can not contain sql:relationship annotation.
Subelement having no sql:is-constant annotation and not mapped to any table can not contain subelements and attributes.
Recursive relationships is not supported.
[Note] Note:

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.

[Tip] See Also:

SQLXML 3.0 documentation: Creating XML Views by Using Annotated XSD Schemas.