我有一个XML字符串,下面的代码段:
<?xml version="1.0"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendPurchases xmlns="urn:services.insurance">
<Partner>
<UserID>MyCompany</UserID>
<Password>ABC123</Password>
</Partner>
<PurchasesRequest>
<Total>100</Total>
</PurchasesRequest>
</SendPurchases>
</soap:Body>
</soap:Envelope>
我正在将XML转换为DOMDocument以使操作“更容易”:
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
然后我想操纵某些值,最好使用它们的路径:
$elements = $xpath->query('/soap:Envelope/soap:Body/SendPurchases/Partner/UserID');
但是上面的查询没有返回任何结果。
事实上,当我遍历文档中的所有元素时:
foreach ($doc->getElementsByTagName('*') as $node) {
echo $node->getNodePath() . "\n";
}
它返回如下内容:
/soap:Envelope/soap:Body
/soap:Envelope/soap:Body/*
/soap:Envelope/soap:Body/*/*[1]
/soap:Envelope/soap:Body/*/*[1]/*[1]
/soap:Envelope/soap:Body/*/*[1]/*[2]
/soap:Envelope/soap:Body/*/*[2]
/soap:Envelope/soap:Body/*/*[2]/*[1]
正如您所看到的,<soap:Body>
内的所有元素都被替换为星号和索引而不是元素名称。
沿着该路径查询有效,但对我来说维护并不容易,我更倾向于使用元素名称。
答案 0 :(得分:2)
感谢@Dekel的提示。我需要注册两个名称空间,然后在查询时指定名称空间:
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xpath->registerNamespace('insr', 'urn:services.insurance');
$elements = $xpath->query('/soap:Envelope/soap:Body/insr:SendPurchases/insr:Partner/insr:UserID');
现在它正在运作。
答案 1 :(得分:0)
另一种选择是use local-name()
:
$xml = '<?xml version="1.0"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendPurchases xmlns="urn:services.insurance">
<Partner>
<UserID>MyCompany</UserID>
<Password>ABC123</Password>
</Partner>
<PurchasesRequest>
<Total>100</Total>
</PurchasesRequest>
</SendPurchases>
</soap:Body>
</soap:Envelope>';
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//*[local-name()='UserID']");
var_dump($elements->item(0)->nodeValue);
// string(9) "MyCompany"
答案 2 :(得分:0)
我快速而肮脏的解决方案:
$path = $oNode->getNodePath(); #eg. /*/*[2]/*
while ($oNode) {
$path = preg_replace('~(.*/)[*]~', '$1'.$oNode->localName, $path, 1);
$oNode = $oNode->parentNode;
}
$path; #eg. /root/node[2]/subnode