Introducing XPath
Số trang: 5
Loại file: pdf
Dung lượng: 39.16 KB
Lượt xem: 32
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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
Nội dung trích xuất từ tài liệu:
Introducing XPathIntroducing XPathThe Extensible Markup Language Path (XPath) is a language that allows you to searchand navigate an XML document, and you can use XPath with SQL Server. In this section,youll explore the structure of an XML document and how to navigate and search anXML document using XPath. In later sections, youll see how to use XPath with SQLServer.XML Document StructureAn XML document file is divided into nodes, with the topmost node being referred to asthe root node. The easiest way to understand how the structure works is to consider anexample XML document; Listing 16.7 shows an XML document contained in the fileCustomers.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 Youll 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.Figure 16.3 shows a visual representation of the Customers.xml document structure.Figure 16.3: Customers.xml document structureAs 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 nodeare known as a node set. The CustomerID, CompanyName, PostalCode, Country, andPhone 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 isknown as the parent node; for example, the NorthwindCustomers node is the parent nodeof 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 ExplorerTip To open the XML file, right-click Customers.xml in Windows Explorer and select Open With ➣ Internet Explorer from the pop-up menu.XPath ExpressionsTo search or navigate an XML document file you supply an expression to XPath. Theseexpressions work within a context, which is the current node being accessed within theXML file. The most commonly used ways of specifying the context are shown in Table16.3. Table 16.3: SPECIFYING THE CONTEXTCHARACTERS 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.Lets take a look at some example XPath expressions. The following example returns theCustomers nodes:/NorthwindCustomers/CustomersAs you can see from this example, you specify the path down the tree structure to specifythe nodes, separating each node with a forward slash (/) character.You can also get all the Customers nodes using the following example, which uses // tospecify the whole XML document as the context://CustomersThe 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/CustomerIDYou can find elements in a node by specifying a search within square brackets []. Thefollowing example returns all the elements of the customer with a CustomerID ofALFKI:/NorthwindCustomers/Customers[CustomerID=ALFKI]/*The following example returns the CompanyName of the customer with a CustomerID ofALFKI:/NorthwindCustomers/Customers[CustomerID=ALFKI]/CompanyNameYou can also use square brackets to indicate the index of a node, starting at index 1. Thefollowing example returns the first Customers node:/NorthwindCustomers/Customers[1]You can use the last() function to get the last node. The following example returns thelast Customers node:/NorthwindCustomers/Customers[last()]If your XML file contains embedded attributes rather than elements to hold values, thenyour XPath search expression is slightly different. Listing 16.8 shows an XML filenamed CustomersWithAttributes.xml that uses attributes.Listing 16.8: CUSTOMERSWITHATTRIBUTES.XML To access an attribute you place an at (@) character at the start of the attribute name. Thefollowing example returns the CustomerID attribute of the Customers nodes:/NorthwindCustomers/Customers/@CustomerIDThe 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 ofALFKI:/NorthwindCustomers/Customers[@CustomerID=ALFKI]/@CompanyNameNote Ive only touched on XPath expressions ...
Nội dung trích xuất từ tài liệu:
Introducing XPathIntroducing XPathThe Extensible Markup Language Path (XPath) is a language that allows you to searchand navigate an XML document, and you can use XPath with SQL Server. In this section,youll explore the structure of an XML document and how to navigate and search anXML document using XPath. In later sections, youll see how to use XPath with SQLServer.XML Document StructureAn XML document file is divided into nodes, with the topmost node being referred to asthe root node. The easiest way to understand how the structure works is to consider anexample XML document; Listing 16.7 shows an XML document contained in the fileCustomers.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 Youll 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.Figure 16.3 shows a visual representation of the Customers.xml document structure.Figure 16.3: Customers.xml document structureAs 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 nodeare known as a node set. The CustomerID, CompanyName, PostalCode, Country, andPhone 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 isknown as the parent node; for example, the NorthwindCustomers node is the parent nodeof 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 ExplorerTip To open the XML file, right-click Customers.xml in Windows Explorer and select Open With ➣ Internet Explorer from the pop-up menu.XPath ExpressionsTo search or navigate an XML document file you supply an expression to XPath. Theseexpressions work within a context, which is the current node being accessed within theXML file. The most commonly used ways of specifying the context are shown in Table16.3. Table 16.3: SPECIFYING THE CONTEXTCHARACTERS 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.Lets take a look at some example XPath expressions. The following example returns theCustomers nodes:/NorthwindCustomers/CustomersAs you can see from this example, you specify the path down the tree structure to specifythe nodes, separating each node with a forward slash (/) character.You can also get all the Customers nodes using the following example, which uses // tospecify the whole XML document as the context://CustomersThe 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/CustomerIDYou can find elements in a node by specifying a search within square brackets []. Thefollowing example returns all the elements of the customer with a CustomerID ofALFKI:/NorthwindCustomers/Customers[CustomerID=ALFKI]/*The following example returns the CompanyName of the customer with a CustomerID ofALFKI:/NorthwindCustomers/Customers[CustomerID=ALFKI]/CompanyNameYou can also use square brackets to indicate the index of a node, starting at index 1. Thefollowing example returns the first Customers node:/NorthwindCustomers/Customers[1]You can use the last() function to get the last node. The following example returns thelast Customers node:/NorthwindCustomers/Customers[last()]If your XML file contains embedded attributes rather than elements to hold values, thenyour XPath search expression is slightly different. Listing 16.8 shows an XML filenamed CustomersWithAttributes.xml that uses attributes.Listing 16.8: CUSTOMERSWITHATTRIBUTES.XML To access an attribute you place an at (@) character at the start of the attribute name. Thefollowing example returns the CustomerID attribute of the Customers nodes:/NorthwindCustomers/Customers/@CustomerIDThe 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 ofALFKI:/NorthwindCustomers/Customers[@CustomerID=ALFKI]/@CompanyNameNote Ive only touched on XPath expressions ...
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLTài liệu có liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 296 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 296 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 280 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 278 1 0 -
47 trang 250 4 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 246 0 0 -
80 trang 238 0 0
-
6 trang 229 0 0
-
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 226 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 223 0 0