doulouxun6756 2018-06-14 14:31
浏览 31
已采纳

PHP:在表中输出XML数据

Can someone please help me figure out how to output the below XML contents in a HTML table:

XML:

       <student>
        <name>John</name>
        <map>
        <competency>
          <level>5</level>
          <skill name="Maths"> 
             <skillinfo> "some value" </skillinfo>
          </skill>
        <competency>
      <competency>
          <level>4</level>
          <skill name="Science"> 
             <skillinfo> "some value" </skillinfo>
          </skill>
        <competency>
      <competency>
          <level>5</level>
          <skill name="Technology"> 
             <skillinfo> "some value" </skillinfo>
          </skill>
        <competency>
        </map>
    <team name ="Alpha">
    </team>
        </student>
<student>
        <name>James</name>
        <map>
        <competency>
          <level>3</level>
          <skill name="Maths"> 
             <skillinfo> "some value" </skillinfo>
          </skill>
        <competency>
      <competency>
          <level>1</level>
          <skill name="Science"> 
             <skillinfo> "some value" </skillinfo>
          </skill>
        <competency>
        </map>
    <team name ="Alpha">
    </team>
        </student>

What I need is to output student information for team named 'Alpha' in a table.

The output should have student name, skill name and level score. So in the above example, I want to first check whether the student belongs to Team Alpha, and if so output their 'name' (John) and 'level' (5) and 'skill' (technology).

The script will also check if John has the particular skill before outputting the 'level' score for that skill. If this 'skill' does not exist (meaning it is not in the XML file) I want to leave the contents for 'level' in the table blank.

So far I'm able to find the team name using xpath query and then get each competency in a for loop. Then I'm able to find the skill name by using the getAttribute('name') which I then use to check whether that particular skill exists. But what I'm not able to figure out in an efficient manner is how to output the score attached to that skill if that particular skill exists.

Desired Output:

Student Name    Maths    Technology    Science
John              5          5           4
James             3                      1

Hope this makes sense. Thank you in advance.

UPDATE: I managed to figure out how to do this but I'm not able to output the table properly. Problem with the below output is that when a skill is not found, the table shifts the value to the left.

So the output will look like this

Student Name    Maths    Technology    Science
John              5          5           4
James             3          1

Rather it should be:

Student Name    Maths    Technology    Science
John              5          5           4
James             3                      1 

CODE:

   function displayResult() {       
        $data = getXML($URL);         
        $dom = new DOMDocument;
        $dom->loadXML($data);
        $xpath = new DomXpath($dom);
        echo"<table class='table table-striped table-hover' text-center>
            <thead>
            <tr>
              <th>Student Name</th>
              <th>Maths</th>
              <th>Technology</th>
              <th>Science</th>                                                                    
              </tr>
          </thead>
          <tbody>"; 
        echo"<tr>";                                 
  foreach($xpath->query('//student') as $node)  {   
                $getname = ($node->getELementsByTagName('Name'));
                $StudentName = $getname[0]->nodeValue;          
                echo"<td>$StudentName</td>";                                                                                
                foreach($node-> getELementsByTagName('competency') as $scoreNode){              
                    $getskill = $scoreNode -> getELementsByTagName('skill');
                    $sk = $getskill[0]->getAttribute('name');       
                    if ($sk== 'Maths') {
                        foreach($scoreNode->childNodes as $compNode)                            
                            if ($compNode->tagName == 'level') {
                            echo"<td>$compNode->nodeValue</td>";
                            }                                               
                    }
                    if ($sk== 'Science') {
                        foreach($scoreNode->childNodes as $compNode)                            
                            if ($compNode->tagName == 'level') {
                            echo"<td>$compNode->nodeValue</td>";
                            }                                               
                    }                   

                    if ($sk== 'Technology') {
                        foreach($scoreNode->childNodes as $compNode)                            
                            if ($compNode->tagName == 'level') {
                            echo"<td>$compNode->nodeValue</td>";
                            }                                               
                    }                       

            }                               
                echo " </tr>";  
        }
        echo "</tbody>
        </table>";
    }
  • 写回答

1条回答 默认 最新

  • doukui9491 2018-06-15 17:11
    关注

    Simply use XSLT, the special-purpose language designed to transform XML including into HTML and avoid building XML with strings and loops in PHP. In fact, you do not even need PHP as you can simply reference the stylesheet in XML. However, below also shows how to process in PHP:

    XSLT (save as .xsl file; XSLT Fiddle Demo)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="html" indent="yes" />
    
      <xsl:template match="/*">
        <html>
          <head>
            <title>Student Table</title>
          </head>
          <body>
              <table class='table table-striped table-hover'>
                <thead>
                  <tr>
                    <th>Student Name</th>
                    <th>Maths</th>
                    <th>Technology</th>
                    <th>Science</th>
                  </tr>
                </thead>
                <xsl:apply-templates select="student"/>
              </table>
          </body>          
        </html>
        </xsl:template>
    
        <xsl:template match="student">
          <tr>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="map/competency[skill/@name='Maths']/level"/></td>
            <td><xsl:value-of select="map/competency[skill/@name='Technology']/level"/></td>
            <td><xsl:value-of select="map/competency[skill/@name='Science']/level"/></td>
          </tr>
        </xsl:template>
    </xsl:stylesheet>
    

    Stylesheet XML (inline reference, assumed in same directory as source)

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="xslt_script.xsl"?>
    

    PHP (enable php_xsl extension in .ini file)

    # LOAD XML AND XSL FILES
    $xml = new DOMDocument;
    $xml->load('/path/to/input.xml');
    
    $xsl = new DOMDocument;
    $xsl->load('/path/to/xslt_script.xsl');
    
    // CONFIGURE TRANSFORMER
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl);
    
    // RUN TRANSFORMATION
    $newXML = new DOMDocument;
    $newXML = $proc->transformToXML($xml);
    
    // OUTPUT HTML TABLE
    header("Content-Type: text/html");
    echo $newXML;
    
    // SAVE NEW DOM TREE TO FILE
    file_put_contents('/path/to/output.html', $newXML);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵