Excluding Schema Elements from the Resulting XML Document Using sql:map-field

XML and Internet Support

XML and Internet Support

Excluding Schema Elements from the Resulting XML Document Using sql:map-field

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 and does not appear in the XML. This is done by specifying the sql:map-field annotation.

The sql:map-field annotation differs from sql:is-constant in that the unmapped elements and attributes do not appear in the XML document. sql:map-field is especially useful if the schema cannot be modified or is used to validate XML from other sources yet contains data that is not stored in your database.

sql:map-field takes a Boolean value (0 = FALSE, 1 = TRUE). The sql:map-field annotation is valid only on an <attribute>, <element> or <ElementTypes> with text-only content (content=textOnly). The annotation is not valid on an <element> or <ElementTypes> that maps to tables.

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 sql:map-field annotation

Assume you have an XDR schema from some other source. This XDR schema consists of <Employees> element with EmployeeID, FirstName, LastName, and HomeAddress attributes.

In mapping this XDR schema to the Employees table in the database, sql:map-field is specified on the HomeAddress attribute because the Employees table does not store home addresses of employees. As a result, this attribute is not returned in the resulting XML document when an XPath query is specified againt the mapping schema.

Default mapping takes place for the rest of the schema. The <Employees> element maps to the Employees table, and all the attributes map to the columns with the same name in the Employees table. For more information about default mapping, see Default Mapping of XDR Elements and Attributes to Tables and Columns.

<?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="EmployeeID" />
    <AttributeType name="FirstName" />
    <AttributeType name="LastName" />
    <AttributeType name="HomeAddress" />

    <attribute type="EmployeeID" />
    <attribute type="FirstName" />
    <attribute type="LastName" />
    <attribute type="HomeAddress" sql:map-field="0" />
</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 (MySchema.xml) and save it in the directory associated with the template virtual name. The query in the template selects all Employees with EmployeeID equal to 1.
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    <sql:xpath-query mapping-schema="MySchema.xml">
      /Employees[@EmployeeID=1]
    </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema is relative to the directory associated with the template virtual name. An absolute path can also be specified, for example:

    mapping-schema="C:\MyDir\MySchema.xml"
    
  3. This URL executes the template:
    http://IISServer/VirtualRoot/template/MySchemaT.xml
    
  4. Here is the partial result set:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> 
        <Employees EmployeeID="1" FirstName="Nancy" LastName = "Davolio"/> 
    </ROOT>
    

    Note that the EmployeeID, FirstName, and LastName are present, but HomeAddress is not because the mapping schema specified 0 for the sql:map-field attribute.

See Also

IIS Virtual Directory Management for SQL Server

Using XPath Queries

Accessing SQL Server Using HTTP

Executing Template Files Using HTTP