XML และ XPath

มันคืออะไร XPath?

XPath คือองค์ประกอบหลักในมาตรฐาน XSLT

XPath สามารถใช้เพื่อจับตามากับ (เดินทาง) องค์ประกอบและคุณสมบัติในเอกสาร XML

XPath

XPath
  • XPath คือภาษาใช้เพื่อกำหนดส่วนต่างๆของเอกสาร XML
  • XPath ใช้ประโยคทางหน้าที่เพื่อเดินทางในเอกสาร XML
  • XPath มีหลักฐานฟังก์ชัน
  • XPath คือองค์ประกอบหลักใน XSLT และ XQuery
  • XPath คือมาตรฐานที่ถูกแนะนำโดย W3C

XPath แสดงความทางหน้าที่

XPath ใช้ประโยคทางหน้าที่เพื่อเลือกตัวเลือกหรือกลุ่มตัวเลือกในเอกสาร XML ประโยคทางหน้าที่ดูเหมือนกับประโยคที่คุณเห็นเมื่อใช้ระบบไฟล์คอมพิวเตอร์ทางคอมพิวเตอร์แบบปกติ

XPath แสดงความเป็นภาษาใช้ใน JavaScript, Java, XML Schema, PHP, Python, C และ C++ และหลายภาษาอื่นๆ

XPath ใช้ใน XSLT

XPath คือองค์ประกอบหลักในมาตรฐาน XSLT

หากคุณเข้าใจ XPath คุณจะสามารถใช้ XSL ได้ดีที่สุด

ตัวอย่าง XPath

จะใช้ XML ดังนี้

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="美食">
  <title lang="zh">雅舍谈吃</title>
  <author>梁实秋</author>
  <year>2013</year>
  <price>35</price>
</book>
<book category="儿童">
  <title lang="zh">了不起的狐狸爸爸</title>
  <author>罗尔德·达尔</author>
  <year>2009</year>
  <price>10.00</price>
</book>
<book category="文学">
  <title lang="zh">将熟悉变为陌生</title>
  <author>齐格蒙·鲍曼</author>
  <author>彼得·哈夫纳</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="政治">
  <title lang="zh">论美国的民主</title>
  <author>托克维尔</author>
  <year>1989</year>
  <price>60.00</price>
</book>
</bookstore>

ในตารางด้านล่างนี้ เราจัดละเอียด XPath และผลลัพธ์ของแสดง

XPath แสดง ผลลัพธ์
/bookstore/book[1] เลือก element book ที่เป็น child ของ element bookstore ที่มีตำแหน่งเป็นที่หนึ่งของทั้งหมด。
/bookstore/book[last()] เลือก element book ที่เป็น child ของ element bookstore ที่มีตำแหน่งเป็นที่สุดของทั้งหมด。
/bookstore/book[last()-1] เลือก element book ที่เป็น child ของ element bookstore ที่มีตำแหน่งเป็นที่สองของทั้งหมด (จากท้ายของลำดับ)
/bookstore/book[position()<3] เลือก element book ที่เป็น child ของ element bookstore ที่มีตำแหน่งเป็นที่สองของทั้งหมด。
//title[@lang] เลือก element title ที่มี attribute ชื่อ lang。
//title[@lang='en'] เลือก element title ที่มี attribute lang และค่าเท่ากับ "en"。
/bookstore/book[price>35.00] เลือก element ที่มี attribute price ใน bookstore ที่มีค่าต่ำกว่า 35.00 ของ element book ทั้งหมด。
/bookstore/book[price>35.00]/title 選擇 bookstore 元素的 book 元素中,price 元素值大於 35.00 的所有 title 元素。

XPath 教程

您將在我們的 XPath 教程中學到有關 XPath 的更多知識。