使用Simplexml的Xpath根据PHP中的值过滤节点

时间:2019-07-22 21:23:13

标签: php xml xpath simplexml

我正在研究一种基于某些节点在PHP中的值来过滤它们的方法。我正在尝试返回等于“已资助”状态节点的数量。 我不确定的部分是使用数组(新过滤器($ xml_bio_children)中先前过滤器($ xml_bio_record [0])的结果)。
此代码不返回$ count_c的计数。如何过滤status =“ Funded”?感谢您带来的线索。

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<data>
<record id="1A">
    <congrant>
        <ident>a</ident>
        <status>Not Funded</status>
    </congrant>
    <congrant>
        <ident>b</ident>
        <status>Funded</status>
    </congrant>
    <congrant>
        <ident>c</ident>
        <status/>
    </congrant>
</record>
<record id="1B">
    <congrant>
        <ident>a</ident>
        <status>Not Funded</status>
    </congrant>
    <congrant>
        <ident>b</ident>
        <status>Funded</status>
    </congrant>
    <congrant>
        <ident>c</ident>
        <status/>
    </congrant>
</record>
<record id="1C">
    <congrant>
        <ident>aaa</ident>
        <status>Funded</status>
    </congrant>
    <congrant>
        <ident>bbb</ident>
        <status>Funded</status>
    </congrant>
    <congrant>
        <ident>c</ident>
        <status>Funded</status>
    </congrant>
</record>
</data>

这是PHP:

$url_bio = "test.xml";


$xml_bio = simplexml_load_file($url_bio);

$xml_bio_record=$xml_bio->xpath('/data/record');
$count_a = count($xml_bio_record);
echo '<br>$count_a is...'.$count_a.'<br>';//
foreach($xml_bio_record as $xa){
    echo "Found {$xa->status} <br>";
}

$xml_bio_record=$xml_bio->xpath('//record[@id="1A"]');
$count_b = count($xml_bio_record);
echo '<br>$count_b is...'.$count_b.'<br>';//
foreach($xml_bio_record as $xb){
    echo "Found {$xb->status} <br>";
}



$xml_bio_children=$xml_bio_record[0]->xpath('/congrant[status="Funded"]');

$count_c = count($xml_bio_children);
echo '<br>$count_c is...'.$count_c.'<br>';//
foreach($xml_bio_children as $xc){
    echo "Found {$xc->status} <br>";
}

2 个答案:

答案 0 :(得分:0)

这里是纯xpath,可用于获取状态为Funded的元素计数。

count(//record[@id="1A"]//status[.='Funded'])

截屏:

enter image description here

答案 1 :(得分:0)

根据supputuri's answer的建议,您可以将两个XPath表达式组合成一个搜索:

//record[@id="1A"]/congrant[status="Funded"]

如果您仍然希望第一个列表用于其他目的,则可以遍历它并在PHP中进行状态检查:

$xml_bio_record=$xml_bio->xpath('//record[@id="1A"]');
$funded_count = 0;
foreach($xml_bio_record as $xb){
    foreach ($xb->congrant as $congrant) {
        if ( (string)$congrant->status == 'Funded' ) {
            $funded_count++;
        }
    }
}

或者您可以使用.混合循环和XPath,以使XPath搜索相对于特定元素:

$xml_bio_record=$xml_bio->xpath('//record[@id="1A"]');
$total_funded_count = 0;
foreach($xml_bio_record as $xb){
    $xb_funded_count = count($xb->xpath('./congrant[status="Funded"]'));
    $total_funded_count += $xb_funded_count;
}