Đi lại trong XML bằng XPATH part 2

5 5 0
Đi lại trong XML bằng XPATH part 2

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

Thông tin tài liệu

Cái bộ (Set of) Nodes do XPath trả về được gọi là Collection. Thông thường trong lập trình, từ Collection được dùng để nói đến một tập hợp các objects đồng loại. Ta có thể lần lượt đi qua (iterate through) các objects trong một Collection nhưng không được bảo đảm thứ tự của chúng, tức là gặp object nào trước hay object nào sau. Trái lại, trong chuẩn XPath, khi một Collection được trả về bởi một XPath Query (hỏi), nó giữ nguyên thứ tự các Nodes và cấp bậc của chúng trong tài liệu XML. Tức là nếu...

Bài Đi lại XML XPATH (phần II) Collections Cái (Set of) Nodes XPath trả gọi Collection Thơng thường lập trình, từ "Collection" dùng để nói đến tập hợp objects đồng loại Ta qua (iterate through) objects Collection không bảo đảm thứ tự chúng, tức gặp object trước hay object sau Trái lại, chuẩn XPath, Collection trả XPath Query (hỏi), giữ nguyên thứ tự Nodes cấp bậc chúng tài liệu XML Tức XPath trả cành nodes trừ nodes khơng thỏa điều kiện, node cịn lại giữ vị trí cành Vì Attributes Element khơng có thứ tự, nên chúng nằm lộn xộn Collection Indexing Collection Một Collection Nodes xem A rray Muốn nói trực tiếp đến Node Collection ta dùng index cặp ngoặc vng Node thứ có Index Cặp ngoặc vng ([]) có precedence cao (được tính trước) dấu slash(/) hay hai dấu slash (//) Dưới hai thí dụ: Expression Ý nghĩa author[1] Element author author[firstname][3] Element author thứ ba có Element firstname Mối liên hệ (Axes) Một location path dùng Axis để định mối liên hệ Nodes chọn context node Sau bảng liệt kê đầy đủ axes: Axes ancestor:: Ý nghĩa Tổ tiên context node Những tổ tiên context node gồm có cha, ơng nội, ơng cố v.v., ancestor:: axis ln ln kể root node trừ context node root node ancestor-or-self:: Chính context node tổ tiên Cái ancestor-or-self:: axis ln ln kể root node attribute:: Các Attributes context node Nếu context node khơng phải Element chắn axis trống rỗng child:: Con context node Một node nằm context node tree Tuy nhiên, A ttribute hay Namespace nodes không xem context node descendant:: Con cháu context node Con cháu con, cháu, chít, v.v., descendant:: axis khơng bao following:: chứa A ttribute hay Namespace nodes Mọi nodes sau context node tree, không kể cháu, A ttribute nodes, hay Namespace nodes following-sibling:: Mọi nodes em (nằm sau) context node axis nói đến Nodes con, Node cha, nằm tree sau context node Axis không kể Nodes anh nằm trước context node following-sibling:: Nếu context node A ttribute hay Namespace followingsibling:: axis trống rỗng namespace:: Những Namespace nodes context node Mỗi namespace có namespace node scope (phạm vi hoạt động) context node Nếu context node khơng phải Element Axis trống rỗng parent:: Node cha context node, có cha Node cha node nằm phía context node tree preceding:: Mọi nodes trước context node tree, không kể nodes tổ tiên, Attribute nodes, hay Namespace nodes Một cách để nhận diện preceding:: axis nodes kết thúc hoàn toàn trước context node bắt đầu preceding-sibling:: Mọi nodes anh (nằm trước) context node axis nói đến Nodes con, Node cha, nằm tree trước context node preceding-sibling:: Nếu context node A ttribute hay Namespace precedingsibling:: axis trống rỗng self:: Là context node Sàng lọc (Filters) Như ta thấy trên, để giới hạn lấy Nodes thỏa đáng điều kiện, ta gắn Filter (sàng lọc) vào Collection Filter Clause giống giống Clause WHERE ngôn ngữ SQL sở liệu Nếu Collection nằm filter, cho kết TRUE Collection trả Node FALSE Collection trống rỗng (empty) Thí dụ expression author/degree có nghĩa hàm biến đổi Collection trị số Boolean có giá trị TRUE hữa Element author có Element tên degree Filters ln ln tính theo context Nói cách khác, expression book[author] có nghĩa cho Element book tìm thấy, thử xem có chứa Element tên author không Tương tự vậy, book[author = 'Brown'] có nghĩa cho Element book tìm thấy, thử xem có chứa Element tên author với trị số Brown khơng Ta dùng dấu chấm (.) để khám current context node Thí dụ như, book[ = 'Dreams'] có nghĩa cho Element book tìm thấy current context, thử xem có trị số Dreams khơng Dưới thí dụ: Expression Ý nghĩa book[excerpt] Mọi Element book có chứa Element excerpt book[excerpt]/title Mọi Element title nằm Element book có chứa Element excerpt book[excerpt]/author[degree] Mọi Element author có chứa Element degree nằm Elements book có chứa Element excerpt book[author/degree] Mọi Element book có chứa Element author với Element degree book[excerpt][title] Mọi Element book có chứa Element excerpt Element title So sánh Để so sánh hai objects XPath ta dùng dấu (=) cho (!= ) cho không Mọi Element Attributes string, Typecast (xem ) số đem so sánh Expression Ý nghĩa author[lastname = "Smith"] Mọi Element author có chứa Element lastname với trị số Smith author[lastname[1] = "Smith"] Mọi Element author có Element lastname với trị số Smith author/degree[@from != "Harvard"] Mọi Element degree, Element author, có A ttribute from với trị số khơng phải "Harvard" author[lastname = /editor/lastname] Mọi Element author có chứa Element lastname với Element lastname root Element editor author[ = "John Hamilton"] Mọi Element author có trị số string John Hamilton Operator Union | (họp lại) Ngôn ngữ Xpath hỗ trợ Operator Union, giống Logical OR (hoặc là) Dưới vài thí dụ: Expression Ý nghĩa firstname | lastname Mọi Element firstname lastname current context (bookstore/book | bookstore/magazine) Mọi Element book hay magazine Element bookstore book | book/author Mọi Element book hay Element author Elements book (book | magazine)/price Mọi Element price Element book hay Element magazine Thử loại Node (Node Type Tests) Để chọn loại Node khác Element node, ta dùng Node-Type Test Mục đích việc dùng NodeType test để định lựa chọn khác thường Thí dụ như, descendant::text() cho ta text nodes cháu context node, loại node cháu context node Element Có loại Node-Type tests liệt kê Node type Trả Thí dụ comment() comment node following::comment() chọn comment nodes sau context node node() node preceding::node() chọn nodes trước context node processing-instruction() processing instruction node self::processing instruction() chọn processing instruction nodes context node text() text node child::text() chọn text nodes the context node Thử Node nhắm vào loại Processing Instruction Một node test chọn processing instruction thuộc loại nào, tức chọn mục tiêu (target) Cú pháp loại test là: processing-instruction("target") Thí dụ node test sau trả processing instruction nodes có nhắc đến XSL stylesheet tài liệu: /child::processing-instruction("xml-stylesheet") Thêm số thí dụ Location Path Expression Ý nghĩa /author Mọi Element author current context Expresion nầy tương đương với expression hàng kế author Mọi Element author current context /bookstore Document (Root) Element tên bookstore tài liệu nầy //author Mọi Element author tài liệu book[/bookstore/@specialty = @style] Mọi Element book có Attribute style với value value Attribute specialty Document Element bookstore tài liệu author/firstname Mọi Element firstname Elements author bookstore//title Mọi Element title hay nhiều bậc thấp hơn, tức cháu của, Element bookstore Lưu ý expression nầy khác với expression hàng kế bookstore/*/title Mọi Element title cháu bookstore bookstore//book/excerpt//emph Mọi Element emph nơi excerpt elements book , nơi element bookstore .//title Mọi Element title hay nhiều bậc thấp current context node author/* Mọi Element elements author book/*/lastname Mọi Element lastname cháu elements book */* Mọi Element cháu current context node *[@specialty] Mọi Element có Attribute specialty @style A ttribute style current context node price/@exchange A ttribute exchange Elements price current context, tức Elements price current context node price/@exchange/total Trả node set trống rỗng, Attributes khơng có Element Expression nầy chấp nhận văn phạm XML Path Language, không thật hợp lệ book[@style] Mọi Element book có Attribute style current context node Lưu ý phần nằm ngoặc vuông điều kiện Element book book/@style A ttribute style Element booktrong current context node Ở khơng có điều kiện hàng Ta nói đến A ttribute hay Element nằm bên phải @* Mọi A ttributes current context node author[1] Element author thứ current context node author[firstname][3] my:book Element author thứ ba có Element firstname Element book từ namespace my my:* Mọi Element namespace my Học XML Vovisoft © 2000 A ll rights reserved Last Updated: 23 Dec 2003 Webmaster ... firstname Element book từ namespace my my:* Mọi Element namespace my Học XML Vovisoft © 20 00 A ll rights reserved Last Updated: 23 Dec 20 03 Webmaster ... Expression nầy chấp nhận văn phạm XML Path Language, không thật hợp lệ book[@style] Mọi Element book có Attribute style current context node Lưu ý phần nằm ngoặc vuông đi? ??u kiện Element book book/@style... ngoặc vuông đi? ??u kiện Element book book/@style A ttribute style Element booktrong current context node Ở khơng có đi? ??u kiện hàng Ta nói đến A ttribute hay Element nằm bên phải @* Mọi A ttributes

Ngày đăng: 11/05/2021, 00:59

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan