The DOM Level 2 specifies two methods for searching the DOM tree for elements.
getElementsByTagNameNS
The getElementsByTagNameNS method is identical in
function to the getElementsByTagName method, but you must pass the namespace
URI and local name of the elements you are looking for. Like getElementsByTagName,
a nodelist of found elements is returned, which you can iterate through:
$myNodeList =& $xmldoc->getElementsByTagNameNS('http://www.mosforge.com', 'params');
if ($myNodeList != null) {
echo "getElementsByTagNameNS found elements:<br />";
$total = $myNodeList->getLength();
for ($i = 0; $i < $total; $i++) {
$currItem =& $myNodeList->item($i);
echo $currItem->toNormalizedString(true) . "<br />";
}
}
else {
echo "No elements with the requested tag name found. <br />";
}
Substituting the "*" character for either the namespace URI or local name parameters will match
on all namespace URI or all local names:
//matches all elements in the http://www.mosforge.com namespace
$mosElements =& $xmldoc->getElementsByTagNameNS('http://www.mosforge.com', '*');
//matches all params elements, regardless of namespace
$paramsElements =& $xmldoc->getElementsByTagNameNS('*', 'params');
getElementByID
The getElementByID method is a means of searching the
DOM tree for an element based on an "id" attribute.
Unlike the Javascript DOM method by the same name, this does not simply refer to any attribute named "id".
The XML DOM has a much stricter definition -- the ID attribute must either be defined in a DTD or prefixed by
the reserved XML namespace "xml".
For example:
<myElement xml:id="123" />
Since DOMIT! is non-validating and accordingly does not process DTDs in an intelligent manner,
only the second option is available. If you want to use this method, all "id" attributes in your document
must be prefixed by "xml:". You would then write:
$foundElement =& $xmldoc->getElementByID('123');
However, since it is reasonable to assume that DOMIT! will be used to process XHTML, where a less
rigorous definition of "id" is necessary, the getElementByID method
has a second parameter (isStrict) that will match on any element with an attribute key of "id"
or "ID". For example:
$foundElement =& $xmldoc->getElementByID('456', false);
|