PHP 7 DomDocument xpath 查询父参数

PHP 7 DomDocument xpath query parent parameter

我尝试使用 eBaysvc.xsd 不仅要验证 xml,还要构建 xml 本身:
因此,传递为 $API

AddDispute

我想检索这样的东西xml:

<?xml version="1.0" encoding="utf-8"?>
<AddDisputeRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <DisputeExplanation> Tokens </DisputeExplanation>
  <DisputeReason> Tokens </DisputeReason>
  <ItemID> string </ItemID>
  <TransactionID> string </TransactionID>
  <OrderLineItemID> string </OrderLineItemID>
  <Version> string </Version>
  <RequesterCredentials>
    <eBayAuthToken> string </eBayAuthToken>
  </RequesterCredentials>
  <WarningLevel> Tokens </WarningLevel>
</AddDisputeRequest>

我使用 xpath->query 构建了一个脚本,它应该根据多个参数过滤元素,到目前为止我得到了这个:

$Nodes = $xpath->query('//xs:element[(xs:annotation/xs:appinfo/ebl:CallInfo/ebl:CallName/text()="'.$API.'" or xs:annotation/xs:appinfo/ebl:CallInfo/ebl:AllCalls) and (xs:annotation/xs:appinfo/ebl:CallInfo/ebl:RequiredInput/text()="Yes" or xs:annotation/xs:appinfo/ebl:CallInfo/ebl:RequiredInput/text()="Conditionally")]')

但我需要添加一个进一步的参数,该参数与元素无关,但与他的祖先有关:

假设元素带有 name="ItemID",我们需要向查询中添加类似
的内容 and element:parent:parent:parent:parent[@name="'.$API.'Type"]

因为我们有这个定义:

<xs:complexType name="AddDisputeRequestType">
    <xs:annotation>
        <xs:documentation>
            Enables a buyer and seller in an order relationship to
            send messages to each other's My Messages Inboxes.
        </xs:documentation>
        <xs:appinfo>
            <RelatedCalls>
                AddMemberMessagesAAQToBidder, AddMemberMessageRTQ
            </RelatedCalls>
        </xs:appinfo>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="ns:AbstractRequestType">
            <xs:sequence>
                <xs:element name="ItemID" type="ns:ItemIDType" minOccurs="0">
                    ...
                </xs:element>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

但不知道如何实现:有人可以提出解决方案

PS:是否有更简单的方法来检查 RequiredInput

中的 "Yes|Conditionally"

要访问某个节点的祖先,您可以使用如下表达式:

//xs:element[ancestor::*[@name="AddDisputeRequestType"]]

此表达式使用 xpath 祖先轴将 select 作为 xs:element 祖先的所有(和任何)元素满足关于 name 属性的谓词。您可能想用一些更具体的元素名称替换星号。

关于 axes 的一些信息可以在这里找到 https://developer.mozilla.org/en-US/docs/Web/XPath/Axes and here https://www.w3.org/TR/xpath-10/#axes

请注意,如果多个元素与表达式匹配,则可能需要额外注意。这是 http://xsltransform.net/gVAjbTj

的一个简短示例

更新:

is there a like or contains operand

我想你可能会使用 xpath 函数 contains,例如:

//*[contains(name(), 'target')]

select 名称中包含 "target" 子字符串的所有元素。

这个结构几乎可以用来检查文本内容和属性名称,我已经更新了一个 fiddle 一点,你可能想检查它的样本和进一步的摆弄:http://xsltransform.net/gVAjbTj/2

当然,您可以自由组合执行任何特定任务所需的任何内容,例如 "find all elements with this name that have some descendant with that name or any descendant with an attribute named whatever"。

希望对您有所帮助)