2 回答

TA貢獻1797條經驗 獲得超6個贊
正如supputuri 的回答所暗示的那樣,您可以將兩個 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;
}

TA貢獻1906條經驗 獲得超3個贊
這是可用于獲取具有Funded
狀態的元素計數的純 xpath 。
count(//record[@id="1A"]//status[.='Funded'])
截屏:
- 2 回答
- 0 關注
- 191 瀏覽
添加回答
舉報