simplexml 并使用相对路径

发布于 2024-10-30 12:28:36 字数 2881 浏览 1 评论 0原文

我试图获取 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>

我正在寻找这些值: sgr.10.787.F.1300.1 .10utr.10.585.F.1300.1.10

现在这有效:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-11-06 12:28:36

您会注意到您的 节点有一个默认命名空间集:xmlns="http://www.rechtspraak.nl/namespaces/inspubber01" 这意味着 存在该命名空间。这就是为什么 //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";
}

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";
}
寂寞花火° 2024-11-06 12:28:36

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

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

can you try to change on they way it loops?

    while(list( , $node) = each($lijst)) {
    echo 'kenmerk: ',$node,"\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文