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

XML XPath: Node – Code dan Contohnya

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Section Artikel

Terminologi XPath

Node

Di XPath, ada tujuh jenis node: elemen, atribut, teks, namespace, instruksi-pemrosesan, komentar, dan node dokumen.

Dokumen XML diperlakukan sebagai node-tree. Elemen paling atas dari pohon disebut elemen root.

Lihat dokumen XML berikut ini:

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

<bookstore>
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

Contoh node dalam dokumen XML di atas:

<bookstore> (root element node)

<author>J K. Rowling</author> (element node)

lang="en" (attribute node)

Nilai atom

Nilai atom adalah node tanpa child atau parents.

Contoh nilai atom:

J K. Rowling

"en"

Item

Item adalah nilai atau node atom.

Hubungan Node

Induk

Setiap elemen dan atribut memiliki satu parent.

Pada contoh berikut; unsur buku adalah parent dari judul, pengarang, tahun, dan harga:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Children

Node elemen mungkin memiliki nol, satu atau lebih turunan.

Pada contoh berikut; elemen judul, pengarang, tahun, dan harga semuanya merupakan child dari elemen buku:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Siblings

Node yang memiliki parent yang sama.

Pada contoh berikut; elemen judul, penulis, tahun, dan harga semuanya siblings:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Ancestors

A node’s parent, parent’s parent, dll.

Pada contoh berikut; ancestors dari elemen judul adalah elemen buku dan elemen toko buku:

<bookstore>

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

Descendants

A node’s children, children’s children, dll.

Pada contoh berikut: Descendants elemen toko buku adalah elemen buku, judul, pengarang, tahun, dan harga:

<bookstore>

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

You may also like