douxian0008 2019-07-30 17:54 采纳率: 0%
浏览 115

Simplexml对节点值的Xpath过滤和在分页中获取兄弟姐妹值

I'm working on a way to assign a variable equal to a filtered path (filtering certain nodes based upon their value in PHP). I'm trying to return the value of the 'Ident' node, when its sibling ('Status') equals "Funded". In another post related to this project, I got the count of these filtered items (IMSoP's answer), but my aim here is to get the filtered item (node) value for each iteration in a pagination scenario.
Using the count of the filtered items (from that previous post), the pagination code iterates through the filtered items in xml tree data.
Thanks for any leads.

Here is the 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>

Here is the PHP (for the count of the filtered data):

$url_bio = "test.xml";  

$xml_bio = simplexml_load_file($url_bio); 

$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++;
        }
    }
}

Here is what I've tried to get the filtered values to display in the pagination looping. I've simplified the loop somewhat here, to just loop from 0 to the count of filtered items:

$xml_bio_record=$xml_bio->xpath('//record[@id="1A"]');
for ($i = 0; $i <=$funded_count; $i++) { 
    $strCongrant_ident = $xml_bio_record[0]->xpath('./congrant[$i][status="Funded"]/ident];
    echo('<br>$strCongrant_ident is...'.$strCongrant_ident);
} 

I've used this loop idea before in a pagination setting, but getting the filter to apply to the variable here is not working - it is not returning the 'ident' node value being a sibling to the matching 'status="Funded"' node. Thanks for any leads.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题