php xpath 基于多个属性和父属性检索属性值 [英] php xpath retrieving attribute-values based on multiple attributes and parent-attributes

查看:25
本文介绍了php xpath 基于多个属性和父属性检索属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 xml 中选择节点,条件见下文.我使用的是 simplexml,所以 xpath 必须是 1.0.

I need to select nodes from xml, conditions see below. I am using simplexml, so the xpath has to be 1.0.

XML 片段:

<scales>
    <scale id="1" gender="*" age="*">
        <d scid="hi" raw="10" t="76" />
        <d scid="pn" raw="12" t="80" />
    </scale>
    <scale id="2" gender="m" age="*">
        <d scid="hi" raw="8" t="79" />
        <d scid="pn" raw="2" t="50" />
    </scale>
    <scale id="3" gender="*" age="19-39">
        <d scid="hi" raw="0" t="48" />
        <d scid="pn" raw="10" t="49" />
    </scale>
</scales>

现在,我想选择 <d> 节点的 t-Attribute,该节点具有...

Now, I want to select the t-Attribute of a <d> node that has...

 raw="10" AND scid="hi"

 $result=$xml->xpath('//d[@scid="hi"][@raw="10"]/@t');

它的父节点有...

(gender="*" OR gender="m") AND (age="*" OR age="39-59")

$result=$xml->xpath('//scale[@gender="*" or @gender="m"][@age="*" or @age="39-59"]');

我想从我的 simplexml 对象 $xml 中得到 1 个 xpath 语句.

I'd like to get this with 1 xpath-statement from my simplexml-object $xml.

推荐答案

只需结合您的两个 XPath 查询...

Just combine your two XPath query...

现场演示

$str=<<<XML
<scales>
    <scale id="1" gender="*" age="*">
        <d scid="hi" raw="10" t="76" />
        <d scid="pn" raw="12" t="80" />
    </scale>
    <scale id="2" gender="m" age="*">
        <d scid="hi" raw="8" t="79" />
        <d scid="pn" raw="2" t="50" />
    </scale>
    <scale id="3" gender="*" age="19-39">
        <d scid="hi" raw="0" t="48" />
        <d scid="pn" raw="10" t="49" />
    </scale>
</scales>
XML;
$xml=simplexml_load_string($str);
foreach($xml->xpath('//scale[@gender="*" or @gender="m"][@age="*" or @age="39-59"]/d[@scid="hi"][@raw="10"]/@t') as $t)
{
    echo $t;
}

输出76.

这篇关于php xpath 基于多个属性和父属性检索属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆