Total Pageviews

Friday, February 24, 2012

Transform XML documents into XHTML with XSLT files

test.html

<form action = 'xml.php' method = 'post'>
    <h4>Search : </h4><input type = 'text' name = 'search' class='tb7'/><br/><br/>
   
               <input type = 'submit' value = 'Search' name='submit'/>
   
    </form>

xml.php

<?php
header('Content-type: text/xml');
include_once('config.php');

    if(isset($_POST['submit']))
    {
        $search=$_POST['search'];
        $query = "SELECT * FROM `member_details` where `Keywords` LIKE '%$search%'";
        $result = mysql_query($query);
        if(!$result )
        {
            die('Could not get data: ' . mysql_error());
        }       
        $xml_output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \n";
        $xml_output .= "<?xml-stylesheet type=\"text/xsl\" href=\"xslt1.xsl\"?>";
        $xml_output .= "<dataset>\n";
        for($x = 0 ; $x < mysql_num_rows($result) ; $x++){
        $row = mysql_fetch_assoc($result);
        $xml_output .= "<entry>\n";
        $xml_output .= "<id>".$row['Id']."</id>\n";
        $xml_output .= "<name>".$row['Name']."</name>\n";
        $xml_output .= "<address>".$row['Address']."</address>\n";
        $xml_output .= "<postcode>".$row['Postcode']."</postcode>\n";
        $xml_output .= "<telephone>".$row['Telephone']."</telephone>\n";
        $xml_output .= "<opentime>".$row['OpenTime']."</opentime>\n";
        $xml_output .= "<restauranttype>".$row['ResType']."</restauranttype>\n";
        $xml_output .= "<licence>".$row['Licence']."</licence>\n";
        $xml_output .= "<keywords>".$row['Keywords']."</keywords>\n";
        $xml_output .= "<cuisine>".$row['Cuisine']."</cuisine>\n";
        $xml_output .= "</entry>\n";
           
        }
        $xml_output .= "</dataset>\n";    
        echo $xml_output;
       
    }

?>



xslt1.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">
        <html>
        <body>
      
            <table border="0">
            <tr bgcolor="#9acd32">
                <th>Ref No.</th>
                <th>Name</th>
                <th>Telephone</th>
                <th>Full Details</th>
            </tr>
            <xsl:for-each select="dataset/entry">
            <tr>
                <td><xsl:value-of select="id"/></td>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="telephone"/></td>
                <td><xsl:variable name="vid" select="id"/><a href="fullviewXML.php?id={$vid}">full view</a></td>
              
            </tr>
            </xsl:for-each>
            </table>
        </body>
        </html>
        </xsl:template>
        </xsl:stylesheet>        

No comments:

Post a Comment