使用 XSL 转换 XML

transforming XML with XSL

如标题所述,我正在尝试在服务器上使用 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...。现在正在工作。感谢帮助

我已将 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 运行ning 生成了这个结果。转换看起来不错,所以问题可能出在输入上。

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