Forum Moderators: coopster

Message Too Old, No Replies

Possible to use include() to call xml file?

Can you use the include function to call an pre-xsl styled xml file?

         

dcampbell

10:41 am on Dec 10, 2005 (gmt 0)

10+ Year Member



Hi folks

Could anyone tell me whether it's possible to call an xml file as an include?

i.e. include("myfile.xml");

The xml file itself contains the xsl declaration. I can't add all elements to the xsl stylesheet as the page I wish to add the xml file to has a number of db driven elements around it.

Obviously this can be done by parsing the xml file, but I'd rather not do that if possible as the contents updated frequently and old data doesn't require archiving.

Thanks

ergophobe

7:03 pm on Dec 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes you can include it.

The question is, what do you want it to do? I other words, will it simply output the XML (e.g. an RSS feed) or do you need to parse the XML?

dcampbell

9:57 am on Dec 11, 2005 (gmt 0)

10+ Year Member



I only wish to output the XML to screen.

Regards

ergophobe

3:27 pm on Dec 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In that case, just include it and echo it and it will send the XML *as text*

If you want to send it as application XML, you will need to send the proper header() [php.net] call.

dcampbell

3:58 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Thanks for you efforts ergophone. I've tried that, unfortunately I get the following error:

Parse error: syntax error, unexpected T_STRING in c:\filepath\soccerscores_test.xml on line 1

My XML file looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE soccer SYSTEM "soccer4.dtd">
<?xml-stylesheet type="text/xsl" href="soccerscores.xsl"?>
<soccer>
<cp id="15" nm="ENGLAND: Premier League" dt="2005-12-10">
<gm id="78077" tm="13:45" c1="Liverpool" c2="Middlesbrough" cid1="242" cid2="234">
<sr ss="FT" r1="2" r2="0" minute="FT"/>
<ev pl="Fernando Morientes" tp="goal" mt="71" cb="1"/>
<ev pl="Fernando Morientes" tp="goal" mt="77" cb="1"/>
<ev pl="Chris Riggott" tp="rc" mt="84" cb="2"/>
<ev pl="Mohamed Sissoko" tp="yc" mt="66" cb="1"/>
<ev pl="Matthew Bates" tp="yc" mt="66" cb="2"/>
<ev pl="Chris Riggott" tp="yc" mt="79" cb="2"/>
</gm>
</cp>
</soccer>

Regards

dcampbell

4:18 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



OK, I've figured out I need to use xslt_create:

$parser = xslt_create();
$html = xslt_process$parser, "soccerscores_test.xml", "soccerscores.xsl");
xslt_free($parser);
echo $html;

The code above works on a test server I have but not on the server which is going to host the script, where I receive the following error message:

Fatal error: Call to undefined function xslt_create()

I imagine that has something to do with my WAMP installation, dlls not being in the system32 folder and extensions being commented out.

ergophobe

6:45 pm on Dec 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, your error is just because it interprets the <? as invoking php. That had occurred to me. Normally, you can get around it by echoing the <? as text.

So you would have to read the file in to a variable:

$xml = file_get_contents('xml_file.xml');

echo $xml;

That should work as it won't try to evaluate as XML.

dcampbell

7:48 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



I've tried that, unfortunately it doesn't display anything to screen. If I view source, the xml is in there but nothing is presented to the user.

Regards

ergophobe

5:40 pm on Dec 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was assuming that you would be sending this to an RSS reader, an XML application or something like that, not showing it to a user.

If you're sending as text/html to a browser, it should still show though shouldn't it? It just wouldn't truly be interpreted as XML (except maybe by IE?)