Fungsi PHP registerXPathNamespace()
Contoh
Buat konteks namespace untuk kueri XPath berikutnya:
<?php
$xml=<<<XML
<book xmlns:chap="http://example.org/chapter-title">
<title>My Book</title>
<chapter id="1">
<chap:title>Chapter 1</chap:title>
<para>Donec velit. Nullam eget tellus...</para>
</chapter>
<chapter id="2">
<chap:title>Chapter 2</chap:title>
<para>Lorem ipsum dolor sit amet....</para>
</chapter>
</book>
XML;
$sxe=new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('c','http://example.org/chapter-title');
$result=$sxe->xpath('//c:title');
foreach ($result as $title)
{
echo $title . "<br>";
}
?>
Definisi dan Penggunaan
Fungsi registerXPathNamespace() membuat konteks namespace untuk kueri XPath berikutnya.
Fungsi ini berguna jika awalan namespace diubah dalam dokumen XML. Fungsi registerXPathNamespace() akan membuat awalan untuk namespace yang ditentukan, sehingga node XML yang terpengaruh dapat diakses tanpa terlalu banyak mengubah kode aplikasi.
Sintaksis
SimpleXMLElement::registerXPathNamespace(prefix, ns)
Nilai Parameter
Parameter | Description |
---|---|
prefix | Required. Specifies the namespace prefix to use in the XPath query for the namespace given in ns |
ns | Required. Specifies the namespace to use for the XPath query |
Detail Teknis
Nilai Kembali: | BENAR pada kesuksesan. SALAH pada kegagalan |
---|---|
Versi PHP: | 5.1+ |
Referensi PHP SimpleXML