PHP registerXPathNamespace()ฟังก์ชั่น
ตัวอย่าง
สร้างบริบทเนมสเปซสำหรับเคียวรี XPath ถัดไป:
<?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>";
}
?>
ความหมายและการใช้งาน
ฟังก์ชัน registerXPathNamespace() จะสร้างบริบทเนมสเปซสำหรับการสืบค้น XPath ถัดไป
ฟังก์ชันนี้มีประโยชน์หากมีการเปลี่ยนแปลงคำนำหน้าเนมสเปซในเอกสาร XML ฟังก์ชัน registerXPathNamespace() จะสร้างส่วนนำหน้าสำหรับเนมสเปซที่ระบุ เพื่อให้สามารถเข้าถึงโหนด XML ที่ได้รับผลกระทบโดยไม่ต้องแก้ไขโค้ดของแอปพลิเคชันมากเกินไป
ไวยากรณ์
SimpleXMLElement::registerXPathNamespace(prefix, ns)
ค่าพารามิเตอร์
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 |
รายละเอียดทางเทคนิค
คืนมูลค่า: | จริงกับความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 5.1+ |
❮ การอ้างอิง PHP SimpleXML