php and xml

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

php and xml

Post by gurjit »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

How do I get my xsl template to display. I got the xml feed included using php but need to manipulate the data as defined in my xsl template.

Currently the data is just displayed on the screen as:

"MY PROMOTION 
XXX Mr Joe Bloggs Marketing Ltd 
ADR1 ADR2 ADR3 ADR4 XXX XXX
1 APR 2007apr07351.20 1 MAY 2007may070.00 1 JUN 2007jun0749.50 1 JUL 2007jul070.00 1 AUG 2007aug070.00 1 SEP 2007sep070.00 1 OCT 2007oct070.00 1 NOV 2007nov070.00 1 DEC 2007dec070.00 1 JAN 2008jan080.00 1 FEB 2008feb080.00 1 MAR 2008mar080.00 5000.00 8.01 NO Only £4599.30 required to achieve your Target, validate your points and enable you to start claiming rewards 0 0 0 1010"

I AM USING PHP 4.3

heres the code

INDEX.PHP FILE

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<html>
<head>
	<title>Untitled</title>
</head>

<body>
<table>
<tr>
<td>

<table>
<tr>
<td>MY PROMOTION</td>
</tr>
</table>


<?php 
echo('<?xml version="1.0" encoding="iso-8859-1"?>
	 <?xml-stylesheet type="text/xsl" href="XSL_DOC.xsl"?>'); 
include("XML_DOC.xml"); ?> 

</td>
</tr>
</table>


</body>
</html>
?>

XML_DOC.xml FILE

Code: Select all

<data>
<userid>JB88</userid>
<contractor>
<name>Mr Joe Bloggs</name>
<companyname>Marketing Ltd</companyname>
<address><line1>ADR1</line1>
<line2>ADR2</line2>
<line3>ADR3</line3>
<line4>ADR4</line4>
<postcode>XXX XXX</postcode></address>
</contractor>
<cstatement>
<monthlist>
<month><date>1 APR 2007</date><description>apr07</description><amount>351.20</amount></month>
<month><date>1 MAY 2007</date><description>may07</description><amount>0.00</amount></month>
<month><date>1 JUN 2007</date><description>jun07</description><amount>49.50</amount></month>
<month><date>1 JUL 2007</date><description>jul07</description><amount>0.00</amount></month>
<month><date>1 AUG 2007</date><description>aug07</description><amount>0.00</amount></month>
<month><date>1 SEP 2007</date><description>sep07</description><amount>0.00</amount></month>
<month><date>1 OCT 2007</date><description>oct07</description><amount>0.00</amount></month>
<month><date>1 NOV 2007</date><description>nov07</description><amount>0.00</amount></month>
<month><date>1 DEC 2007</date><description>dec07</description><amount>0.00</amount></month>
<month><date>1 JAN 2008</date><description>jan08</description><amount>0.00</amount></month>
<month><date>1 FEB 2008</date><description>feb08</description><amount>0.00</amount></month>
<month><date>1 MAR 2008</date><description>mar08</description><amount>0.00</amount></month>
</monthlist>
<target>5000.00</target>
<percent>8.01</percent>
<validated>NO</validated>
<message>Only £4599.30 required to achieve your Target, validate your points and enable you to start claiming rewards</message>
<points><total>0</total>
<redeemed>0</redeemed>
<remain>0</remain>
<spent>1010</spent></points>
<rewardlist>

</rewardlist></cstatement>
</data>

XSL_DOC.xsl FILE

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:ms="urn:schemas-microsoft-com:xslt">

<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template match="/">
  <html>
  <body>
  <h2><xsl:value-of select="data/contractor/name"/> <br/> <xsl:value-of select="data/contractor/companyname"/></h2>
  	<table>
	<tr bgcolor="#ffff00">
	<td> 
	<strong>ADDRESS</strong><br/>
	<xsl:value-of select="data/contractor/address"/>,<br/>
	<xsl:value-of select="data/contractor/address/line2"/>,<br/>
	<xsl:value-of select="data/contractor/address/line3"/>,<br/>
	<xsl:value-of select="data/contractor/address/line4"/>,<br/>
	<xsl:value-of select="data/contractor/address/postcode"/>
	</td>
	</tr>
	<tr>
	<td> </td>
	</tr>
	
	</table>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th align="left">Date</th>
        <th align="left">Amount</th>
	
      </tr>
      <xsl:for-each select="data/cstatement/monthlist/month">
      <tr>
        <td><xsl:value-of select="date"/></td>
        <td><xsl:value-of select="amount"/></td>

      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by gurjit on Fri May 25, 2007 5:36 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

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

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

echo $proc->transformToXML($xml);
from: http://php.net/xsl-xsltprocessor-transform-to-xml

Incidentally, is that the same Joey Bloggs that used to me a miner? Would his job be available?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I get the following error. I think its aphp 4.3 issue.

ANY IDEAS HOW TO RESOLVE IT


Warning: domdocument() expects at least 1 parameter, 0 given in /path/httpdocs/xml_test/index.php on line 24

Fatal error: Call to undefined function: load() in /path/httpdocs/xml_test/index.php on line 25
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I USED THE CODE BELOW AND IT WORKED.

Does xslt_create(); create the document on the server and if "YES" then does it delete it?

Code: Select all

<?php 


    $xml_file = "XML_DOC.xml";  
    $xsl_file = "XSL_DOC.xsl";

    $xh = xslt_create();
    $result = xslt_process($xh,$xml_file,$xsl_file,NULL,NULL);

    if($result)
    {
        echo $result;
    }

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you want to know wether xslt_create() creates a file on the server the answer is: no.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

if i upgrade to php 5 from php 4.3 I have read that I will need to change my code because php 5 does not support xslt


is there an alternative solution of manipulating the xml file into xsl, so it is compatable across php 4.3 and 5?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

"xslt" is the php4 implementation, "xsl" is the php5 implementation

http://php.net/xslt (php4)

http://php.net/xsl (php5)
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

so what your saying is that there is no one method that works for both?

if i code now in php 4 i will need to update the source code if i move to php 5?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could just write your code to support both and choose which one to use based on a few checks...
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

so there is no one method that works for both?


i've exhausted google trying to find one method.
Post Reply