php - 通过 PHP SimpleXMLElement 在根元素中获取命名空间名称

标签 php xml namespaces simplexml xml-namespaces

假设我有以下 XML(这是 ATOM 的标准)

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

    <title>Example Feed</title>
    <subtitle>A subtitle.</subtitle>
    <link href="http://example.org/feed/" rel="self" />
    <link href="http://example.org/" />
    <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
    <updated>2003-12-13T18:30:02Z</updated>


    <entry>
        <title>Atom-Powered Robots Run Amok</title>
        <link href="http://example.org/2003/12/13/atom03" />
        <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
        <link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
        <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
        <updated>2003-12-13T18:30:02Z</updated>
        <summary>Some text.</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>This is the entry content.</p>
            </div>
        </content>
        <author>
            <name>John Doe</name>
            <email>johndoe@example.com</email>
        </author>
    </entry>

</feed>

另外,假设上述 XML 位于地址为 http://www.example.com/atom.xml 的网页中。

我知道您可以通过以下代码获取根元素名称:
// Using curl to access the page
$ch = curl_init('http://www.example.com/atom.xml');
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

$xml = new SimpleXmlElement($result);
$root = $xml->getName();
echo ($root)  // This prints "feed", which is the root element

另外,我知道以下调用将获取所有根命名空间。
$xml->getNamespaces(true)

在我们的例子中,返回值是:
Array
(
    [] => http://www.w3.org/2005/Atom
)

但是,我真的很想知道命名空间的名称。换句话说,我不知道调用哪个函数返回 xmlns .我如何知道命名空间的名称?我需要它在处理前进行一些验证。

请帮忙,谢谢。

最佳答案

"However, I really like to know the name of the namespace. In other words, I do not know which function to call that returs xmlns".


xmlns是命名空间声明的默认属性名称的 XML 语法。目前尚不清楚您要验证什么,但我认为可以安全地假设如果 getNamespaces()返回前缀为空的命名空间,则源 XML 具有有效的默认命名空间(换句话说,源 XML 具有 xmlns )。

引自 W3C “Namespaces in XML” 以供引用:
  • If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that of the default namespace in the scope of the element to which the declaration is attached. In such a default declaration, the attribute value may be empty. Default namespaces and overriding of declarations are discussed in "5. Applying Namespaces to Elements and Attributes". [Default Namespace]

  • [3] DefaultAttName ::= 'xmlns' . [Default Attribute Name]

  • 关于php - 通过 PHP SimpleXMLElement 在根元素中获取命名空间名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389021/

    相关文章:

    javascript - 如何使用jquery在按钮单击时显示表单?

    php - 将数组插入 MySQL

    php - 使用 laravel 中的迁移删除数据库中的主键和自动增量

    PHP 自动加载子文件夹

    namespaces - Clojure:函数的全限定名

    php - MySQL查询货币格式

    python - 在 Python 中使用 ETree 保存 XML。它不保留 namespace ,不添加 ns0、ns1 和删除 xmlns 标记

    c# - 想要将 XElement 的全部内容作为字符串返回

    xml - 将 DOM 序列化为 XML 文本文件的算法是什么?

    具有 C++ 作用域的 Emacs 点符号