title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | helpviewer_keywords | monikerRange | ||
---|---|---|---|---|---|---|---|---|---|---|---|
Specifying XPath Variables in XPath Queries (SQLXML) |
View examples of how to specify XPath variables in SQLXML 4.0 XPath queries. |
MikeRayMSFT |
mikeray |
03/16/2017 |
sql |
xml |
reference |
|
=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current |
[!INCLUDE SQL Server Azure SQL Database] 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).
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='ContactID'>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">
Contact[@ContactID=$ContactID]
</sql:xpath-query>
</ROOT>
-
Copy the sample schema code and paste it into a text file. Save the file as SampleSchema1.xml.
-
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='ContactID'>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"> Contact[@ContactID=$ContactID] </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"
-
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.