Jaa


Specifying a Target Namespace Using sql:target-namespace (XDR Schema)

Important

This topic is included as a reference for legacy applications. No future development work will be done on this feature. Avoid using this feature in new development work. Instead, use annotated XSD schemas to create XML views. For more information, see Introduction to Annotated XSD Schemas (SQLXML 4.0). You can convert existing annotated XDR schemas to XSD schemas. For more information, see Converting Annotated XDR Schemas to Equivalent XSD Schemas (SQLXML 4.0).

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 the following examples, you must meet certain requirements. For more information, see Requirements for Running SQLXML Examples.

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="Sales.SalesOrderHeader" >
   <AttributeType name="SalesOrderID" />
   <attribute type="SalesOrderID"/>
</ElementType>
<ElementType name="Sales.Customer" >
   <AttributeType name="CustomerID" />
   <attribute type="CustomerID" />
   <element type="Sales.SalesOrderHeader" >
      <sql:relationship 
            key="CustomerID" 
            foreign-key="CustomerID" 
            key-relation="Sales.Customer" 
            foreign-relation="Sales.SalesOrderHeader" />
   </element>
</ElementType>
</Schema>

To test a sample XPath query against the schema

  1. Copy the schema code above and paste it into a text file. Save the file as TargetNS-Xdr.xml.

  2. Copy the following template and paste it into a text file. Save the file as TargetNS-XdrT.xml in the same directory where you saved TargetNS-Xdr.xml.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="TargetNS-Xdr.xml" 
                       xmlns:x="urn:MyNamespace" >
        x:Sales.Customer[@CustomerID=1]
      </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 (TargetNS-Xdr.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\TargetNS-Xdr.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

Here is the result set:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <y0:Sales.Customer xmlns:y0="urn:MyNamespace" CustomerID="1">
    <y0:Sales.SalesOrderHeader SalesOrderID="43860" /> 
    <y0:Sales.SalesOrderHeader SalesOrderID="44501" /> 
    <y0:Sales.SalesOrderHeader SalesOrderID="45283" /> 
    <y0:Sales.SalesOrderHeader SalesOrderID="46042" /> 
  </y0:Sales.Customer>
</ROOT>

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

See Also

Reference

Using XPath Queries in SQLXML 4.0

Help and Information

Getting SQL Server 2005 Assistance