Tutorial XML

RUMAH XML Pengantar XML XML Cara menggunakan Pohon XML Sintaks XML Elemen XML Atribut XML Ruang Nama XML Tampilan XML XML HttpRequest Pengurai XML XML DOM XML XPath XML XSLT XML XQuery XML XLink Validator XML XML DTD Skema XML Server XML Contoh XML Kuis XML Sertifikat XML

XML AJAX

Pengenalan AJAX AJAX XMLHttp Permintaan AJAX Tanggapan AJAX File XML AJAX AJAX PHP AJAX ASP Basis Data AJAX Aplikasi AJAX Contoh AJAX

XML DOM

Pengenalan DOM Node DOM Mengakses DOM Info Simpul DOM Daftar Simpul DOM Melintasi DOM Navigasi DOM DOM Dapatkan Nilai DOM Ubah Node DOM Hapus Node DOM Ganti Node DOM Buat Node DOM Tambahkan Node Node Klon DOM Contoh DOM

Tutorial XPath

Pengenalan XPath Node XPath Sintaks XPath Sumbu XPath Operator XPath Contoh XPath

Tutorial XSLT

Pengenalan XSLT Bahasa XSL Transformasi XSLT XSLT <templat> XSLT <nilai-dari> XSLT <untuk-setiap> XSLT <sort> XSLT <jika> XSLT <pilih> Terapkan XSLT XSLT di Klien XSLT di Server XSLT Sunting XML Contoh XSLT

Tutorial XQuery

Pengenalan XQuery Contoh XQuery XQuery FLWOR HTML XQuery Persyaratan XQuery Sintaks XQuery Tambahkan XQuery XQuery Pilih Fungsi XQuery

XML DTD

Pengenalan DTD Blok Bangunan DTD Elemen DTD Atribut DTD Elemen DTD vs Attr Entitas DTD Contoh DTD

Skema XSD

Pengenalan XSD XSD Bagaimana caranya? XSD <skema> Elemen XSD Atribut XSD Pembatasan XSD

Kompleks XSD

Elemen XSD XSD Kosong Elemen XSD Saja Hanya Teks XSD Campuran XSD Indikator XSD XSD <apa saja> XSD <anyAttribute> Pergantian XSD Contoh XSD

Data XSD

String XSD Tanggal XSD XSD Numerik XSD Lain-lain Referensi XSD

Layanan Web

Layanan XML XML WSDL XML SOAP XML RDF XML RSS

Referensi

Jenis Node DOM Simpul DOM Daftar Simpul DOM DOM BernamaNodeMap Dokumen DOM Elemen DOM Atribut DOM Teks DOM DOM CDATA Komentar DOM DOM XMLHttpRequest Pengurai DOM Elemen XSLT Fungsi XSLT/XPath

Metode XML DOM compareDocumentPosition()


Objek Simpul

Contoh

Fragmen kode berikut memuat " books.xml " ke dalam xmlDoc dan membandingkan penempatan dua node (elemen <book> pertama dan ketiga) dalam hierarki DOM:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName('book')[0];
    var y = xmlDoc.getElementsByTagName('book')[2];
    document.getElementById("demo").innerHTML =
    x.compareDocumentPosition(y);
}

Output dari kode di atas akan menjadi:

4

Sebagian besar browser, akan memperlakukan spasi kosong atau baris baru sebagai node teks, IE 9 dan sebelumnya tidak. Jadi, pada contoh di atas, sebagian besar browser akan menampilkan 4, sedangkan IE 9 dan sebelumnya akan menampilkan 2.


Definisi dan Penggunaan

Metode compareDocumentPosition() membandingkan penempatan dua node dalam hierarki DOM (dokumen).


Dukungan Peramban

Internet Explorer Firefox Opera Google Chrome Safari

Metode compareDocumentPosition() didukung di semua browser utama.

Catatan: Internet Explorer 9 dan versi sebelumnya tidak mendukung metode ini.


Sintaksis

nodeObject.compareDocumentPosition(node)

Parameter

Parameter Type Description
node Node object Required. Specifies the node to compare with the current node

Nilai Kembali

Type Description
Number A Number representing where the two nodes are positioned compared to each other. The possible return values are:

1 - No relationship, the two nodes do not belong to the same document.

2 - The specified node precedes the current node.

4 - The specified node follows the current node.

8 - The specified node contains the current node.

16 - The specified node is contained by the current node.

32 - The specified and the current node have no common container node or the two nodes are different attributes of the same node.

Note: The return value could also be a combination of values. E.g. a return value of 20 means that the specified node is contained by the current node (16) AND the specified node follows the current node (4).

Detail Teknis

Versi DOM Objek Node Tingkat 3 Inti

Objek Simpul