开发者

simplexml and using relative path

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>
     开发者_C百科           <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?


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:

$soapEnvelope = simplexml_load_string($xml);

$soapEnvelope->registerXPathNamespace(
    'inspubber01',
    'http://www.rechtspraak.nl/namespaces/inspubber01'
);

foreach ($soapEnvelope->xpath('//inspubber01:publicatieKenmerk') as $publicatieKenmerk)
{
    echo $publicatieKenmerk, "\n";
}


can you try to change on they way it loops?

    while(list( , $node) = each($lijst)) {
    echo 'kenmerk: ',$node,"\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜