Dokumen Contoh XML
Kita akan gunakan dokumen XML berikut dalam contoh di bawah ini.
<?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 Axes
XPath axes mewakili hubungan ke node konteks (saat ini) dan digunakan untuk menemukan node yang berhubungan dengan node tersebut di pohon. Berikut ini ada beberapa Axes dengan hasil axesnya :
AxisName | Hasil |
---|---|
ancestor | Memilih semua ancestor (parent, grandparent, dll) dari curent node. |
ancestor-or-self | Memilih semua ancestor (parent, grandparent, dll) dari current node dan current node itu sendiri |
attribute | Memilih semua atribut dari current node |
child | Memilih semua children dari current node |
descendant | Memilih semua descendants(children, grandchildren, dll) dari current node. |
descendant-or-self | Memilih semua descendants(children, grandchildren,, dll) dari current node dan current node itu sendiri |
following | Memilih semua yang ada di dokumen setelah tag penutup dari current node |
following-sibling | Memilih semua siblings setelah current node |
namespace | Memilih semua node namespace dari current node |
parent | Memilih parents dari current node |
preceding | Memilih semua node yang muncul sebelum node saat ini dalam dokumen, kecuali ancestor, node atribut, dan node namespace |
preceding-sibling | Memilih semua siblings sebelum current node |
self | Memilih current node |
Lokasi Path Expression
Lokasi path bisa absolut atau relatif.
Lokasi path absolut dimulai dengan garis miring (/) dan jalur lokasi relatif tidak. Dalam kedua kasus ini, jalur lokasi terdiri dari satu atau beberapa langkah, masing-masing dipisahkan oleh garis miring:
An absolute location path: /step/step/... A relative location path: step/step/...
Setiap langkah dievaluasi terhadap node di node-set saat ini.
Sebuah langkah terdiri dari:
- an axis (mendefinisikan hubungan pohon antara node yang dipilih dan current node)
- a node-test (mengidentifikasi node dalam axes)
- nol atau lebih predikat (untuk lebih menyempurnakan kumpulan node yang dipilih)
Sintaks untuk lokasi path adalah:
axisname::nodetest[predicate]
Contoh
Example | Hasil |
---|---|
child::book | Memilih semua node book yang merupakan anak dari current node |
attribute::lang | Memilih atribut lang dari current node |
child::* | Memilih semua child elemen dari current node |
attribute::* | Memilih semua atribut dari current node |
child::text() | Memilih semua child text node dari current node |
child::node() | Memilih semua child dari current node |
descendant::book | Memilih semua descendants buku dari current node |
ancestor::book | Memilih semua ancestor bukucurrent node |
ancestor-or-self::book | Memilih semua ancestor book dari current node dan saat ini juga jika itu adalah book node |
child::*/child::price | Memilih semua price grandchildren dari current node |