Specifying XPath Variables in XPath Queries (SQLXML 4.0)

The following examples show how XPath variables are passed in XPath queries. The XPath queries in these examples are specified against the mapping schema contained in SampleSchema1.xml. For information about this sample schema, see Sample Annotated XSD Schema for XPath Examples (SQLXML 4.0).

Examples

A. Use the XPath variables

A sample template consists of two XPath queries. Each of the XPath queries takes one parameter. The template also specifies default values for these parameters. The default values are used if parameter values are not specified. Two parameters with default values are specified in <sql:header>.

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <sql:header>
     <sql:param name='CustomerID'>1</sql:param>
     <sql:param name='BusinessEntityID'>1</sql:param> 
  </sql:header>
  <sql:xpath-query mapping-schema="SampleSchema1.xml">
    Customer[@CustomerID=$CustomerID] 
  </sql:xpath-query >
  <sql:xpath-query mapping-schema="SampleSchema1.xml">
   Person[@BusinessEntityID=$BusinessEntityID] 
  </sql:xpath-query>
</ROOT>

To test the XPath query against the mapping schema

  1. Copy the sample schema code and paste it into a text file. Save the file as SampleSchema1.xml.

  2. Create the following template (XPathVariables.xml) and save it in the directory where:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:header>
         <sql:param name='CustomerID'>1</sql:param>
         <sql:param name='BusinessEntityID'>1</sql:param> 
      </sql:header>
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        Customer[@CustomerID=$CustomerID] 
      </sql:xpath-query >
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
       Person[@BusinessEntityID=$BusinessEntityID] 
      </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (SampleSchema1.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\SampleSchema1.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.

Note

In this example, no parameters are passed. Therefore, the default parameter values are used.