如何通过xpath按名称获取根节点或指定节点


How to get root or specified node by name via xpath?

需要按名称指定一些节点。例如,它应该如何查找节点'命令'或根节点?

这是我写的

        $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']]"

但是如果我往上面走-有两个节点' catalogeitem ',我得到一个xpath错误

根节点:/*

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

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