为下一个 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+ |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!