SimpleXML 无法找到命名空间元素

发布于 2024-10-10 02:59:51 字数 1961 浏览 0 评论 0原文

我似乎无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-10-17 02:59:51

children() 只提供上下文节点的[直接]子节点,而不是后代节点。

$html = simplexml_load_file('test.html');

// get <body/>'s children
$html->body->children('zuq', true);

// use XPath to get all zuq:* nodes
$html->xpath('//zuq:*');

children() only gives you the [direct] children of the context node, not descendants.

$html = simplexml_load_file('test.html');

// get <body/>'s children
$html->body->children('zuq', true);

// use XPath to get all zuq:* nodes
$html->xpath('//zuq:*');
錯遇了你 2024-10-17 02:59:51

仅供参考,如果名称空间定义 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文