Specifying Default Values for Attributes in the XDR Schema

XML and Internet Support

XML and Internet Support

Specifying Default Values for Attributes in the XDR Schema

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.

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

In this example, attribute Title is given a default value of XYZ. When employee records are retrieved, a default value is assigned for the employees who 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="Employees" >
    <AttributeType name="EID" sql:field="EmployeeID"/>
    <AttributeType name="FirstName" />
    <AttributeType name="LastName" />
    <AttributeType name="Title" default="XYZ"/>

    <attribute type="EID" sql:field="EmployeeID" />
    <attribute type="FirstName" />
    <attribute type="LastName" />
    <attribute type="Title"  />
</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 query in the template selects a customer with the CustomerID of ALFKI.
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="MySchema.xml">
        /Employees
      </sql:xpath-query>
    </ROOT>
    
  3. This URL executes the template:
    http://IISServer/VirtualRoot/template/MySchemaT.xml
    

See Also

IIS Virtual Directory Management for SQL Server

Using XPath Queries

Accessing SQL Server Using HTTP

Executing Template Files Using HTTP