Share via


Creating Constant Elements Using sql:is-constant (XDR Schema)

Important

This topic is included as a reference for legacy applications. No future development work will be done on this feature. Avoid using this feature in new development work. Instead, use annotated XSD schemas to create XML views. For more information, see Introduction to Annotated XSD Schemas (SQLXML 4.0). You can convert existing annotated XDR schemas to XSD schemas. For more information, see Converting Annotated XDR Schemas to Equivalent XSD Schemas (SQLXML 4.0).

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 the following examples, you must meet certain requirements. For more information, see Requirements for Running SQLXML Examples.

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 the <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="Sales.SalesOrderHeader" >
   <AttributeType name="SalesOrderID" />
   <attribute type="SalesOrderID" />
</ElementType>
<ElementType name="OrderList" sql:is-constant="1">
   <element type="Orders">
      <sql:relationship 
                   key-relation="Sales.Customer" 
                   foreign-relation="Sales.SalesOrderHeader" 
                   key="CustomerID" 
                   foreign-key="CustomerID" />
   </element>
</ElementType>
<ElementType name="Sales.Customer" >
   <AttributeType name="CustomerID" />
   <attribute type="CustomerID" />
   <element type="OrderList" />
</ElementType>
</Schema>

To test a sample XPath query against the schema

  1. Copy the schema code above and paste it into a text file. Save the file as isConstant-xdr.xml.

  2. Copy the following template and paste it into a text file. Save the file as isConstant-xdrT.xml in the same directory where you saved isConstant-xdr.xml. The XPath query in the template selects all <Sales.Customer> elements with a CustomerID attribute value of 1.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql" >
    <sql:xpath-query mapping-schema="isConstant-xdr.xml" >
       /Sales.Customer[@CustomerID=1]
    </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (isConstant-xdr.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\isConstant-xdr.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

Here is the result set:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Sales.Customer CustomerID="1">
    <OrderList>
      <Sales.SalesOrderHeader SalesOrderID="43860" /> 
      <Sales.SalesOrderHeader SalesOrderID="44501" /> 
      <Sales.SalesOrderHeader SalesOrderID="45283" /> 
      <Sales.SalesOrderHeader SalesOrderID="46042" /> 
    </OrderList>
  </Sales.Customer>
</ROOT>

See Also

Reference

Using XPath Queries in SQLXML 4.0

Help and Information

Getting SQL Server 2005 Assistance