Default Values for Schema Attributes


In XDR, schema elements cannot be assigned a default value. It is possible, however, to assign default values to attributes. Default values are allowed on the <AttributeType> entity. If a table column that is referenced by an attribute in a schema contains a null value, nothing will be returned in a query that canvasses this column. On the other hand, if a default attribute is specified, then that default value will be returned. An important point to remember here is that the returned value might still not appear in the document. The parser uses it to validate the document.

If you are using the MSXML Parser, you must ensure that the resolveExternals flag is set to TRUE (the default value). The Document Object Model (DOM) will now supply the default value. A discussion of this property is located at http://msdn.microsoft.com/library/psdk/xmlsdk/xmld1vg3.htm.

Listing 5.58 gives an example utilizing the Orders table that supplies the "Not Known" default value for any returned record that has a null value in the ShipRegion column. The accompanying template file is given in Listing 5.59, and the results are shown in Listing 5.60.

Listing 5.58 Schema Demonstrating the Default Attribute
 <?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="Name" />      <AttributeType name="Address" />      <AttributeType name="City" />      <AttributeType name="Region" default="Not Known" />      <attribute type="Name" sql:field="ShipName" />      <attribute type="Address" sql:field="ShipAddress" />      <attribute type="City" sql:field="ShipCity" />      <attribute type="Region" sql:field="ShipRegion" />    </ElementType>  </Schema> 
Listing 5.59 The Template to Test the Schema
 <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">    <sql:xpath-query mapping-schema="../schemas/DefaultSchema.xml">      /Orders    </sql:xpath-query>  </ROOT> 
Listing 5.60 Partial Results of Testing the Default Attribute
 <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">    <Orders Name="Vins et alcools Chevalier" Address="59 rue de l'Abbaye"     City="Reims" />    <Orders Name="Toms Spezialitten" Address="Luisenstr. 48"     City="Mnster" />    <Orders Name="Hanari Carnes" Address="Rua do Pao, 67" City="Rio de     Janeiro" Region="RJ" />  ...  </ROOT> 

Notice that, just as was previously mentioned, even though you specify a default value, it might not appear in the document. The parser to validate the document uses the value.

Microsoft has released a visual drag-and-drop schema-generation utility called the SQL SERVER XML View Mapper. This is a very easy tool to use and is freely downloadable from the Microsoft Web site at www.microsoft.com. Search on "view mapper" in the search utility. Appendix G of this book provides its basic operation and an explanation of the time this utility can save you.



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net