How to read xml file and apply css to it using php
Hi all,
I have 'abc.xml' file and 'abc.xsl' file
my work is to read abc.xml file and display such a way that css of abc.xsl should apply to abc.xml
i.e. data of xml file should display in <table><tr><td> format of abc.xsl
I'm giving those 2 files
abc.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<xmlticker>
<pause>6000</pause>
<message> :: DIAMONDS OFFER :: MQ G VVS1 | 1.50 CTS | | | 4000 USD</message>
<message> :: BUY COLOUR STONES :: AMETHYST | 4.78 CTS | MARQUISE GREEN | STONE | 19 USD</message>
<message> :: JEWELLERY OFFERS :: BANGLES | 18K | YELLOW GOLD | DIAMONDS STUDDED | 2215 USD</message>
<message> :: DIAMOND DEMANDS :: HEART | 2.00-2.50 | G-I VS1-VS2 | CERTIFIED ONLY</message>
<message> :: COLOUR STONES DEMANDS :: TOURMALINE OVAL | 1.70-1.75</message>
<message> :: BLUE COLOUR STONES DEMANDS :: TWILIGHT TOPAZ ROUND | 10 CTS | ANY COLOR | PAIR</message>
<message> :: JEWELLERY DEMANDS :: REVIERA NECKLACE | 0.35 CTS | YELLOW GOLD | DIAMONDS</message>
<message> :: JEWELLERY DEMANDS :: BRACELET | 5MM | SILVER | PEARL</message>
</xmlticker>
abc.xls
Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<link href="polygon_offers.css" rel="stylesheet" type="text/css" />
<html>
<body bgcolor="A2A2A2">
<table width="750" border="0" align="left" cellpadding="0" cellspacing="0" padding="0" class="table_border">
<tr>
<td>
<table width="750" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="table_border">
<tr>
<td height="80" colspan="2" align="left" valign="top">
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="123" height="80" align="center" valign="middle" class="header_bg"><img src="images/Jckindia.jpg" width="61" height="65" /></td>
<td width="450" height="80" align="right" valign="middle" class="header_bg"><span class="header_title">Offers by</span><span class="net_title"></span></td>
<td width="152" align="left" valign="middle" class="header_bg"><img src="images/polygon-logo.gif" width="187" height="50" /></td>
<td width="25" align="right" valign="middle" class="header_bg"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="2" colspan="2" class="horizontal_sep_yellow"></td>
</tr>
<tr>
<td height="25" colspan="2"></td>
</tr>
<tr>
<td width="590"></td>
<td width="160">
<a target="_blank" href="http://www.abc.net" ><img src="images/btn_getmore.gif" width="132" height="31" border="0"/></a>
</td>
</tr>
<tr>
<td height="25" colspan="2"></td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle">
<table width="695" height="157" border="0" cellpadding="0" cellspacing="0" class="table_border">
<tr>
<td width="1" rowspan="3" align="left" valign="top" bgcolor="#EBEBEB"></td>
<td height="1" align="left" valign="top" bgcolor="#EBEBEB"></td>
<td width="1" rowspan="3" align="left" valign="top" bgcolor="#EBEBEB"></td>
</tr>
<tr>
<td align="left" valign="top">
<table width="693" border="0" cellpadding="0" cellspacing="1">
<tr >
<td height="25" class="table_head">Offer</td>
<td height="25" class="table_head">Type</td>
<td height="25" class="table_head">Specifications</td>
<td height="25" class="table_head">Color</td>
<td height="25" class="table_head">Stone</td>
<td height="25" class="table_head">Price</td>
</tr>
<xsl:for-each select="/xmlticker/message">
<xsl:variable name="text" select="child::node()" />
<tr >
<td height="25" class="table_detail" >
<xsl:variable name='Offer' select='substring-after($text,"::")'/>
<xsl:variable name='Offers' select='substring-before($Offer,"::")'/>
<xsl:value-of select="$Offers"/>
</td>
<td height="25" class="table_detail" >
<xsl:variable name='Type2' select='substring-after($text,"::")'/>
<xsl:variable name='Type1' select='substring-after($Type2,"::")'/>
<xsl:variable name='Type' select='substring-before($Type1,"|")'/>
<xsl:value-of select="$Type"/>
</td>
<td height="25" class="table_detail" >
<xsl:variable name='Type' select='substring-after($text,"|")'/>
<xsl:variable name='Type1' select='substring-before($Type,"|")'/>
<xsl:value-of select="$Type1"/>
</td>
<td height="25" class="table_detail" >
<xsl:variable name='Type' select='substring-after($text,"|")'/>
<xsl:variable name='Type1' select='substring-after($Type,"|")'/>
<xsl:variable name='Type2' select='substring-before($Type1,"|")'/>
<xsl:value-of select="$Type2"/>
</td>
<td height="25" class="table_detail" >
<xsl:variable name='Type' select='substring-after($text,"|")'/>
<xsl:variable name='Type1' select='substring-after($Type,"|")'/>
<xsl:variable name='Type2' select='substring-after($Type1,"|")'/>
<xsl:variable name='Type3' select='substring-before($Type2,"|")'/>
<xsl:value-of select="$Type3"/>
</td>
<td height="25" class="table_detail_price" >
<xsl:variable name='Type' select='substring-after($text,"|")'/>
<xsl:variable name='Type1' select='substring-after($Type,"|")'/>
<xsl:variable name='Type2' select='substring-after($Type1,"|")'/>
<xsl:variable name='Type3' select='substring-after($Type2,"|")'/>
<xsl:value-of select="$Type3"/>
</td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
<tr>
<td height="1" align="left" valign="top" bgcolor="#EBEBEB"></td>
</tr>
</table>
<br/>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td>
<a target="_blank" href="http://www.abc.net" ><img src="images/btn_getmore.gif" width="132" height="31" border="0" /></a>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Re: How to read xml file and apply css to it using php
So what is your question?
First impressions. Your XML file is strange; because you have got multiple data in the <message> element which is going to be harder to extract in the XSL as demonstrated by the use of substring. I suggest you break this down into many elements if the format is as rigid as the XSL suggests.
Re: How to read xml file and apply css to it using php
Also, have a look at the XSLT extension for PHP 4 (http://www.php.net/xslt) and the XSL functionality for PHP 5 (http://uk3.php.net/manual/en/ref.xsl.php).
Re: How to read xml file and apply css to it using php
thanks visualad!!
I got solution for it.. I need not break down elements...here is code
PHP Code:
<?php
// Load the XML source
$xml = new DOMDocument;
$xml->load('abc.xml');
$xsl = new DOMDocument;
$xsl->load('abc.xsl');
$proc = new XsltProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
it's very sweet, short and easy code...