【问题标题】:How to get root or specified node by name via xpath?如何通过 xpath 按名称获取根节点或指定节点?
【发布时间】:2016-10-07 13:16:06
【问题描述】:

需要获取一些由名称指定的节点。例如它应该如何查找节点“命令”或根节点?

这是我写的

        $xpath = "/ns0:StandardBusinessDocument/eanucc:message/eanucc:transaction/command";
        $xpath .= "/eanucc:documentCommand/documentCommandOperand/ns1:catalogueItemNotification/catalogueItem";
        $xpath .= "/catalogueItemChildItemLink/catalogueItem/catalogueItemChildItemLink/catalogueItem/tradeItem";
        $xpath .= "/tradeItemIdentification/gtin[.='$gtin']/";

在哪里$gtin = 00000075032814

这就是我得到的

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 00000075032814
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 00000075032814
        )

)

UPD:事情是我可以得到这样的节点

"[catalogueItem/tradeItem/tradeItemIdentification/gtin[.='$gtin']]"

但如果我要往上走 - 还有两个节点“catalogueItem”,我会遇到 xpath 错误。

【问题讨论】:

标签: php xml xpath


【解决方案1】:

根节点:/*

节点名称://*[local-name() = 'command']//command(假设它们位于正确的 XML 命名空间中)

https://www.w3.org/TR/xpath/#function-local-name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2023-03-26
    相关资源
    最近更新 更多