PHP:检索DOMElement的所有声明的命名空间 [英] PHP: retrieve all declared namespaces of a DOMElement
问题描述
我正在使用 DOM扩展程序来解析包含<一个href =http://en.wikipedia.org/wiki/Xml_namespace =nofollow noreferrer> xml命名空间。我会想到,命名空间声明就像任何其他属性一样对待,但我的测试似乎不同意。我有一个这样的文档:
< rdf:RDF
xmlns:rdf =http: /www.w3.org/1999/02/22-rdf-syntax-ns#
xmlns =http://purl.org/rss/1.0/
xmlns:taxo =http ://purl.org/rss/1.0/modules/taxonomy/
xmlns:dc =http://purl.org/dc/elements/1.1/
xmlns:syn =http ://purl.org/rss/1.0/modules/syndication/
xmlns:prism =http://purl.org/rss/1.0/modules/prism/
xmlns:admin = http://webns.net/mvcb/
>
此类测试代码如下:
$ doc = new DOMDocument();
$ doc-> loadXml(file_get_contents('/ home / soulmerge / tmp / rss1.0 / recent.xml'));
$ root = $ doc-> documentElement;
var_dump($ root-> tagName);
#prints'string(7)rdf:RDF'
var_dump($ root-> attributes-> item(0));
#prints'NULL'
var_dump($ root-> getAttributeNode('xmlns'));
#prints'object(DOMNameSpaceNode)#3(0){}'
所以问题是:
- 有谁知道在哪里可以找到
DOMNameSpaceNode
的文档? 在php.net上搜索不产生任何有用的结果。 - 如何从该DOMElement中提取所有这些命名空间声明?
除非有更直接的方式,否则可以使用XPath及其命名空间轴。
例如
<?php
$ doc = new DOMDocument;
$ doc-> loadxml('< rdf:RDF
xmlns:rdf =http://www.w3.org/1999/02/22-rdf-syntax-ns#
xmlns =http://purl.org/rss/1.0/
xmlns:taxo =http://purl.org/rss/1.0/modules/taxonomy/
xmlns :dc =http://purl.org/dc/elements/1.1/
xmlns:syn =http://purl.org/rss/1.0/modules/syndication/
xmlns :prism =http://purl.org/rss/1.0/modules/prism/
xmlns:admin =http://webns.net/mvcb/
>
...
< / rdf:RDF>');
$ context = $ doc-> documentElement;
$ xpath = new DOMXPath($ doc);
foreach($ xpath-> query('namespace :: *',$ context)as $ node){
echo $ node-> nodeValue,\\\
;
}
打印
http://www.w3.org/XML/1998/namespace
http://webns.net/mvcb/
http://purl.org/ rss / 1.0 / modules / prism /
http://purl.org/rss/1.0/modules/syndication/
http://purl.org/dc/elements/1.1/
http://purl.org/rss/1.0/modules/taxonomy/
http://purl.org/rss/1.0/
http://www.w3.org/1999/02/ 22-rdf-syntax-ns#
编辑和btw:我还没有找到DOMNameSpaceNode的文档。但是您可以从ext / dom / php_dom.c中的源代码中扣除(部分)其功能
它似乎没有暴露任何方法并公开属性
nodeName,nodeValue,nodeType,
prefix,localName,namespaceURI,
ownerDocument,parentNode
由与相应的DOMNode属性相同的功能处理。 p>
I am using the DOM extension to parse an xml file containing xml namespaces. I would have thought that namespace declarations are treated just like any other attribute, but my tests seem to disagree. I have a document that starts like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
xmlns:prism="http://purl.org/rss/1.0/modules/prism/"
xmlns:admin="http://webns.net/mvcb/"
>
And a test code like this:
$doc = new DOMDocument();
$doc->loadXml(file_get_contents('/home/soulmerge/tmp/rss1.0/recent.xml'));
$root = $doc->documentElement;
var_dump($root->tagName);
# prints 'string(7) "rdf:RDF"'
var_dump($root->attributes->item(0));
# prints 'NULL'
var_dump($root->getAttributeNode('xmlns'));
# prints 'object(DOMNameSpaceNode)#3 (0) {}'
So the questions are:
- Does anyone know where could I find the documentation of
DOMNameSpaceNode
? A search on php.net does not yield any useful result. - How do I extract all those namespace declarations from that DOMElement?
Unless there is a more direct way you can use XPath and its namespace axis.
e.g.
<?php
$doc = new DOMDocument;
$doc->loadxml('<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
xmlns:prism="http://purl.org/rss/1.0/modules/prism/"
xmlns:admin="http://webns.net/mvcb/"
>
...
</rdf:RDF>');
$context = $doc->documentElement;
$xpath = new DOMXPath($doc);
foreach( $xpath->query('namespace::*', $context) as $node ) {
echo $node->nodeValue, "\n";
}
prints
http://www.w3.org/XML/1998/namespace
http://webns.net/mvcb/
http://purl.org/rss/1.0/modules/prism/
http://purl.org/rss/1.0/modules/syndication/
http://purl.org/dc/elements/1.1/
http://purl.org/rss/1.0/modules/taxonomy/
http://purl.org/rss/1.0/
http://www.w3.org/1999/02/22-rdf-syntax-ns#
edit and btw: I haven't found documentation for DOMNameSpaceNode either. But you can "deduct" (parts of) its functionality from the source code in ext/dom/php_dom.c
It doesn't seem to expose any methods and exposes the properties
"nodeName", "nodeValue", "nodeType",
"prefix", "localName", "namespaceURI",
"ownerDocument", "parentNode"
all handled by the same functions as the corresponding DOMNode properties.
这篇关于PHP:检索DOMElement的所有声明的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!