XML  •  Create an XML View

Listing 5. The mapping schema ties together the XML schema in Listing 3 and the database schema in Listing 4. It has special elements that allow you to define mappings between database columns and XML elements or attributes. The mappings defined in this XML View are passed to the XQueryProcessor, which the XmlAdapter uses to fill an XPathDocument2 object with data (see Listing 6).

<?xml version="1.0" encoding="utf-8" ?> 
<m:MappingSchema 
   xmlns:m="http://schemas.microsoft.com/data/
      2002/09/28/mapping">
   <m:DataSources>
      <m:DataSource Name="XML" Direction="Target" 
         Type="Xml">
         <m:Schema Location=
            "CustomersOrders.xsd" />
      </m:DataSource>
      <m:DataSource Name="SQL-Northwind" 
         Direction="Source" Type="SQL Server">
      <m:Schema Location=
         "CustomersOrdersDB.xml" />
      <m:Variable Name="Orders_B" Select=
         "Orders" />
      <m:Variable Name=
            "Customers_A" Select="Customers"/>
      <m:Relationship Name="CustomersOrders" 
         FromVariable="Customers_A" 
            ToVariable="Orders_B">
         <m:FieldJoin From="CustomerID" 
            To="CustomerID" />
         </m:Relationship>
      </m:DataSource>
   </m:DataSources>
   <m:Mappings>
      <m:Map SourceVariable="Customers_A" 
      TargetSelect="/Customer">
         <m:FieldMap 
            SourceField="CustomerID" 
               TargetField="@id" />
         <m:FieldMap 
            SourceField="ContactName" 
               TargetField="@name" />
      </m:Map>
      <m:Map SourceVariable="Orders_B" 
         TargetSelect="/Customer/Order">
         <m:FieldMap 
            SourceField="OrderID" 
               TargetField="@orderID" />
         <m:FieldMap 
            SourceField="OrderDate" 
               TargetField="@date" />
         <m:FieldMap 
            SourceField="CustomerID" 
               TargetField="@id" />
      </m:Map>
   </m:Mappings>
</m:MappingSchema>