PHP xpath根据两个属性(编辑)及其父属性的值选择节点


php xpath select nodes based on values of two attributes (edit) AND its parents attributes

xpath 1.0 - 我不得不改变方法,因为XML已经改变。

我需要拉

t-Attribute FROM the <d>-node WHERE its 
    raw-Attribute = 10 
    AND its parent-node's <scale> gender-Attribute = "*" OR "m" 
    AND the age-Attribute = "*" OR "39-59"  
<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>

对于我最初的目标,路人的解决方案就像一个魅力,但我不知道如何解决额外的任务......

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

这是我最初的任务...

我想从 xml 中选择节点,其中两个属性包含特定值。我使用的是simplexml,所以xpath必须是1.0。

XML代码段:

<scales>
    <scale id="1" gender="*" age="*">
        <d>5</d>
        <d>9</d>
    </scale>
    <scale id="2" gender="m" age="*">
        <d>3</d>
        <d>0</d>
    </scale>
    <scale id="3" gender="*" age="19-39">
        <d>12</d>
        <d>19</d>
    </scale>
</scales>

现在,我想选择<scale>所有具有...

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

在我的示例中,id=1 和 id=2 的那些

我知道如何使用 1 个属性来做到这一点,但没有 OR/AND 条件......

$scales=$xml->xpath("//scale[@gender='m']");

尝试

//scale[@gender="*" or @gender="m"][@age="*" or @age="39-59"]

现场演示

$str=<<<XML
<scales>
    <scale id="1" gender="*" age="*">
        <d>5</d>
        <d>9</d>
    </scale>
    <scale id="2" gender="m" age="*">
        <d>3</d>
        <d>0</d>
    </scale>
    <scale id="3" gender="*" age="19-39">
        <d>12</d>
        <d>19</d>
    </scale>
</scales>
XML;
$xml=simplexml_load_string($str);
$result=$xml->xpath('//scale[@gender="*" or @gender="m"][@age="*" or @age="39-59"]');
foreach($result as $node)
{
    echo $node->attributes()->id."'n";
}