使用XSL转换XML

时间:2015-01-10 14:31:44

标签: php xml xslt

正如标题所说,我正在尝试使用服务器上的XSL转换XML文件。我的XML文件保存到服务器并显示正常。但是,如果我尝试转换它,则不显示任何内容。这是有问题的代码。

XML.PHP

<?php 
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php"); 
$connection = mysqli_connect($hostname, $username, $password, $databaseName) or die("you did not connect");
$query = "SELECT * FROM art";
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
//Top of xml file
$_xml = '<?xml version="1.0"?>'; 
$_xml .="<art>"; 
while($row = mysqli_fetch_array($result)) { 
$_xml .="<art>"; 
$_xml .="<art_name>".$row['name']."</art_name>"; 
$_xml .="<art_category>".$row['category']."</art_category>"; 
$_xml .="<art_price>".$row['price']."</art_price>"; 
$_xml .="</art>"; 
} 
$_xml .="</art>"; 
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//And output
//print $xmlobj->asXML();
//or we could write to a file
$xmlobj->asXML('art.xml');
?>

XSL.PHP

<?php require 'header.php';?>
<div class="sixteen columns">
<?php 
//Create a DomDocument object

  $xml = new DOMDocument;

  // Load the XML source

  $xml -> load('art.xml');


//Similar with XSL

  $xsl = new DOMDocument;

  $xsl -> load('art.xsl');

  // Create and Configure the transformer

  $proc = new XSLTProcessor;

  // attach the xsl rules

  $proc -> importStyleSheet($xsl);

  //Output

  echo $proc -> transformToXML($xml);


?>
</div>
<?php require 'footer.php'; ?>

修改

关于迟到回复的歉意。保存后,XML文件现在保存数据。这是所要求的文件。

art.xml

<?xml version="1.0"?>
<art><art><art_name>Game Weapons</art_name><art_category>Weapons</art_category><art_price>11</art_price></art><art><art_name>Violet</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>Wolf</art_name><art_category>Character</art_category><art_price>11</art_price></art><art><art_name>A Calvert</art_name><art_category>Character</art_category><art_price>11</art_price></art><art><art_name>Scatter Character</art_name><art_category>Character</art_category><art_price>11</art_price></art><art><art_name>Bonfire</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>Clouds</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>Dawn</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>Face</art_name><art_category>Character</art_category><art_price>11</art_price></art><art><art_name>Scarra</art_name><art_category>Character</art_category><art_price>11</art_price></art><art><art_name>Scatter World</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>Woods</art_name><art_category>Scenery</art_category><art_price>11</art_price></art><art><art_name>f</art_name><art_category>f</art_category><art_price>2</art_price></art></art>

art.xsl

<!--  Edited with XML Spy v4.2  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>Art</h2>
<table border="1">
<tr bgcolor="#333">
<th align="left" style="padding:0 0 0 20px;"><font color="#FFF">Name</font></th>
<th align="left" style="padding:0 0 0 15px;"><font color="#FFF">Category</font></th>
<th align="left" style="padding:0 0 0 5px;"><font color="#FFF">Price</font></th>
</tr>
<xsl:for-each select="art/art">
<tr>
<td style="padding:10px 15px 10px 0;">
<xsl:value-of select="art_name"/>
</td>
<td style="padding:10px 15px 10px 15px;">
<xsl:value-of select="art_category"/>
</td>
<td style="padding:10px 15px 10px 15px;">
<xsl:value-of select="art_price"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

修改

事实证明代码没有错。服务器已禁用XSL ....现在正在工作。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我已将art.xml的内容保存在一个名为该文件的文件中,并将转换内容保存在同一位置的另一个名为arttrans.xsl的文件中。

然后在PHP中运行这个非常简单的脚本

<h1>something</h1>
<?php   
    $xml = new DOMDocument;
    $xml->load('art.xml');

    $xsl = new DOMDocument;
    $xsl->load('arttrans.xsl');

    // Configure the transformer
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl); // attach the xsl rules

    echo $proc->transformToXML($xml);
?>

给出以下结果:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h2>Art</h2>
<table border="1">
<tr bgcolor="#333">
<th align="left" style="padding:0 0 0 20px;"><font color="#FFF">Name</font></th>
<th align="left" style="padding:0 0 0 15px;"><font color="#FFF">Category</font></th>
<th align="left" style="padding:0 0 0 5px;"><font color="#FFF">Price</font></th>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Game Weapons</td>
<td style="padding:10px 15px 10px 15px;">Weapons</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Violet</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Wolf</td>
<td style="padding:10px 15px 10px 15px;">Character</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">A Calvert</td>
<td style="padding:10px 15px 10px 15px;">Character</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Scatter Character</td>
<td style="padding:10px 15px 10px 15px;">Character</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Bonfire</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Clouds</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Dawn</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Face</td>
<td style="padding:10px 15px 10px 15px;">Character</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Scarra</td>
<td style="padding:10px 15px 10px 15px;">Character</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Scatter World</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">Woods</td>
<td style="padding:10px 15px 10px 15px;">Scenery</td>
<td style="padding:10px 15px 10px 15px;">11</td>
</tr>
<tr>
<td style="padding:10px 15px 10px 0;">f</td>
<td style="padding:10px 15px 10px 15px;">f</td>
<td style="padding:10px 15px 10px 15px;">2</td>
</tr>
</table>
</body></html>

我使用在XAMPP中运行Apache的PHP生成了这个结果。转换看起来很好,所以问题可能在于输入。

编辑:我已插入标题。无论转换是否运行,都会打印此标题 - 只是为了确保您正在定位正确的网页。