Using sql:id-prefix

XML and Internet Support

XML and Internet Support

Using sql:id-prefix

The sql:id-prefix annotation is used to create a valid XML ID, IDREF, or IDREFS attribute.

In an XML document, the values of ID type attributes must be distinct. If there are multiple ID type attributes in an XML document, to ensure that the values of these attributes are distinct, specify the sql:id-prefix attribute for the ID type attributes. sql:id-prefix is also used to create named tokens from numbers. The value specified for sql:id-prefix must be a valid name character.

The sql:id-prefix attribute is used to prepend the values of ID, IDREF, and IDREFS with a string, thereby, making it unique. No checks are made to ensure the validity of the prefixes and the uniqueness of the values of ID, IDREF, or IDREFS.

sql:id-prefix is ignored on attributes that are not of type ID, IDREF, or IDREFS.

Note  Each value of the ID, IDREF, and IDREFS attributes is limited to 4,000 characters, including the prefix (if specified).

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 sql:id-prefix for an ID type attribute

In this XDR schema, OrderID and EmployeeID attributes are declared as ID type. To ensure that the IDs are unique and valid, sql:id-prefix annotation is specified for these attributes:

<?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="Order" sql:relation="Orders" sql:key-fields="OrderID">
    <AttributeType name="OrderID" dt:type="id" sql:id-prefix="Ord-" />
    <AttributeType name="OrderDate" />
 
    <attribute type="OrderID" />
    <attribute type="OrderDate" />
  </ElementType>

  <ElementType name="Employee" sql:relation="Employees">
    <AttributeType name="EmployeeID" dt:type="id" />
     <AttributeType name="LastName" />

    <attribute type="EmployeeID" />
    <attribute type="LastName" />
    <AttributeType name="OrderList" dt:type="idrefs"
                                 sql:id-prefix="Ord-" />
    <attribute type="OrderList" sql:relation="Orders" sql:field="OrderID">
        <sql:relationship
                key-relation="Employees"
                key="EmployeeID"
                foreign-relation="Orders"
                foreign-key="EmployeeID" />
    </attribute>
    <element type="Order">
        <sql:relationship key-relation="Employees"
                          key="EmployeeID"
                          foreign-relation="Orders"
                          foreign-key="EmployeeID" />
    </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. The XPath query in the template returns the <Employee> and <Order> subelements where EmployeeID is 1.
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="MySchema.xml">
        Employee[@EmployeeID="1"]
      </sql:xpath-query>
    </ROOT>
    
  3. This URL executes the template:
    http://IISServer/VirtualRoot/template/MySchemaT.xml
    

Here is the partial result set:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Employee EmployeeID="1" LastName="Davolio" OrderList="Ord-10258 Ord-
            10270 Ord-10275 Ord-10285">
    <Order OrderID="Ord-10258" OrderDate="1996-07-17T00:00:00" /> 
    <Order OrderID="Ord-10270" OrderDate="1996-08-01T00:00:00" /> 
    <Order OrderID="Ord-10275" OrderDate="1996-08-07T00:00:00" /> 
    <Order OrderID="Ord-10285" OrderDate="1996-08-20T00:00:00" /> 
  </Employee>
</ROOT>

See Also

IIS Virtual Directory Management for SQL Server

Using XPath Queries

Accessing SQL Server Using HTTP

Executing Template Files Using HTTP