我正在使用xpath获取某些节点的值。以下是示例xml:
<Order>
<OrderNumber>2112491630</OrderNumber>
<OrderId>9876543</OrderId>
<OrderDetails>
<OrderDetail>
<SKUData>
<SKUQuestions>
<SKUQuestion>
<Tag>PowerOn</Tag>
<Question>Does the Device Power On?</Question>
<Answer>Yes</Answer>
</SKUQuestion>
<SKUQuestion>
<Tag>OtherIssues</Tag>
<Question>Please describe the issue(s) the device is experiencing in detail</Question>
<Answer>Im a pineapple</Answer>
</SKUQuestion>
</SKUQuestions>
</SKUData>
</OrderDetail>
<OrderDetail>
<SKUData>
<SKUQuestions>
<SKUQuestion>
<Tag>PowerOn</Tag>
<Question>Does the Device Power On?</Question>
<Answer>Yes</Answer>
</SKUQuestion>
<SKUQuestion>
<Tag>OtherIssues</Tag>
<Question>Please describe the issue(s) the device is experiencing in detail</Question>
<Answer>Im a banana</Answer>
</SKUQuestion>
</SKUQuestions>
</SKUData>
</OrderDetail>
</OrderDetails>
这是php:
<?php
foreach($files as $filename) {
$xml=simplexml_load_file($filename) or die("Error: Cannot create object");
foreach($xml->OrderDetails->OrderDetail as $orderDetail) {
$qOtherIssues = $orderDetail->xpath('/SKUData/SKUQuestions/SKUQuestion[Tag="OtherIssues"]');
foreach($qOtherIssues as $a) {
$otherIssues = str_replace("'", "\'", $a->Answer);
}
$qPowerOn = $orderDetail->xpath('/SKUData/SKUQuestions/SKUQuestion[Tag="PowerOn"]');
foreach($qPowerOn as $a) {
$powerOn = str_replace("'", "\'", $a->Answer);
}
}
}
} ?&GT;
使用绝对xpath设置,它不会为问题组返回任何内容。
当我使用通配符(//)作为路径时,它只返回最后一组问题(我是香蕉等)。我想在每个OrderDetail标记中返回一组问题。
我迷路了。