【问题标题】:PHP 7.2 xml domdocument get nodevalue of given childnode value of sibling having attribute equal toPHP 7.2 xml domdocument获取属性等于的兄弟节点的给定子节点值的节点值
【发布时间】:2019-01-02 17:07:57
【问题描述】:

我有以下xml

<ReviseInventoryStatusResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2019-01-02T15:42:31.495Z</Timestamp>
  <Ack>Warning</Ack>
  <Errors>
    <ShortMessage>Requested StartPrice and Quantity revision is redundant.</ShortMessage>
    <LongMessage>The existing price and quantity values are identical to those specified in the request and, therefore, have not been modified.</LongMessage>
    <ErrorCode>21917091</ErrorCode>
    <SeverityCode>Warning</SeverityCode>
    <ErrorParameters ParamID="ItemID">
      <Value>770386906435</Value>
    </ErrorParameters>
    <ErrorParameters ParamID="SKU">
      <Value/>
    </ErrorParameters>
    <ErrorClassification>RequestError</ErrorClassification>
  </Errors>
  <Errors>
    <ShortMessage>Requested StartPrice and Quantity revision is redundant.</ShortMessage>
    <LongMessage>The existing price and quantity values are identical to those specified in the request and, therefore, have not been modified.</LongMessage>
    <ErrorCode>21917091</ErrorCode>
    <SeverityCode>Warning</SeverityCode>
    <ErrorParameters ParamID="ItemID">
      <Value>770386906436</Value>
    </ErrorParameters>
    <ErrorParameters ParamID="SKU">
      <Value/>
    </ErrorParameters>
    <ErrorClassification>RequestError</ErrorClassification>
  </Errors>
  ...
  </ReviseInventoryStatusResponse>

我需要获取在 ErrorParameters/Value 中 ParamID="ItemID" 中找到的给定 ItemID 的 ShortMessage

我已经能够检查是否存在具有给定值的节点:

$xmlr = new DOMDocument();
$xmlr->load('ReviseInventoryStatusResponse_error.xml');
$xpath = new DOMXPath($xmlr);
$xpath->registerNamespace('e', 'urn:ebay:apis:eBLBaseComponents');
$nodeExists=($xpath->query('//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]')->length>0)?1:0;

然后我尝试通过

获取它的 ShortMessage
$xpath->query('//e:Errors[e:ErrorParameters@ParamID="ItemID"]/e:Value[.="770386906435"]/e:ShortMessage')

但由于没有找到组合过滤器的正确方法,因此得到了无效的谓词。

能否请教正确的方法?

谢谢

【问题讨论】:

    标签: php xml xpath domdocument


    【解决方案1】:

    这里有几种方法

    1. 使用嵌套条件选择Errors元素节点:
      //e:Errors[e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]]/e:ShortMessage
    2. 选择Value 元素并使用parent
      //e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/parent::*/parent::*/e:ShortMessage
    3. 选择Value 元素并使用ancestor
      //e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/ancestor::e:Errors/e:ShortMessage

    如果您使用DOMXpath::evaluate(),您可以使用count()string() 直接获取标量值。您甚至可能不需要检查节点是否存在,因为在这种情况下结果将是一个空字符串。

    $document = new DOMDocument();
    $document->loadXML($xml);
    $xpath = new DOMXPath($document);
    $xpath->registerNamespace('e', 'urn:ebay:apis:eBLBaseComponents');
    $nodeExists = $xpath->evaluate(
      'count(//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]) > 0'
    );
    
    var_dump($nodeExists);
    
    $shortMessage = $xpath->evaluate(
      'string(//e:Errors[e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]]/e:ShortMessage)'
    );
    var_dump($shortMessage);
    
    $shortMessage = $xpath->evaluate(
      'string(//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/parent::*/parent::*/e:ShortMessage)'
    );
    var_dump($shortMessage);
    
    $shortMessage = $xpath->evaluate(
      'string(//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/ancestor::e:Errors/e:ShortMessage)'
    );
    var_dump($shortMessage);
    

    输出:

    bool(true) 
    string(56) "Requested StartPrice and Quantity revision is redundant." 
    string(56) "Requested StartPrice and Quantity revision is redundant." 
    string(56) "Requested StartPrice and Quantity revision is redundant."
    

    【讨论】:

      【解决方案2】:

      如果你使用 XPath 来获取短消息,一个快速检查是否有消息的方法是只检查查询是否有任何结果......

      $smgs = $xpath->query('//e:Errors[e:ErrorParameters[@ParamID="ItemID" and e:Value="7720386906435"]]/e:ShortMessage');
      if ( count($smgs) > 0 ) {
          echo $smgs[0]->nodeValue;
      }
      else {
          echo "does not exist";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 2011-10-13
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多