PHP 7.2 xml domdocument 获取给定子节点的节点值具有属性等于的兄弟节点值
PHP 7.2 xml domdocument get nodevalue of given childnode value of sibling having attribute equal to
我有以下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 中找到的给定 ItemID 的 ShortMessage,其中 ParamID="ItemID"
我已经能够检查是否存在具有给定值的节点:
$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')
但是由于没有找到组合过滤器的正确方法,所以得到了预期的无效谓词。
能否请教正确的方法?
谢谢
这里有几种方法
- Select
Errors
元素节点使用嵌套条件:
//e:Errors[e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]]/e:ShortMessage
- Select
Value
元素并使用 parent
轴
//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/parent::*/parent::*/e:ShortMessage
- Select
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."
如果您使用 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";
}
我有以下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 中找到的给定 ItemID 的 ShortMessage,其中 ParamID="ItemID"
我已经能够检查是否存在具有给定值的节点:
$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')
但是由于没有找到组合过滤器的正确方法,所以得到了预期的无效谓词。
能否请教正确的方法?
谢谢
这里有几种方法
- Select
Errors
元素节点使用嵌套条件:
//e:Errors[e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]]/e:ShortMessage
- Select
Value
元素并使用parent
轴
//e:ErrorParameters[@ParamID="ItemID"]/e:Value[. = "770386906435"]/parent::*/parent::*/e:ShortMessage
- Select
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."
如果您使用 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";
}