Home » XML » XML XPath : Axes – Code dan Contohnya

XML XPath : Axes – Code dan Contohnya

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Section Artikel

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 :

AxisNameHasil
ancestorMemilih semua ancestor (parent, grandparent, dll) dari curent node.
ancestor-or-selfMemilih semua ancestor (parent, grandparent, dll) dari current node dan current node itu sendiri
attributeMemilih semua atribut dari current node
childMemilih semua children dari current node
descendantMemilih semua descendants(children, grandchildren, dll) dari current node.
descendant-or-selfMemilih semua descendants(children, grandchildren,, dll) dari current node dan current node itu sendiri
followingMemilih semua yang ada di dokumen setelah tag penutup dari current node
following-siblingMemilih semua siblings setelah current node
namespaceMemilih semua node namespace dari current node
parentMemilih parents dari current node
precedingMemilih semua node yang muncul sebelum node saat ini dalam dokumen, kecuali ancestor, node atribut, dan node namespace
preceding-siblingMemilih semua siblings sebelum current node
selfMemilih 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

ExampleHasil
child::bookMemilih semua node book yang merupakan anak dari current node
attribute::langMemilih 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::bookMemilih semua descendants buku dari current node
ancestor::book Memilih semua ancestor bukucurrent node
ancestor-or-self::bookMemilih semua ancestor book dari current node dan saat ini juga jika itu adalah book node
child::*/child::priceMemilih semua price grandchildren dari current node

You may also like