SimpleXML 无法找到命名空间元素
我似乎无法让 PHP 的 SimpleXML 类识别 XHTML 文档中带前缀的命名空间元素。这是我的示例:
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/zuq">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<zuq:region name="myRegion">
<div class="myClass">
<h1><zuq:data name="myDataHeading" /></h1>
<p><zuq:data name="myDataParagraph" /></p>
</div>
</zuq:region>
</body>
</html>
当我执行以下操作时:
$sxml = simplexml_load_file('test.html');
print_r($sxml);
它返回:
SimpleXMLElement Object
(
[head] => SimpleXMLElement Object
(
[meta] => SimpleXMLElement Object
(
[@attributes] => Array
(
[http-equiv] => Content-Type
[content] => text/html; charset=utf-8
)
)
[title] => Untitled Document
)
[body] => SimpleXMLElement Object
(
[h1] => Heading
[p] => Paragraph
)
)
但是当我执行以下操作时:
$sxml = simplexml_load_file('test.html');
$sxml_zuq = $sxml->children('zuq', true);
print_r($sxml_zuq);
它返回空:
SimpleXMLElement Object
(
)
使用 foreach
迭代对象,否则不会似乎不起作用,并且在 children()
中使用 URI 而不是前缀也会失败。
我显然在某个地方犯了错误,但我不确定在哪里,因为我的尝试与我在阅读中遇到的许多教程示例完全相同。
这是怎么回事?
I can't seem to get PHP's SimpleXML class to recognize prefixed namespace elements in an XHTML document. Here's my example:
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/zuq">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<zuq:region name="myRegion">
<div class="myClass">
<h1><zuq:data name="myDataHeading" /></h1>
<p><zuq:data name="myDataParagraph" /></p>
</div>
</zuq:region>
</body>
</html>
When I do the following:
$sxml = simplexml_load_file('test.html');
print_r($sxml);
It returns:
SimpleXMLElement Object
(
[head] => SimpleXMLElement Object
(
[meta] => SimpleXMLElement Object
(
[@attributes] => Array
(
[http-equiv] => Content-Type
[content] => text/html; charset=utf-8
)
)
[title] => Untitled Document
)
[body] => SimpleXMLElement Object
(
[h1] => Heading
[p] => Paragraph
)
)
But when I do the following:
$sxml = simplexml_load_file('test.html');
$sxml_zuq = $sxml->children('zuq', true);
print_r($sxml_zuq);
It returns empty:
SimpleXMLElement Object
(
)
Iterating through the object with foreach
or otherwise doesn't seem to work, and using the URI rather than the prefix in children()
also fails.
I've obviously made a mistake somewhere, but I'm not sure where, as my attempt is quite identical to many tutorial examples I've come across in my reading.
What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
children()
只提供上下文节点的[直接]子节点,而不是后代节点。children()
only gives you the [direct] children of the context node, not descendants.仅供参考,如果名称空间定义 xmlns:zuq="http://localhost/zuq" 在实际 xml 元素而不是根节点中声明,那么我们必须另外调用 registerXPathNamespace 函数,然后使用 xpath,如http://www.dimuthu.org/blog/2008/ 09/30/xpath-in-simplexml/
Just for Info, in case the namespace definition , xmlns:zuq="http://localhost/zuq" was declared in the actual xml element instead of root node, then we have to additionally call registerXPathNamespace function and then use xpath as given in http://www.dimuthu.org/blog/2008/09/30/xpath-in-simplexml/