Specifying Default Values for Attributes in the XDR Schema (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).

In a database columns can be assigned default values. Similarly, in an XDR schema, default values can be set for attributes (elements cannot be assigned default values in the XDR schema). The XDR schema allows the default attribute specification on <AttributeType>.

If a column value associated with an attribute is NULL, that attribute is not returned for that instance of the element. But if the default attribute is specified on the <AttributeType>, then the attribute is returned with the default value specified.

For example, in extracting data from the database into an XML document, if one of the attribute values is missing, a default value of that attribute in the XDR schema is used.

Note

The default values may not appear in the document that is returned, rather this value is used by the validating parser whenever the attribute is not present.

Note

The default value is used if the parser used is schema-aware. That is, for the MSXML parser, you must ensure that the resolveExternals flag is set to TRUE (the default), and the parser then fetches the schemas. Once parsed, the individual instances have the attributes (for which the default is specified), regardless of whether the attribute was included in the XML document. The DOM supplies the default value.

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 the default value for an attribute in the XDR schema

In this example, the Title attribute is given a default value of "XYZ". When contact records are retrieved, a default value is assigned for contacts that do not have a title.

<?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="Person.Contact" >
    <AttributeType name="CID" />
    <AttributeType name="FirstName" />
    <AttributeType name="LastName" />
    <AttributeType name="Title" default="XYZ"/>

    <attribute type="CID" sql:field="ContactID" />
    <attribute type="FirstName" />
    <attribute type="LastName" />
    <attribute type="Title"  />
</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 DefaultValueXdr.xml.

  2. Copy the following template and paste it into a text file. Save the file as DefaultValueXdrT.xml in the same directory where you saved DefaultValueXdr.xml.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="DefaultValueXdr.xml">
        /Person.Contact[@CID &gt; 1000 and @CID &lt; 1006]
      </sql:xpath-query>
    </ROOT>
    

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

    mapping-schema="C:\MyDir\DefaultValueXdr.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">
  <Person.Contact CID="1001" FirstName="Terri" LastName="Duffy" /> 
  <Person.Contact CID="1002" FirstName="Roberto" LastName="Tamburello" /> 
  <Person.Contact CID="1003" FirstName="Michael" LastName="Sullivan" /> 
  <Person.Contact CID="1004" FirstName="Sharon" LastName="Salavaria" /> 
  <Person.Contact CID="1005" FirstName="Gail" LastName="Erickson" Title="Ms." /> 
</ROOT>

See Also

Reference

Using XPath Queries in SQLXML 4.0

Help and Information

Getting SQL Server 2005 Assistance