simplexml 并使用相对路径
我试图获取 xml 响应中的一部分,而不遵循整个路径。现在我知道 xpath 具有搜索能力,但不知何故我不明白它... :(
我正在解析的 XML 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/cir01/searchUndertakingResponse</wsaAction>
<wsaMessageID>urn:uuid:11f7d4cd-2280-4298-85eb-dadf5bd743f1</wsaMessageID>
<wsaRelatesTo>urn:uuid:59630fbd-b990-4020-9c1c-822c58186d96</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-df25f141-fed2-47ed-967e-93cd04d1c8f2">
<wsuCreated>2011-04-02T06:52:52Z</wsuCreated>
<wsuExpires>2011-04-02T06:57:52Z</wsuExpires>
</wsuTimestamp>
</wsseSecurity>
</envHeader>
<soapBody>
<searchUndertakingResponse xmlns="http://www.rechtspraak.nl/namespaces/cir01"><searchUndertakingResult>
<publicatieLijst xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" extractiedatum="2011-04-02T08:52:51" xmlns="http://www.rechtspraak.nl/namespaces/inspubber01">
<publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk>
<publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>
</publicatieLijst>
</searchUndertakingResult>
</searchUndertakingResponse>
</soapBody>
</soapEnvelope>
我正在寻找这些值:
现在这有效:
$lijst = $results->soapBody->searchUndertakingResponse->searchUndertakingResult->publicatieLijst->publicatieKenmerk;
foreach ($lijst AS $kenmerk) {
echo $kenmerk."<BR>";
}
但我不想使用它,因为我需要灵活地处理其他结果。不能依赖 searchUndertakeResponse->searchUndertakeResult
所以我希望使用 xpath 到达那里,但这不起作用:
$lijst = $results->xpath('//publicatieKenmerk');
foreach($lijst as $kenmerk) {
echo $kenmerk."<br />";
}
但我认为relative 也可以工作......有什么想法吗?
I am trying to get to a part in an xml response, without following the whole path. Now I know that xpath has search abilities, but somehow I dont understand it... :(
The XML i am parsing is this:
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/cir01/searchUndertakingResponse</wsaAction>
<wsaMessageID>urn:uuid:11f7d4cd-2280-4298-85eb-dadf5bd743f1</wsaMessageID>
<wsaRelatesTo>urn:uuid:59630fbd-b990-4020-9c1c-822c58186d96</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-df25f141-fed2-47ed-967e-93cd04d1c8f2">
<wsuCreated>2011-04-02T06:52:52Z</wsuCreated>
<wsuExpires>2011-04-02T06:57:52Z</wsuExpires>
</wsuTimestamp>
</wsseSecurity>
</envHeader>
<soapBody>
<searchUndertakingResponse xmlns="http://www.rechtspraak.nl/namespaces/cir01"><searchUndertakingResult>
<publicatieLijst xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" extractiedatum="2011-04-02T08:52:51" xmlns="http://www.rechtspraak.nl/namespaces/inspubber01">
<publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk>
<publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>
</publicatieLijst>
</searchUndertakingResult>
</searchUndertakingResponse>
</soapBody>
</soapEnvelope>
And I am looking for these values: <publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk> <publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>
Now this works:
$lijst = $results->soapBody->searchUndertakingResponse->searchUndertakingResult->publicatieLijst->publicatieKenmerk;
foreach ($lijst AS $kenmerk) {
echo $kenmerk."<BR>";
}
But I dont want to use this, as I need to be flexible for other results. and cannot rely on
searchUndertakingResponse->searchUndertakingResult
So I was hoping to use xpath to get there, but this doesnt work:
$lijst = $results->xpath('//publicatieKenmerk');
foreach($lijst as $kenmerk) {
echo $kenmerk."<br />";
}
But I thought relative would work as well... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会注意到您的
节点有一个默认命名空间集:xmlns="http://www.rechtspraak.nl/namespaces/inspubber01"
这意味着
存在该命名空间。这就是为什么//publicatieKenmerk
找不到它,您必须在正确的命名空间中搜索。为此,您可以使用自己的前缀注册命名空间,并在以下 XPath 查询中使用该前缀,如下所示:
You'll notice that your
<publicatieLijst>
node has a default namespace set:xmlns="http://www.rechtspraak.nl/namespaces/inspubber01"
which means that<publicatieKenmerk>
exists that namespace. That's why//publicatieKenmerk
won't find it, you have to search in the right namespace.For that, you can register the namespace with your own prefix and use that prefix in the following XPath query, like this:
你能尝试改变它的循环方式吗?
can you try to change on they way it loops?