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>";
}
?>
$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 節點可以在不改變應用程序代碼太多的情況下進行訪問。
語法
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
技術細節
返回值: | 如果成功則返回TRUE,如果失敗則返回FALSE。 |
---|---|
PHP 版本: | 5.2+ |
PHP SimpleXML參考手冊