首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP和XPath按条件获取distinct属性

PHP和XPath按条件获取distinct属性
EN

Stack Overflow用户
提问于 2018-03-26 19:52:58
回答 1查看 89关注 0票数 0

我正在使用PHP解析XML,并希望使用XPATH根据系统和前值条件返回一个惟一的区段值。

XML:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
$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)

代码语言:javascript
复制
Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Invoice
            )

    )
)

正面:$xmlel->xpath($query_front)

代码语言:javascript
复制
Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Url
            )

    )
)

两者:$xmlel->xpath($query_both)

代码语言:javascript
复制
Array
(
)

我想要实现的目标:

系统:

代码语言:javascript
复制
Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Invoice
            )

    )
)

前面:

代码语言:javascript
复制
Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Url
            )

    )
)
[1] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Invoice
            )

    )
)

两者:

代码语言:javascript
复制
Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [section] => Invoice
            )

    )
)

如何获得不同的section

其中:system = 1,

其中front =1

其中systemfront =1

EN

回答 1

Stack Overflow用户

发布于 2018-03-27 19:01:44

好的,所以我得出的结论是,我也需要前置-兄弟:

代码语言:javascript
复制
$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';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49490834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档