PHP: retrieve all declared namespaces of a DOMElement

前端 未结 2 892
误落风尘
误落风尘 2021-01-18 02:04

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

2条回答
  •  余生分开走
    2021-01-18 02:44

    Unless there is a more direct way you can use XPath and its namespace axis.
    e.g.

    loadxml('
    ...
    ');
    $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.

提交回复
热议问题