$xml = ''; $dom = new DOMDocument(); $dom->loadXML($xml); $title = $dom->getElementsByTagName('title')[0]; if($title->hasAttributeNS('http://www.myns.com', 'id')) { echo $title->getAttributeNS('http://www.myns.com', 'id'); } The Great Gatsby
$xml = 'Brief Description: The above examples demonstrate the use of hasAttributeNS method to check whether the 'title' element has an attribute named 'id' with the 'http://www.myns.com' namespace. If the attribute exists, its value is retrieved using getAttributeNS method. If the attribute does not exist, then the code handles it accordingly. The library package used here is the PHP DOM extension which is a built-in library in PHP.'; $dom = new DOMDocument(); $dom->loadXML($xml); $title = $dom->getElementsByTagName('title')[0]; if($title->hasAttributeNS('http://www.myns.com', 'id')) { echo $title->getAttributeNS('http://www.myns.com', 'id'); } else { echo "Attribute not found."; } War and Peace