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.
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.
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()]