有人可以帮我弄清楚如何在HTML表格中输出以下XML内容:
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>
我需要的是为名为&#39; Alpha&#39;的团队输出学生信息。在一张桌子里。
输出应具有学生姓名,技能名称和等级分数。所以在上面的例子中,我想首先检查学生是否属于Team Alpha,如果是,则输出他们的姓名&#39; (约翰)和&#39;等级&#39; (5)和&#39;技能&#39; (技术)。
在输出“&#39;等级”之前,该脚本还会检查John是否具备特定技能。得分该技能。如果这个技能&#39;不存在(意味着它不在XML文件中)我想将内容保留为&#39; level&#39;在表空白。
到目前为止,我能够使用xpath查询找到团队名称,然后在for循环中获得每个能力。然后,我可以使用getAttribute(&#39; name&#39;)找到技能名称,然后我用它来检查该特定技能是否存在。但是,我无法以有效的方式解决的问题是如果存在特定技能,如何输出附加到该技能的分数。
期望的输出:
Student Name Maths Technology Science
John 5 5 4
James 3 1
希望这是有道理的。提前谢谢。
更新 我设法弄清楚如何做到这一点,但我无法正确输出表格。以下输出的问题是,当找不到技能时,表格会将值移到左侧。
所以输出看起来像这样
Student Name Maths Technology Science
John 5 5 4
James 3 1
而应该是:
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>";
}
答案 0 :(得分:1)
只需使用XSLT,这是专门用于将XML转换为HTML的专用语言,并避免在PHP中使用字符串和循环构建XML。实际上,您甚至不需要PHP,因为您可以简单地引用XML中的样式表。但是,下面还显示了如何在PHP中处理:
XSLT (另存为.xsl文件; 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>
样式表XML (内联参考,假设在与源相同的目录中)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xslt_script.xsl"?>
PHP (在.ini文件中启用php_xsl扩展名)
# 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);