PHP Xpath返回零匹配

Considering this XML

<?xml version="1.0" encoding="UTF-8"?> <Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0"> <Subject> <NameID>801234567890</NameID> </Subject> .... </Assertion>

PHP

$dom = new DOMDocument(); $ret = $dom->loadXML($data); $xp = new DOMXPath($dom); $node_list = $xp->query('/Assertion');

$node_list->length return 0 element. I want to extract the DOMElement but somehow it didn't work.

As stated on the comments by Sami Kuhmonen you may need to register the namespaces, here is an example:

<?php
$string= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0">
   <Subject>
      <NameID>801234567890</NameID>
   </Subject>
</Assertion>
XML;

$dom = new DOMDocument();
$dom->loadXML($string);
$xp = new DOMXPath($dom);

// registering the namespaces
$xp->registerNamespace('a','urn:oasis:names:tc:SAML:2.0:assertion');
$xp->registerNamespace('b','http://www.w3.org/2001/XMLSchema-instance');

// using the prefix of the registered namespace in the xpath expression
$node_list = $xp->query('/a:Assertion');

print $node_list->length
?>