<!--PHP编程实战-->
<!--XML -->
<!--14-12-->
<!--使用XPath并注册命名空间后查找文档的内容-->
<?php
error_reporting(E_ALL);
$xml = <<<THE_XML
<animals xmlns:dog="http://foobar.com/dog" xmlns:cat="http://foobar.com/cat">
<dog:name>snoopy</dog:name>
<dog:color>brown</dog:color>
<dog:breed>beagle cross</dog:breed>
<cat:name>teddy</cat:name>
<cat:color>brown</cat:color>
<cat:breed>tabby</cat:breed>
<dog:name>jade</dog:name>
<dog:color>black</dog:color>
<dog:breed>lab cross</dog:breed>
</animals>
THE_XML;
$xml_object = simplexml_load_string($xml);
$xml_object->registerXPathNamespace('cat', 'http://foobar.com/cat');
$xml_object->registerXPathNamespace('dog', 'http://foobar.com/dog');
$names = $xml_object->xpath("dog:name");
foreach ($names as $name) {
print $name . "<br/>";
}
?>
知识点
- 执行XPath时,需要注册命名空间,然后将命名空间作为查询元素的前缀.