PHP: retrieve all declared namespaces of a DOMElement

Posted by soulmerge on Stack Overflow See other posts from Stack Overflow or by soulmerge
Published on 2010-03-18T13:57:11Z Indexed on 2010/03/18 14:01 UTC
Read the original article Hit count: 297

Filed under:
|
|

I am using the DOM extension to parse an xml file containing xml namespaces. I would have 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:

  1. Does anyone know where could I find the documentation of DOMNameSpaceNode? A search on php.net does not yield any useful result.
  2. How do I extract all those namespace declarations from that DOMElement?

© Stack Overflow or respective owner

Related posts about php

Related posts about dom