doushi3322 2014-12-31 03:59
浏览 29
已采纳

XML数据使用带有多个元素的PHP表,输出应该基于X元​​素的数量

my xml looks likes this

<?xml version="1.0" encoding="utf-8"?>
<personnel>
                <person id='1'>
                                <person_name>John</person_name>
                                <person_lastname>Doe</person_lastname>
                                <codes>
                                         <code_number>A132</code_number>
                                         <code_number>B123</code_number>
                                         <code_number>C123</code_number>
                                </codes>
                                <person_code>
                                <code_title>Developer</requisition_title>
                                <code_relocation>15~25%</code_relocation>
                                </person_code>
                </person>
             
</personnel>

i wanted to make a table(using PHP) which rows are dependent by the number of "code_number" under "codes". so that the table should look like this:

Person ID | Person Name| Person LastName | Code Numbers 
_________________________________________________________
         1|        John|              Doe|        A123
_________________________________________________________
         1|        John|              Doe|        B123
_________________________________________________________
         1|        John|              Doe|        C123

my currrent PHP script which i got from other stackoverflow examples is:

<?php
$dom = new DOMDocument();
$dom->load('person.xml');
$xpath = new DOMXpath($dom);

echo <<<EOF <table border="1" cellspacing="2" cellpadding="2">   <tr>
    <th>Person ID</th>
    <th>Person Name</th>
    <th>Person LastName </th>
    <th>Code Numbers</th>
    </tr>
EOF;

$persons = $xpath->evaluate('//person');
foreach ($persons as $person) {
   echo '<tr>', "
";
   echo '<td>', htmlspecialchars($xpath->evaluate('string(./id)', $person)), '</td>', "
";
   echo '<td>', htmlspecialchars($xpath->evaluate('string(./person_name)', $person)), '</td>', "
";
   echo '<td>', htmlspecialchars($xpath->evaluate('string(./person_lastname)', $person)), '</td>', "
";
   $codes = array();
   foreach ($xpath->evaluate('./codes/code_number', $person) as $code_number) {
       $codes[] = $code_number->nodeValue;
   }
   echo '<td>'.htmlspecialchars(implode(', ', $codes)).'</td>', "
";
   echo '</tr>', "
"; } echo '</table>';

?>

however the output of this is based on how many "person id" is on the XML file. thanks for you help!

</div>
  • 写回答

1条回答 默认 最新

  • dsv73806 2014-12-31 04:33
    关注

    If you want 1 row per <codes> per <person>, then you need to loop through the <codes> inside the <person>. To get the <person> id use ->getAttribute('id') -

    <?php
    $dom = new DOMDocument();
    $dom->load('person.xml');
    $xpath = new DOMXpath($dom);
    
    echo <<<EOF
    <table border="1" cellspacing="2" cellpadding="2">   <tr>
        <th>Person ID</th>
        <th>Person Name</th>
        <th>Person LastName </th>
        <th>Code Numbers</th>
        </tr>
    EOF;
    
    $persons = $xpath->evaluate('//person');
    foreach ($persons as $person) {
        //loop through each <code_number>
        foreach ($xpath->evaluate('./codes/code_number', $person) as $code_number) {
            echo '<tr>', "
    ";
            echo '<td>', htmlspecialchars($person->getAttribute('id')), '</td>', "
    ";
            echo '<td>', htmlspecialchars($xpath->evaluate('string(./person_name)', $person)), '</td>', "
    ";
            echo '<td>', htmlspecialchars($xpath->evaluate('string(./person_lastname)', $person)), '</td>', "
    ";
            echo '<td>'.htmlspecialchars($code_number->nodeValue).'</td>', "
    ";
            echo '</tr>', "
    "; 
        }
    }
    echo '</table>';
    ?>
    

    This results in

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点