XML Tutorial

XML หน้าแรก บทนำ XML XML วิธีใช้งาน XML Tree ไวยากรณ์ XML องค์ประกอบ XML คุณสมบัติ XML XML เนมสเปซ จอแสดงผล XML XML HttpRequest XML Parser XML DOM XML XPath XML XSLT XML XQuery XML XLink เครื่องมือตรวจสอบ XML XML DTD XML Schema XML Server ตัวอย่าง XML แบบทดสอบ XML ใบรับรอง XML

XML AJAX

บทนำ AJAX AJAX XMLHttp คำขอ AJAX การตอบสนอง AJAX ไฟล์ AJAX XML AJAX PHP AJAX ASP ฐานข้อมูล AJAX แอปพลิเคชัน AJAX ตัวอย่าง AJAX

XML DOM

บทนำ DOM โหนด DOM การเข้าถึง DOM ข้อมูลโหนด DOM รายการโหนด DOM DOM Traversing การนำทาง DOM DOM รับค่า DOM เปลี่ยนโหนด DOM ลบโหนด DOM แทนที่โหนด DOM สร้างโหนด DOM เพิ่มโหนด DOM โคลนโหนด ตัวอย่าง DOM

บทช่วย สอนXPath

บทนำ XPath โหนด XPath ไวยากรณ์ XPath แกน XPath ตัวดำเนินการ XPath ตัวอย่าง XPath

กวดวิชาXSLT

บทนำ XSLT ภาษา XSL การแปลง XSLT XSLT <แม่แบบ> XSLT <ค่าของ> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <เลือก> ใช้ XSLT XSLT บนไคลเอนต์ XSLT บนเซิร์ฟเวอร์ XSLT แก้ไข XML ตัวอย่าง XSLT

กวดวิชาXQuery

บทนำ XQuery ตัวอย่าง XQuery XQuery FLWOR XQuery HTML ข้อกำหนด XQuery ไวยากรณ์ XQuery XQuery เพิ่ม XQuery Select ฟังก์ชัน XQuery

XML DTD

บทนำ DTD บล็อกตัวต่อ DTD องค์ประกอบ DTD คุณสมบัติ DTD องค์ประกอบ DTD เทียบกับ Attr หน่วยงาน DTD ตัวอย่าง DTD

ส คีมา XSD

บทนำ XSD XSD วิธีการ XSD <schema> องค์ประกอบ XSD แอตทริบิวต์ XSD ข้อจำกัด XSD

XSD Complex

องค์ประกอบ XSD XSD ว่างเปล่า องค์ประกอบ XSD เท่านั้น XSD Text เท่านั้น XSD ผสม ตัวชี้วัด XSD XSD <ใด ๆ> XSD <anyAttribute> การทดแทน XSD XSD ตัวอย่าง

ข้อมูลXSD

XSD สตริง วันที่ XSD XSD ตัวเลข XSD เบ็ดเตล็ด ข้อมูลอ้างอิง XSD

บริการเว็บ

XML Services XML WSDL XML SOAP XML RDF XML RSS

อ้างอิง

ประเภทโหนด DOM โหนด DOM DOM NodeList DOM NamedNodeMap เอกสาร DOM องค์ประกอบ DOM แอตทริบิวต์ DOM ข้อความ DOM DOM CDATA ความคิดเห็นของ DOM DOM XMLHttpRequest DOM Parser องค์ประกอบ XSLT ฟังก์ชัน XSLT/XPath

ไวยากรณ์ XPath


XPath ใช้นิพจน์เส้นทางเพื่อเลือกโหนดหรือชุดโหนดในเอกสาร XML โหนดถูกเลือกโดยทำตามเส้นทางหรือขั้นตอน


เอกสารตัวอย่าง XML

เราจะใช้เอกสาร XML ต่อไปนี้ในตัวอย่างด้านล่าง

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

การเลือกโหนด

XPath ใช้นิพจน์เส้นทางเพื่อเลือกโหนดในเอกสาร XML โหนดถูกเลือกโดยทำตามเส้นทางหรือขั้นตอน นิพจน์เส้นทางที่มีประโยชน์ที่สุดจะแสดงรายการด้านล่าง:

Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

ในตารางด้านล่าง เราได้แสดงรายการนิพจน์เส้นทางและผลลัพธ์ของนิพจน์:

Path Expression Result
bookstore Selects all nodes with the name "bookstore"
/bookstore Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@lang Selects all attributes that are named lang


ภาคแสดง

เพรดิเคตใช้เพื่อค้นหาโหนดเฉพาะหรือโหนดที่มีค่าเฉพาะ

เพรดิเคตจะฝังอยู่ในวงเล็บเหลี่ยมเสมอ

ในตารางด้านล่าง เราได้แสดงรายการนิพจน์เส้นทางบางส่วนที่มีเพรดิเคตและผลลัพธ์ของนิพจน์:

Path Expression Result
/bookstore/book[1] Selects the first book element that is the child of the bookstore element.

Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1]. To solve this problem in IE, set the SelectionLanguage to XPath:

In JavaScript: xml.setProperty("SelectionLanguage","XPath");
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang='en'] Selects all the title elements that have a "lang" attribute with a value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

การเลือกโหนดที่ไม่รู้จัก

สามารถใช้สัญลักษณ์แทน XPath เพื่อเลือกโหนด XML ที่ไม่รู้จัก

Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind

ในตารางด้านล่าง เราได้แสดงรายการนิพจน์เส้นทางและผลลัพธ์ของนิพจน์:

Path Expression Result
/bookstore/* Selects all the child element nodes of the bookstore element
//* Selects all elements in the document
//title[@*] Selects all title elements which have at least one attribute of any kind

การเลือกหลายเส้นทาง

โดยใช้ | โอเปอเรเตอร์ในนิพจน์ XPath คุณสามารถเลือกได้หลายเส้นทาง

ในตารางด้านล่าง เราได้แสดงรายการนิพจน์เส้นทางและผลลัพธ์ของนิพจน์:

Path Expression Result
//book/title | //book/price Selects all the title AND price elements of all book elements
//title | //price Selects all the title AND price elements in the document
/bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document