Specifying a Target Namespace Using sql:target-namespace

XML and Internet Support

XML and Internet Support

Specifying a Target Namespace Using sql:target-namespace

The sql:target-namespace annotation can be used to place elements and attributes from the default namespace into a different namespace. The sql:target-namespace attribute can be added only to the <Schema> tag in the XDR schema.

The value of sql:target-namespace is the namespace URI (Uniform Resource Identifier) to be used for generating elements and attributes specified in the mapping schema. This URI is applied to all elements and attributes in the default namespace. The XML document returned from queries against this schema contain xmlns:prefix="uri" declarations and prefix the element and attribute names accordingly. The URI that is used comes from the value of the sql:target-namespace annotation. However, the prefix is generated arbitrarily and does not correspond to any values in the schema (even if the prefixes are used in the schema).

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 a target namespace

In this example, sql:target-namespace annotation is used to specify the target namespace. As a result, all the elements and attributes that would have gone to the default namespace are redirected to the target namespace (MyNamespace).

<?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"
        sql:target-namespace="urn:MyNamespace">
<ElementType name="Orders" >
   <AttributeType name="OrderID" />
   <attribute type="OrderID"/>
</ElementType>
<ElementType name="Customers" >
   <AttributeType name="CustomerID" />
   <attribute type="CustomerID" />
   <AttributeType name="Contact" />
   <attribute type="Contact" sql:field="ContactName" />
   <element type="Orders" >
      <sql:relationship 
            key="CustomerID" 
            foreign-key="CustomerID" 
            key-relation="Customers" 
            foreign-relation="Orders" />
   </element>
</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:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="MySchema.xml" 
                       xmlns:x="urn:MyNamespace" >
        x:Customers
      </sql:xpath-query>
    </ROOT>
    

    The XPath query in the template requests all the <Customer> elements defined in the namespace MyNamespace. In the template a prefix, x, is bound to the namespace.

    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/TargetNST.xml
    
  4. Here is the result:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> 
      <y0:Customers xmlns:y0="urn:MyNamespace" CustomerID="ALFKI" 
                                               Contact="Maria Anders"> 
        <y0:Orders OrderID="10643" /> 
        <y0:Orders OrderID="10692" /> 
        <y0:Orders OrderID="10702" /> 
        <y0:Orders OrderID="10835" /> 
        <y0:Orders OrderID="10952" /> 
        <y0:Orders OrderID="11011" /> 
      </y0:Customers> 
    </ROOT>
    

Note that the prefixes generated are arbitrary, but map to the same namespace.

See Also

IIS Virtual Directory Management for SQL Server

Using XPath Queries

Accessing SQL Server Using HTTP

Executing Template Files Using HTTP