我有一个XML文件,我想用PHP读取数据。我已经从中读取了数据,但问题是我只想为一个类别运行循环,并显示该类别中的所有匹配项这是我的XML文件链接和我的PHP代码
<?php
$test = simplexml_load_file('http://www.goalserve.com/samples/soccer_livescore.xml');
//echo $test->category->matches->match->localteam["name"]."<br>";
//echo $test->category->matches["date"];
?>
<table border="1">
<tr>
<td style="border:none;">Local Team </td>
<td>Score </td>
<td>Visitor team</td>
<td>Status </td>
<td>Date </td>
<td>Time </td>
<td>HT </td>
<td class="hidden-result">Events</td>
</tr>
<?php
foreach ($test->category as $cate) {
foreach ($cate->children() as $matches) {
foreach ($matches->children() as $match) {
// print_r($match);
echo "<td>".$match->localteam["name"]."</td>";
echo '<td><a href="javascript:MoreInformation('.$match["fix_id"].');"><img src="http://www.goalserve.com/images/arrow-down.gif" alt="More information" title="More information" id=I'.$match["fix_id"].'" name=I'.$match["fix_id"].' />'.$match->localteam["goals"].'-'.$match->visitorteam["goals"].'</a></td>';
echo "<td>".$match->visitorteam["name"]."</td>";
echo "<td>".$match["status"]."</td>";
echo "<td>".$match["date"]."</td>";
echo "<td>".$match["time"]."</td>";
echo "<td>".$match->ht["score"]."</td>";
?>
<div class="hidden-result">
<?php
foreach ($match->events->event as $t_event) {
if($t_event["type"]=="goal") {
?>
<td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/1.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td>
<?php
}
if($t_event["type"]=="yellowcard") {?>
<td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/3.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td>
<?php
}
if($t_event["type"]=="redcard") {?>
<td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/2.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td>
<?php
}
}?>
</div>
<?php
echo "</tr>";
}
}
}
?>
</table>
如果使用XSLT作为HTML的转换语言,则XML文件的输出只需要3行PHP代码:
<?php
$proc=new XsltProcessor;
$proc->importStylesheet(DOMDocument::load("test.xsl")); //load XSL script
echo $proc->transformToXML(DOMDocument::load("soccer_livescore.xml")); //load XML file and echo
?>
这是HTML表(文件"test.xsl")的正确XSLT代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head/>
<body>
<table>
<tbody>
<tr>
<th>Local team</th>
<th>Score</th>
<th>Visitor team</th>
<th>Status</th>
<th>Date</th>
</tr>
<xsl:apply-templates select="scores/category[@name='England: Premier League']/matches/match"/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="match">
<tr>
<td><xsl:value-of select="localteam/@name"/></td>
<td><xsl:value-of select="ht/@score"/></td>
<td><xsl:value-of select="visitorteam/@name"/></td>
<td><xsl:value-of select="@status"/></td>
<td><xsl:value-of select="@formatted_date"/></td>
</tr>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
这并没有包含您的所有列,但它给出了想法。我承认这不是您想要的方式,但它解决了您的问题,并显示了使用XSLT:-)是多么容易
在第一个foreach中,您可以通过attributes()函数访问类别的id(或名称)。
http://us1.php.net/manual/en/simplexmlelement.attributes.php
$cate->attributes()->id
这应该有助于你弄清楚你目前正在迭代的类别,然后只显示你想要的和/或跳过其他类别。