亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Simplexml 的 Xpath 根據 PHP 中的值過濾節點

使用 Simplexml 的 Xpath 根據 PHP 中的值過濾節點

PHP
烙印99 2021-10-22 16:54:13
我正在研究一種根據某些節點在 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>";}======== 作為對此的補充,如果我希望將一個變量設置為等于$xb->xpath('./congrant[status="Funded"]'),例如:$xml_congrant_congrant_children=$xb->xpath('./congrant[status="Funded"]')然后在分頁場景中使用索引來循環資助結果,那如何實現?例如for ($i = $offset; $i < ($offset + $per_page); $i++) { $strCongrant_ident = $xml_congrant_congrant_children[$i]['ident'];} 我之前在分頁設置中使用過這個循環想法,但是讓過濾器應用于此處的變量不起作用。感謝您提供任何線索。
查看完整描述

2 回答

?
FFIVE

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;

}


查看完整回答
反對 回復 2021-10-22
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

這是可用于獲取具有Funded狀態的元素計數的純 xpath 。

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

截屏:

http://img1.sycdn.imooc.com//61727c5e0001eb4004580823.jpg

查看完整回答
反對 回復 2021-10-22
  • 2 回答
  • 0 關注
  • 191 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號