Specifying XPath Variables in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying XPath Variables in XPath Queries

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 XPath Queries.

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'>ALFKI</sql:param>
     <sql:param name='EmployeeID'>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">
   Employee[@EmployeeID=$EmployeeID] 
  </sql:xpath-query>
</ROOT>

This template is stored in a file (MyTemplate.xml) and executed using a URL:

http://IISServer/VirtualRoot/template/MyTemplate.xml

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

In the following URL, the CustomerID parameter value is provided. Therefore, the default customer ID value ALFKI is ignored. Because no value is provided for the EmployeeID parameter, the default value is used.

http://IISServer/VirtualRoot/template/MyTemplate.xml?CustomerID=BERGS

In the following URL, both parameter values are passed (default values are ignored).

http://IISServer/VirtualRoot/template/MyTemplate.xml?CustomerID=BERGS&EmployeeID=2

Note  The XPath query can be specified directly in the URL: http://IISServer/nwind/schema/SampleSchema1.xml/Customer[@CustomerID=$CustomerID]?CustomerID=ANATR&root=root.

The virtual name schema is of schema type. The schema file is stored in the directory associated with virtual name of schema type. The root parameter is used to specify a top-level element for the resulting XML document (root can be any value).