Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Specifying Explicit Conversion Functions in XPath Queries

Specifying Explicit Conversion Functions in XPath Queries

SQL Server 2000

The following examples show how explicit conversion functions are specified 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 number() explicit conversion function

The number() function converts an argument to a number.

Assume the value of EmployeeID is nonnumeric, the following query converts EmployeeID to a number and compares it with the value 4. The query returns all <Employee> element children of the context node with the EmployeeID attribute that has a numeric value of 4:

/child::Employee[number(attribute::EmployeeID)=4]

A shortcut to the attribute axis (@) can be specified, and because the child axis is the default, it can be omitted from the query:

/Employee[number(@EmployeeID)=4]

In relational terms, the query returns an employee with an EmployeeID of 4.

To test the XPath query against the mapping schema

  1. Create the following template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        /Employee[number(@EmployeeID)=4]
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
    
B. Use the string() explicit conversion function

The string() function converts an argument to a string.

The following query converts EmployeeID to a string and compares it with the value 4. The query returns all <Employee> element children of the context node with the EmployeeID attribute that has a string value of 4:

/child::Employee[string(attribute::EmployeeID)="4"]

A shortcut to the attribute axis (@) can be specified, and because the child axis is the default, it can be omitted from the query:

/Employee[string(@EmployeeID)="4"]

In relational terms, the query returns an employee who has an EmployeeID of 4.

The following query returns <Customer> elements with a ContactName attribute that is a nonempty string:

Customer[string(@ContactName)=true()]

To test the XPath query against the mapping schema

  1. Create the folloiwng template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        Employee[string(@EmployeeID)="4"]
      </sql:xpath-query>
    </ROOT>
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
Show:
© 2015 Microsoft