2016-10-18 3 views
0

このXMLを考慮PHPのXpathリターンゼロ一致

<?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>8</NameID> </Subject> .... </Assertion>

PHP

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

$node_list->length戻り0素子。私はDOMElementを抽出したいが、何とか動作しなかった。

+1

それが適切に実装を行うのと仮定すると、どのような要素 –

+0

を探していたときに名前空間を指定する必要があります名前空間の?あなたは例を挙げて答えることができますか?ランダムな名前空間を置くことはできますか? –

答えて

1

あなたはここに、名前空間を登録する必要がありサミKuhmonenのコメントで述べたようには例です:

<?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>8</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 
?> 
関連する問題