Introducing XPath

The Extensible Markup Language Path (XPath) is a language that allows you to search and navigate an XML document, and you can use XPath with SQL Server. In this section, you'll explore the structure of an XML document and how to navigate and search an XML document using XPath. In later sections, you'll see how to use XPath with SQL Server.

XML Document Structure

An XML document file is divided into nodes, with the topmost node being referred to as the root node. The easiest way to understand how the structure works is to consider an example XML document; Listing 16.7 shows an XML document contained in the file Customers.xml.

Listing 16.7: CUSTOMERS.XML

start example
 <?xml version="1.0"?> <NorthwindCustomers>   <Customers>     <CustomerID>ALFKI</CustomerID>     <CompanyName>Alfreds Futterkiste</CompanyName>     <PostalCode>12209</PostalCode>     <Country>Germany</Country>     <Phone>030-0074321</Phone>   </Customers>   <Customers>     <CustomerID>ANATR</CustomerID>     <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>     <PostalCode>05021</PostalCode>     <Country>Mexico</Country>     <Phone>(5) 555-4729</Phone>   </Customers> </NorthwindCustomers> 
end example

Note 

You'll find all the XML files in the xml directory for this chapter .

Note 

The line <?xml version="1.0"?> indicates that Customers.xml is an XML file that uses the 1.0 standard.

Figure 16.3 shows a visual representation of the Customers.xml document structure.

click to expand
Figure 16.3: Customers.xml document structure

As you can see from Figure 16.3, an XML document is structured like an inverted tree. NorthwindCustomers is the root node. The two Customers nodes beneath the root node are known as a node set. The CustomerID, CompanyName, PostalCode, Country, and Phone are known as elements. Each Customers node and its CustomerID, CompanyName, PostalCode , Country, and Phone elements are known as a node subtree. A node located beneath another node is known as a child node, and the node above is known as the parent node; for example, the NorthwindCustomers node is the parent node of the child Customers nodes.

You can view an XML file using Microsoft Internet Explorer, as shown in Figure 16.4.

click to expand
Figure 16.4: Viewing Customers.xml in Internet Explorer

Tip 

To open the XML file, right-click Customers.xml in Windows Explorer and select Open With Internet Explorer from the pop-up menu.

XPath Expressions

To search or navigate an XML document file you supply an expression to XPath. These expressions work within a context, which is the current node being accessed within the XML file. The most commonly used ways of specifying the context are shown in Table 16.3.

Table 16.3: SPECIFYING THE CONTEXT

CHARACTERS

DESCRIPTION

/

Specifies the root node as the context.

./

Specifies the current node as the context.

../

Specifies the parent node as the context.

//

Specifies the whole XML document as the context.

.//

Specifies the whole XML document starting at the current node as the context.

Let's take a look at some example XPath expressions. The following example returns the Customers nodes:

 /NorthwindCustomers/Customers 

As you can see from this example, you specify the path down the tree structure to specify the nodes, separating each node with a forward slash (/) character.

You can also get all the Customers nodes using the following example, which uses // to specify the whole XML document as the context:

 //Customers 

The next example returns the Customers nodes and all their elements:

 /NorthwindCustomers/Customers/* 

Note 

The asterisk (*) specifies all the elements.

The next example returns just the CustomerID element of the Customers nodes:

 /NorthwindCustomers/Customers/CustomerID 

You can find elements in a node by specifying a search within square brackets []. The following example returns all the elements of the customer with a CustomerID of ALFKI:

 /NorthwindCustomers/Customers[Customer]/* 

The following example returns the CompanyName of the customer with a CustomerID of ALFKI:

 /NorthwindCustomers/Customers[Customer]/CompanyName 

You can also use square brackets to indicate the index of a node, starting at index 1. The following example returns the first Customers node:

 /NorthwindCustomers/Customers[1] 

You can use the last() function to get the last node. The following example returns the last Customers node:

 /NorthwindCustomers/Customers[last()] 

If your XML file contains embedded attributes rather than elements to hold values, then your XPath search expression is slightly different. Listing 16.8 shows an XML file named CustomersWithAttributes.xml that uses attributes.

Listing 16.8: CUSTOMERSWITHATTRIBUTES.XML

start example
 <?xml version="1.0"?> <NorthwindCustomers>   <Customers     Customer     CompanyName="Alfreds Futterkiste"     PostalCode="12209"     Country="Germany"     Phone="030-0074321"   />   <Customers     Customer     CompanyName="Ana Trujillo Emparedados y helados"     PostalCode="05021"     Country="Mexico"     Phone="(5) 555-4729"   /> </NorthwindCustomers> 
end example

To access an attribute you place an at (@) character at the start of the attribute name. The following example returns the CustomerID attribute of the Customers nodes:

 /NorthwindCustomers/Customers/@CustomerID 

The next example returns all the attributes of the customer with a CustomerID of ALFKI:

 /NorthwindCustomers/Customers[@Customer]/* 

The following example returns the CompanyName of the customer with a CustomerID of ALFKI:

 /NorthwindCustomers/Customers[@Customer]/@CompanyName 

Note 

I've only touched on XPath expressions in this section. You can use many other mathematical operators, Boolean expressions, and much more. You can learn more about XPath in the SQL Server Books Online documentation and at the World Wide Web Consortium's (WC3) Web site at www.w3.org; just look for XPath in the table of contents.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net