simplexml并使用相对路径

时间:2011-04-02 06:59:37

标签: php xml 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>

我正在寻找这些价值观:<publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk> <publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>

现在可行:

$lijst = $results->soapBody->searchUndertakingResponse->searchUndertakingResult->publicatieLijst->publicatieKenmerk;
    foreach ($lijst AS $kenmerk) {
        echo $kenmerk."<BR>";
    }

但我不想使用它,因为我需要灵活处理其他结果。而且不能依赖 searchUndertakingResponse-&GT; searchUndertakingResult

所以我希望用xpath来实现,但这不起作用:

 $lijst = $results->xpath('//publicatieKenmerk');
    foreach($lijst as $kenmerk) {
        echo $kenmerk."<br />";
    }

但我认为亲戚也会工作......任何想法?

2 个答案:

答案 0 :(得分:2)

您会注意到您的<publicatieLijst>节点设置了默认命名空间:xmlns="http://www.rechtspraak.nl/namespaces/inspubber01"这意味着<publicatieKenmerk>存在该命名空间。这就是为什么//publicatieKenmerk找不到它,你必须在正确的命名空间中搜索。

为此,您可以使用自己的前缀注册命名空间,并在以下XPath查询中使用该前缀,如下所示:

$soapEnvelope = simplexml_load_string($xml);

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

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

答案 1 :(得分:1)

你能尝试改变他们循环的方式吗?

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