我在通过以下方式加载的XML文件中提供了一个票证列表:
$xml = simplexml_load_file($xmlFile);票的记录中包含两个字段,如下所示:
<tickets>
<ticket>
<custom-fields>
<custom-field type="List" name="public" id="1392571">Public</custom-field>
<custom-field type="List" name="Typ" id="1150963">Change Request</custom-field>
</custom-fields>
</ticket>
<ticket>
<custom-fields>
<custom-field type="List" name="public" id="1392571">Non-Public</custom-field>
<custom-field type="List" name="Typ" id="1150963">Change Request</custom-field>
</custom-fields>
</ticket>
</tickets>现在,我只想展示公共门票。但是我想展示一下这张票的类型。
所以我需要检查一下
如果第一个自定义字段具有属性“name=”“Public”和值“Public”
的同级字段的值。
我已经可以检查的第一部分是:
if($ticket->{'custom-fields'}[0]->{'custom-field'}[0]) == "PUBLIC"){
//now populate the Value from the second field
}问:我现在如何从兄弟姐妹中提取值?
或者更好。是否有可能得到具有特殊值的字段的值?我在XPATH中使用的Liek:
/tickets/ticket/custom-fields/custom-field[@name="public"]/../custom-field[@name="Typ"]谢谢!
发布于 2020-10-12 10:17:05
您完全可以使用XPath查询:'/tickets/ticket/custom-fields[custom-field="Public"]/custom-field[@name="Typ"]'来完成这一任务。
完整的例子:
$xmlString = '
<tickets>
<ticket>
<custom-fields>
<custom-field type="List" name="public" id="1392571">Public</custom-field>
<custom-field type="List" name="Typ" id="1150963">Change Request (public)</custom-field>
</custom-fields>
</ticket>
<ticket>
<custom-fields>
<custom-field type="List" name="public" id="1392571">Non-Public</custom-field>
<custom-field type="List" name="Typ" id="1150963">Change Request (non-public)</custom-field>
</custom-fields>
</ticket>
</tickets>';
$xmlDoc = new SimpleXMLElement($xmlString);
$output = $xmlDoc->xpath('/tickets/ticket/custom-fields[custom-field="Public"]/*[@name="public"]/../custom-field[@name="Typ"]');给出
array(1) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(3) {
["type"]=>
string(4) "List"
["name"]=>
string(3) "Typ"
["id"]=>
string(7) "1150963"
}
[0]=>
string(15) "Change Request (public)"
}
}在PHP7.2上测试
https://stackoverflow.com/questions/64315237
复制相似问题