Hàm registerXPathNamespace () trong PHP
Thí dụ
Tạo bối cảnh không gian tên cho truy vấn XPath tiếp theo:
<?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>";
}
?>
Định nghĩa và Cách sử dụng
Hàm registerXPathNamespace () tạo bối cảnh không gian tên cho truy vấn XPath tiếp theo.
Hàm này hữu ích nếu tiền tố không gian tên được thay đổi trong tài liệu XML. Hàm registerXPathNamespace () sẽ tạo tiền tố cho không gian tên được chỉ định, để các nút XML bị ảnh hưởng có thể được truy cập mà không làm thay đổi mã ứng dụng quá nhiều.
Cú pháp
SimpleXMLElement::registerXPathNamespace(prefix, ns)
Giá trị tham số
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 |
Chi tiết kỹ thuật
Giá trị trả lại: | ĐÚNG về thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 5.1+ |
❮ Tham chiếu PHP SimpleXML