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>
See Also
IIS Virtual Directory Management for SQL Server