Bewerken

Delen via


Introduction to Using XPath Queries (SQLXML 4.0)

Applies to: SQL Server Azure SQL Database

An XML Path Language (XPath) query can be specified as part of a URL or within a template. The mapping schema determines the structure of this resulting fragment, and the values are retrieved from the database. This process is conceptually similar to creating views using the CREATE VIEW statement and writing SQL queries against them.

Note

To understand XPath queries in SQLXML 4.0, you must be familiar with XML views and related concepts such as templates and mapping schema. For more information, see Introduction to Annotated XSD Schemas (SQLXML 4.0), and the XPath standard defined by the World Wide Web Consortium (W3C).

An XML document consists of nodes such as an element node, attribute node, text node, and so on. For example, consider this XML document:

<root>  
  <Customer cid= "C1" name="Janine" city="Issaquah">  
      <Order oid="O1" date="1/20/1996" amount="3.5" />  
      <Order oid="O2" date="4/30/1997" amount="13.4">Customer was  
          very satisfied</Order>  
   </Customer>  
   <Customer cid="C2" name="Ursula" city="Oelde" >  
      <Order oid="O3" date="7/14/1999" amount="100" note="Wrap it blue white red">  
          <Urgency>Important</Urgency>  
      </Order>  
      <Order oid="O4" date="1/20/1996" amount="10000"/>  
   </Customer>  
</root>  

In this document, <Customer> is an element node, cid is an attribute node, and "Important" is a text node.

XPath is a graph navigation language used to select a set of nodes from an XML document. Each XPath operator selects a node-set based on a node-set selected by a previous XPath operator. For example, given a set of <Customer> nodes, XPath can select all <Order> nodes with the date attribute value of "7/14/1999". The resulting node-set contains all the orders with order date 7/14/1999.

The XPath language is defined by the World Wide Web Consortium (W3C) as a standard navigation language. SQLXML 4.0 implements a subset of the W3C XPath specification, which is located at http://www.w3.org/TR/1999/PR-xpath-19991008.html.

The following are key differences between the W3C XPath implementation and the SQLXML 4.0 implementation.

  • Root queries

    SQLXML 4.0 does not support the root query (/). Every XPath query must begin at a top-level <ElementType> in the schema.

  • Reporting errors

    The W3C XPath specification defines no error conditions. XPath queries that fail to select any nodes return an empty node-set. In SQLXML 4.0, a query can return many types of error messages.

  • Document order

    In SQLXML 4.0, document order is not always determined. Therefore, numeric predicates and axes that use document order (such as following) are not implemented.

    The lack of document order also means that the string value of a node can be evaluated only when that node maps to a single column in a single row. An element with child elements or an IDREFS or NMTOKENS node cannot be converted to string.

    Note

    In some cases, the key-fields annotation or keys from the relationship annotation can result in a deterministic document order. However, this is not the primary use of these annotations For more information, see Identifying Key Columns Using sql:key-fields (SQLXML 4.0) and Specifying Relationships Using sql:relationship (SQLXML 4.0).

  • Data types

    SQLXML 4.0 has limitations in implementing the XPath string, number, and boolean data types. For more information, see XPath Data Types (SQLXML 4.0).

  • Cross-product queries

    SQLXML 4.0 does not support cross-product XPath queries, such as Customers[Order/@OrderDate=Order/@ShipDate]. This query selects all Customers with any Order for which the OrderDate equals the ShipDate of any Order.

    However, SQLXML 4.0 does support queries such as Customer[Order[@OrderDate=@ShippedDate]], which selects Customers with any Order for which the OrderDate equals its ShipDate.

  • Error handling and security

    Depending on the schema and XPath query expression that are used, Transact-SQL errors could be exposed to users under certain conditions.

The tables in the following sections provide details about how the implementation of XPath queries in SQLXML 4.0 differs from the W3C specification in these areas.

Supported Functionality

The following table shows the features of the XPath language that are implemented in SQLXML 4.0.

Feature Item Link to sample queries
Axes attribute, child, parent, and self axes Specifying Axes in XPath Queries (SQLXML 4.0)
Boolean-valued predicates including successive and nested predicates Specifying Arithmetic Operators in XPath Queries (SQLXML 4.0)
All relational operators =, !=, <, <=, >, >= Specifying Relational Operators in XPath Queries (SQLXML 4.0)
Arithmetic operators +, -, *, div Specifying Arithmetic Operators in XPath Queries (SQLXML 4.0)
Explicit conversion functions number(), string(), Boolean() Specifying Explicit Conversion Functions in XPath Queries (SQLXML 4.0)
Boolean operators AND, OR Specifying Boolean Operators in XPath Queries (SQLXML 4.0)
Boolean functions true(), false(), not() Specifying Boolean Functions in XPath Queries (SQLXML 4.0)
XPath variables Specifying XPath Variables in XPath Queries (SQLXML 4.0)

Unsupported Functionality

The following table shows the features of the XPath language that are not implemented in SQLXML 4.0.

Feature Item
Axes ancestor, ancestor-or-self, descendant, descendant-or-self (//), following, following-sibling, namespace, preceding, preceding-sibling
Numeric-valued predicates
Arithmetic operators mod
Node functions ancestor, ancestor-or-self, descendant, descendant-or-self (//), following, following-sibling, namespace, preceding, preceding-sibling
String functions string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize(), translate()
Boolean functions lang()
Numeric functions sum(), floor(), ceiling(), round()
Union operator |

When you specify XPath queries in a template, note the following behavior:

  • XPath can contain characters such as < or & that have special meanings in XML (and template is an XML document). You must escape these characters using XML &-encoding, or specify the XPath in the URL.

See Also

Using XPath Queries in SQLXML 4.0