1. Trang chủ
  2. » Công Nghệ Thông Tin

Introducing XPath

5 283 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 39,16 KB

Nội dung

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 <?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> 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. 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 . 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[CustomerID="ALFKI"]/* The following example returns the CompanyName of the customer with a CustomerID of ALFKI: /NorthwindCustomers/Customers[CustomerID="ALFKI"]/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 <?xml version="1.0"?> <NorthwindCustomers> <Customers CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" PostalCode="12209" Country="Germany" Phone="030-0074321" /> <Customers CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" PostalCode="05021" Country="Mexico" Phone="(5) 555-4729" /> </NorthwindCustomers> 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[@CustomerID="ALFKI"]/* The following example returns the CompanyName of the customer with a CustomerID of ALFKI: /NorthwindCustomers/Customers[@CustomerID="ALFKI"]/@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. . 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. 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

Ngày đăng: 07/11/2013, 15:15

Xem thêm

TỪ KHÓA LIÊN QUAN