Forum Moderators: coopster

Message Too Old, No Replies

XML transform in PHP 4?

possible?

         

jazzle

12:00 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



I'm hoping to eventually use a cron job to take an XML file and transform it on a daily basis.

I found [uk.php.net...]

which looks like a start, but the error returned is
Fatal error: Cannot instantiate non-existent class: domdocument in ...
which I guess means that XML support isn't part of of the version of php on my host (4.3.11).

Is there anyway to do the transform without getting the later(/est) php version?

TIA,
J

coopster

10:48 pm on Sep 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you looked into the other DOM [php.net] and/or XML Functions in PHP? Or perhaps PEAR or another class?

jazzle

9:11 am on Sep 19, 2005 (gmt 0)

10+ Year Member



I have now!
unfortunately, that DOM link appears to be PHP5 specific, and I couldn't see what I needed from Pear.

Of course, my major problem is that I can't install extensions anyway since it's not my server.

I can't be the first person to require XSLT in PHP4!
all advice or suggestions will be muchly appreciated.

coopster

3:45 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I didn't mean for you to stop there in the manual, keep searching ... ;)

there are also XSL functions [php.net]. Not having control over the server is indeed an issue at times, but most decent hosts will accommodate by either allowing you to compile in your own server space and/or do it for you.

jazzle

4:22 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



right now I just need to know (how¦whether it's possible) - I don't want to waste time when I'm sure someone else has already done it.

please don't take this the wrong way - I usually appreciate simply being put on the right track - but this time I need a solution.

Mehuge

12:15 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



I think this is what you are looking for:

[uk2.php.net...]

jazzle

2:54 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



that looks perfect, but I get
Fatal error: Call to undefined function: xslt_create() in /usr/home/**.php on line **

it also doesn't recognise xslt_process().

So I think there's something fundamental I need done, though will it be up to my host to do it?

coopster

12:43 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can check and see if it is installed on your server by running a phpinfo() [php.net] function. If it is not installed [php.net] then yes, your host would likely need to get involved.

Mehuge

10:34 am on Oct 4, 2005 (gmt 0)

10+ Year Member



if you can resolve your undefined function problem, here is some example code that uses xslt_process to transform XML data

// Transform $xml data using $xslFile
// e.g. xslTransform('<myxml>data</myxml>','my.xsl')
function xslTransform($xml, $xslFile)
{
$args = array('/_xml' => $xml);
$xslt_args = array();
$xh = xslt_create();
$r = xslt_process($xh, 'arg:/_xml', $xslFile, NULL, $args, $xslt_args);
xslt_free($xh);
return $r;
}

jazzle

11:23 am on Oct 4, 2005 (gmt 0)

10+ Year Member



thanks for all your help Mehuge and coopster, I have now got it working, though am having bizarre intermittent problems with the server.

jazzle

11:38 am on Oct 4, 2005 (gmt 0)

10+ Year Member



actually, perhaps those errors aren't simply intermittent.

It now seems that I try and run the following code nothing is returned in Fx (very odd - it just keeps the previous page contents and doesn't complain), and IE shows 'the page cannot be displayed'.


// Allocate a new XSLT processor
$xh = xslt_create();

// Process the document
if (!xslt_process($xh, 'pricegraphxmldata.xml', 'cptj.xsl', 'result.xml'))
echo("Sorry: " . xslt_error($xh) . " and the error code is " . xslt_errno($xh));
else echo("successful");

//
xslt_free($xh);

-----
I'm fairly sure that this exact code has worked earlier today.

bulkit

9:59 am on Oct 5, 2005 (gmt 0)

10+ Year Member



I've found this function really useful in the past (before I had php5 which is a dream)

<edit by coopster>
Search for "devdump" in the User Contributed Notes on [php.net...]
</edit>

it serializes your xml into an array that you can easily transform.

[edited by: coopster at 1:57 pm (utc) on Oct. 5, 2005]
[edit reason] removed url per TOS [/edit]

jazzle

10:18 am on Oct 5, 2005 (gmt 0)

10+ Year Member



thanks :) - that could be really useful in another realted project I'm working on.