Introducing XPath

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:5

0
50
lượt xem
13
download

Introducing XPath

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Giới thiệu Extensible Markup XPath The Path Language (XPath) là một ngôn ngữ mà cho phép bạn tìm kiếm và điều hướng một tài liệu XML, và bạn có thể sử dụng XPath với SQL Server. Trong phần này, bạn sẽ khám phá được cấu trúc của một tài liệu XML và làm thế nào để điều hướng và tìm kiếm một tài liệu XML bằng cách sử dụng XPath

Chủ đề:
Lưu

Nội dung Text: Introducing XPath

  1. 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 ALFKI Alfreds Futterkiste 12209 Germany 030-0074321 ANATR Ana Trujillo Emparedados y helados 05021 Mexico (5) 555-4729 Note You'll find all the XML files in the xml directory for this chapter . Note The line indicates that Customers.xml is an XML file that uses the 1.0 standard.
  2. 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
  3. 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:
  4. /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
  5. 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.
Đồng bộ tài khoản