PHP: retrieve all declared namespaces of a DOMElement

前端 未结 2 889
误落风尘
误落风尘 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:50

    Note, that

    echo $root->getAttributeNode('xmlns')->nodeValue . "\n";
    echo $root->getAttribute('xmlns') . "\n";
    echo $root->getAttribute('xmlns:syn') . "\n";
    

    all work as expected, and print out

    http://purl.org/rss/1.0/
    http://purl.org/rss/1.0/
    http://purl.org/rss/1.0/modules/syndication/
    

    because DOMNameSpaceNode is a Node, not a NodeCollection.

    Just clarifying, that, unless something in PHP DOM extension changes, XPath (as explained by VolkerK) is the only native way to get all the namespaces, regardless of documentation.

提交回复
热议问题