PHP xPath 解析 SOAP

PHP xPath parsing SOAP

PHP xPath 解析 SOAP

我有一个 SOAP 请求,其主体类似于下面的代码。 (我已经删除了不相关的节点)。我只想解析对 NumType 为 HI 的 NumInfo 节点的请求。我需要获取具有 NumType = HI 的 NumInfo 节点的节点的 Type、Num、Desc、Quantity 和 PriceInfo 值。

像这样使用 xpath 获取节点:

$xml = simplexml_load_file('RequestExample.xml');
$xml->registerXPathNamespace("bms", "http://www.cieca.com/BMS");
$xml->xpath("//bms:Selected[contains(., 'HI')]")

将获得包含 HI 的选定节点。 xpath returns 一组 SimpleXML 对象,对吗?

如何获取与该 NumInfo 节点对应的父节点的其他信息?另外,通过注册命名空间,我不应该能够在我的 xpath 查询中省略它们吗?

<soap:Body>
    <bms:ProcurementAddRq>
        <bms:ProcurementFolder>
            <bms:ProcurementInfo>
                <bms:ProcurementList>
                    <bms:Procurement>
                        <bms:Selected>
                            <bms:NumInfo>
                                <bms:NumType>OE</bms:NumType>
                                <bms:Num>04715SNAA90ZZ</bms:Num>
                            </bms:NumInfo>
                            <bms:NumInfo>
                                <bms:NumType>HI</bms:NumType>
                                <bms:Num>187-01436</bms:Num>
                            </bms:NumInfo>
                            <bms:NumInfo>
                                <bms:NumType>HPT</bms:NumType>
                                <bms:Num>187</bms:Num>
                            </bms:NumInfo>
                            <bms:Type>PAN</bms:Type>
                            <bms:Desc>Cover</bms:Desc>
                            <bms:Quantity>1</bms:Quantity>
                            <bms:PriceInfo>
                                <bms:UnitListPrice>328.42</bms:UnitListPrice>
                                <bms:UnitNetPrice>328.42</bms:UnitNetPrice>
                            </bms:PriceInfo>
                        </bms:Selected>
                    </bms:Procurement>
                    <bms:Procurement>
                        <bms:Selected>
                            <bms:NumInfo>
                                <bms:NumType>OE</bms:NumType>
                                <bms:Num>71570SNAA00</bms:Num>
                            </bms:NumInfo>
                            <bms:Type>PAN</bms:Type>
                            <bms:Desc>Abs</bms:Desc>
                            <bms:Quantity>1</bms:Quantity>
                            <bms:PriceInfo>
                                <bms:UnitListPrice>49.80</bms:UnitListPrice>
                                <bms:UnitNetPrice>49.80</bms:UnitNetPrice>
                            </bms:PriceInfo>
                        </bms:Selected>
                    </bms:Procurement>
                    <bms:Procurement>
                        <bms:Selected>
                            <bms:NumInfo>
                                <bms:NumType>OE</bms:NumType>
                                <bms:Num>66100SNEA00ZZ</bms:Num>
                            </bms:NumInfo>
                            <bms:Type>PAN</bms:Type>
                            <bms:Desc>Panel</bms:Desc>
                            <bms:Quantity>1</bms:Quantity>
                            <bms:PriceInfo>
                                <bms:UnitListPrice>355.83</bms:UnitListPrice>
                                <bms:UnitNetPrice>355.83</bms:UnitNetPrice>
                            </bms:PriceInfo>
                        </bms:Selected>
                    <bms:Procurement>
                        <bms:Selected>
                            <bms:NumInfo>
                                <bms:NumType>OE</bms:NumType>
                                <bms:Num>04655SNE305ZZ</bms:Num>
                            </bms:NumInfo>
                            <bms:Type>PAP</bms:Type>
                            <bms:Desc>Pan</bms:Desc>
                            <bms:Quantity>1</bms:Quantity>
                            <bms:PriceInfo>
                                <bms:UnitListPrice>994.13</bms:UnitListPrice>
                                <bms:UnitNetPrice>994.13</bms:UnitNetPrice>
                            </bms:PriceInfo>
                        </bms:Selected>
                </bms:ProcurementList>
            </bms:ProcurementInfo>
        </bms:ProcurementFolder>
    </bms:ProcurementAddRq>
</soap:Body>

您应该能够使用...

获取数据
$selected = $xml->xpath("//bms:Selected[bms:NumInfo/bms:NumType='HI']")[0];
echo (string)$selected->children("bms",true)->Desc;

注意 XPath 函数末尾的 [0],正如您所说,因为它 returns 匹配的节点列表。通常你会在 foreach() 中使用它,但如果只有一个,那么你可以这样缩短它。

当你获取一个节点时,这实际上是一个 SimpleXMLElement,使用 (string) 将值转换为字符串,允许你更灵活地使用它作为值(echo 无论如何,但这只是为了说明原理)

->children("bms",true) 位 returns 命名空间中的所有子节点(使用前缀),这允许您在没有前缀的情况下使用 ->Desc

即使您注册了名称空间,您仍然需要在您的 XPath 语句中使用它,这允许您混合使用名称空间和元素。您可能具有相同的命名元素但命名空间不同,因此这可确保您引用正确的元素。