CakeFest 2024: The Official CakePHP Conference

DOMElement::getElementsByTagNameNS

(PHP 5, PHP 7, PHP 8)

DOMElement::getElementsByTagNameNS名前空間 URI とローカル名から要素を取得する

説明

public DOMElement::getElementsByTagNameNS(?string $namespace, string $localName): DOMNodeList

この関数は、指定した名前 localName および namespace のタグを持つ すべての子孫要素を取得します。

パラメータ

namespace

条件に一致する要素の名前空間 URI。 "*" はすべての名前空間に一致します。 null を渡すと、空の名前空間に一致します。

localName

条件に一致する要素のローカル名。 "*" はすべてのローカル名に一致します。

戻り値

この関数は、一致したすべての要素からなる DOMNodeList クラスのインスタンスを返します。 各要素は、ツリー内の探索時に見つかった順で並べられます。

変更履歴

バージョン 説明
8.0.3 namespace は、nullable になりました。

参考

add a note

User Contributed Notes 1 note

up
2
spam at chovy dot com
14 years ago
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.
To Top