I had some difficulty stripping all default NS attributes for an ns-uri in one shot, the following will work though...first strip the documentElement namespace, then getElementsByTagNameNS() -- the documentation should reflect that the 2nd argument is actually the name of the tag, not the local namespace prefix as I first expected:
<?php
function strip_default_ns( $xml = null, $ns_uri = 'http://example.com/XML-Foo' ) {
$ns_local = '';
$ns_tag = '*';
if ( empty($xml) ) return false;
//remove document namespace
$dom = new DOMDocument();
$dom->loadXML($xml);
$dom->documentElement->removeAttributeNS($ns_uri, $ns_local);
//strip element namespaces
foreach ( $dom->getElementsByTagNameNS($ns_uri, $ns_tag) as $elem ) {
$elem->removeAttributeNS($ns_uri, $ns_local);
}
return $dom->saveXML();
}
$stripped_xml = strip_default_ns($the_xml);
?>
$stripped_xml can now take advantage of running XPath queries on it for the NULL namespace.
DOMElement::getElementsByTagNameNS
(PHP 5)
DOMElement::getElementsByTagNameNS — 名前空間 URI とローカル名から要素を取得する
説明
この関数は、指定した名前 localName および namespaceURI のタグを持つ すべての子孫要素を取得します。
パラメータ
- namespaceURI
-
名前空間 URI。
- localName
-
ローカル名。ツリー内のすべての要素を返すには * を使用します。
返り値
この関数は、一致したすべての要素からなる DOMNodeList クラスのインスタンスを返します。 各要素は、ツリー内の探索時に見つかった順で並べられます。
DOMElement::getElementsByTagNameNS
spam at chovy dot com
04-Jun-2009 09:36
04-Jun-2009 09:36
