我正在使用PHP解析XML,并希望使用XPATH根据系统和前值条件返回一个惟一的区段值。
XML:
<?xml version="1.0" encoding="utf-8"?>
<language>
<phrase section="Url" system="0" front="1" data="_URL_M1">invoice</phrase>
<phrase section="Url" system="0" front="1" data="_URL_M2">view</phrase>
<phrase section="Invoice" system="1" front="0" data="_MOD_IM1">System1</phrase>
<phrase section="Invoice" system="1" front="0" data="_MOD_IM2">System2</phrase>
<phrase section="Invoice" system="0" front="1" data="_MOD_IM3">Front1</phrase>
<phrase section="Invoice" system="0" front="1" data="_MOD_IM4">Front2</phrase>
<phrase section="Invoice" system="1" front="1" data="_MOD_IM5">System and Front</phrase>
</language>所以我有部分Url,它只在前面= 1的地方显示,而部分Invoice显示在前面=1的地方,前面和系统(两者)=1的地方。需要清楚地显示部分。
PHP:
$xmlel = simplexml_load_file(BASEPATH . self::langdir . $flag . "/" . $filename . ".xml");
$query_system = '/language/phrase[@system="1" and @front="0" and not(@section = preceding-sibling::phrase/@section)]/@section';
$query_front = '/language/phrase[@system="0" and @front="1" and not(@section = preceding-sibling::phrase/@section)]/@section';
$query_both = '/language/phrase[@system="1" and @front="1" and not(@section = preceding-sibling::phrase/@section)]/@section';使用当前的代码,我得到了如下结果:
系统:$xmlel->xpath($query_system)
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Invoice
)
)
)正面:$xmlel->xpath($query_front)
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Url
)
)
)两者:$xmlel->xpath($query_both)
Array
(
)我想要实现的目标:
系统:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Invoice
)
)
)前面:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Url
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Invoice
)
)
)两者:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[section] => Invoice
)
)
)如何获得不同的section
其中:system = 1,
其中front =1
其中system和front =1
发布于 2018-03-27 19:01:44
好的,所以我得出的结论是,我也需要前置-兄弟:
$query_system = '/language/phrase[@system="1" and @front="0" and not(@section = preceding-sibling::*[@system="1" and @front="0"]/@section)]/@section';
$query_front = '/language/phrase[@system="0" and @front="1" and not(@section = preceding-sibling::*[@system="0" and @front="1"]/@section)]/@section';
$query_both = '/language/phrase[@system="1" and @front="1" and not(@section = preceding-sibling::*[@system="1" and @front="1"]/@section)]/@section';https://stackoverflow.com/questions/49490834
复制相似问题