douxian9706
2015-01-10 14:31
浏览 107

使用XSL转换XML

As the title says, I'm trying to transform an XML file using XSL at the server. My XML file saves to the server and displays fine. However if I try transforming it, nothing displays. Here is the code in question.

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'; ?>

EDIT

aplogies about the late reply. When saved, the XML file now holds data. Here is are the files as requested.

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>

EDIT

Turns out there was noting wrong with the code. The server has XSL disabled....working now. Thanks for the help

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxing9219 2015-01-10 18:17
    已采纳

    I've saved the content of the art.xml in a file called just that, and saved the content of your transformation in another file called arttrans.xsl in the same locaion.

    Then running this very simple script in 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);
    ?>
    

    gives the following result:

    <!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>
    

    I've generated this result using PHP running in Apache in XAMPP. The transformation looks fine, so maybe the issue lies with the input.

    EDIT: I've inserted a headline. This headline is printed whether the transformation is run or not - just to be absolutely sure you are targetting the right webpage.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题