Creating Constant Elements Using sql:is-constant

XML and Internet Support

XML and Internet Support

Creating Constant Elements Using sql:is-constant

Because of the default mapping, every element and attribute in the XDR schema maps to a database table and column. At times, you may want create an element in the XDR schema that does not map to any database table or column but still appears in the XML document. These are called constant elements. To create a constant element, specify the sql:is-constant annotation. sql:is-constant takes a Boolean value (0 = FALSE, 1 = TRUE).

This annotation is specified on <ElementType>, which does not map to any database table, thereby making it a constant element. The sql:is-constant annotation can be used for:

  • Adding a top-level element to the XML document. XML requires a single top-level element (<root> element) for the document.

  • Creating container elements, for example, an <Orders> element that wraps all Orders.
Examples

To create working samples using these examples, you must create the nwind virtual directory (to access the Northwind database) and a virtual name of template type. For more information about creating the nwind virtual directory, see Creating the nwind Virtual Directory.

In creating working samples in each example, templates are used to specify XPath queries against the mapping XDR schema. There are different ways of using annotated XDR schemas in queries, for example, inline schemas and schemas in the URL. For more information, see Using Annotated XDR Schemas in Queries.

A. Specify sql:is-constant to add a container element

In this annotated XDR schema, <OrderList> is defined as a constant element containing all the <Orders> subelements. The sql:is-constant annotation is specified on the OrderList <ElementType>, making it a constant, and therefore not mapping to any database table. Although <OrderList> element does not map to any database table/column, it still appears in the resulting XML as a container element containing <Orders> subelements.

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<ElementType name="Orders" >
   <AttributeType name="OrderID" />
   <attribute type="OrderID" />
</ElementType>
<ElementType name="OrderList" sql:is-constant="1">
   <element type="Orders">
      <sql:relationship 
                   key-relation="Customers" 
                   foreign-relation="Orders" 
                   key="CustomerID" 
                   foreign-key="CustomerID" />
   </element>
</ElementType>
<ElementType name="Customers" >
   <AttributeType name="CustomerID" />
   <attribute type="CustomerID" />
   <element type="OrderList" />
</ElementType>
</Schema>

Testing a sample XPath query against the schema

  1. Save the schema (MySchema.xml) in the directory associated with the template virtual name that you have already created (or one of its subdirectories, in which case you must specify the relative path in the mapping-schema attribute).

  2. Create this template (MySchemaT.xml) and save it in the directory associated with the template virtual name. The XPath query in the template selects all <Employees> elements with EmployeeID attribute value 1.
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql" >
    <sql:xpath-query mapping-schema="TestSchema1.xml" >
    /Customers
    </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema is relative to the directory associated with the template virtual name. An absolute path can also be specified, for example:

    mapping-schema="C:\MyDir\MySchema.xml"
    
  3. This URL executes the template:
    http://IISServer/VirtualRoot/template/MySchemaT.xml
    
  4. Here is the partial result set:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> 
      <Customers CustomerID="ALFKI"> 
        <OrderList> 
          <Orders OrderID="10643" /> 
          <Orders OrderID="10692" /> 
          <Orders OrderID="10702" /> 
          <Orders OrderID="10835" /> 
          <Orders OrderID="10952" /> 
          <Orders OrderID="11011" /> 
        </OrderList> 
    </Customers>
    ...
    </ROOT>
    

See Also

IIS Virtual Directory Management for SQL Server

Using XPath Queries

Accessing SQL Server Using HTTP

Executing Template Files Using HTTP