This is continued from http://phpbuilder.com/board/showthread.php?t=10313971 which I set as resolved but have since encountered problems...

I'm basicly just trying to include () an xml file in a php page.

I've run into some unexpected problems with this though...

I have an XML file which references an XSL file. When I open the XML file in a browser it correctly applies the XSL and sorts and formats the XML. The tutorial I followed to creat this is here - http://www.w3schools.com/xsl/xsl_sort.asp.

Problem 1 is that when I try to include () the xml file I get a parse error. I think that this is due to the xml declaration and the xsl declaration at the start of the .xml file being enclosed in <? ?>.

This brings me to question 1: How do I turn off short tags so it'll only parse <?php ... ?> rather than everything in <? ... ?>. I'm using php5 on apache server if that helps... I had a brief search through both manuals but couldn't find referance to short tag format in either (I'll have a proper look later though)

Moving on...

On this page there are a number of ways offered in the user comments at the bottom for getting round this problem - basicly using echo () to generate the xml declarations;

http://www.php.net/manual/en/language.basic-syntax.php

I've tried several and, while I no longer get the php parse error, the xml file isn't finding the xsl so all the xml is just displayed as plain text...

So heres question 2: Why is the xml file not linking to the xsl correctly? Is it just because the browser isn't recognising it as xml because I 'cheated' php into echoing the xml and xsl sheet declarations (in which case I assume resolving question 1 will sort this out too) or am I missing something more serious?

Hope that all made sense!

EDIT: Shrike has kindly pointed me in the right direction with regards to turning short tags off which I'll try when I get home from work. Will post back how I get on...

    1.) To turn short tags on/off, it's a php.ini setting.
    2.) Try setting the header to be "text/xml" before echoing any XML.

    $ini =ini_set('short_open_tag', 0);
    if(empty($ini) || !$ini)
    {
      die("The script can't run because short tags are enabled!!");
    }

    However, this won't solve your problem because:

    <?php
    
    include('somefile.xml');
    // The first line of the XML file will be <?xml blahblahblah ?>
    // So the php parser will stop after the first line, and all XML
    // will be returned as text.
    ?>

    ~Brett

      It's not going to stop the parser 'cos the parser isn't running...

      According to http://uk.php.net/function.include

      When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

      Well, thats fine by me - the xml file contains no php at all so I don't want the parser running!

      Still not working tho...

      I've turned short tags off and now the xml file is correctly included without need to make the xml declarations echoed or anything... thats great.

      BUT! It's still not picking up the formatting from the xsl file - no idea why tho

        Actually, if you read the include() manual, if it runs into valid PHP tags, the parser will run. Isn't "<?" a valid PHP short tag? Yes, it is. So an xml declaration has a valid PHP short tag. So with them on, it will parse the rest of the document, or try to.

        Try using the header:
        And out of curiosity, why include an XML file just to display it?

        <?php
        
        header('Content-Type: text/xml');
        
        include('somefile.xml');
        
        ?>

        ~Brett

          I've turned short tags off so the parser will only start on <?php.

          I'm using include () to get the xml so that I can use XSLT to sort and format the data, at the time it sounded like the easiest way to do it although now I'm wondering if loading the XML file into an array and using php to do the sorting etc might be easier.

          I have a kinda grim determination to get it working now though 🙂

          Will have another look later. I'm sure I'm just missing something stupid!

            Hmm... just found this which sounds like it will do exactly what i want it to;

            http://uk.php.net/manual/en/function.xslt-process.php

            Only problem is that;

            This extension uses Sablotron and expat, which can both be found at http://www.gingerall.com/. Binaries are provided as well as source.

            I'm going to try and set this up on my server at home that I'm using for testing stuff out - if/when I get external hosting, does anyone know how hard it is to get providers to install additional stuff like this for php?

              Normally good hosts will at least try and install the extension on a testing server to see if it will work with the current setup. If they aren't willing to test it, you'll find a hard time getting it added safely.

              If you post a short bit of your XML and XSLT documents, I can test locally something I've been thinking about for a while.

              But you still didn't answer this question:
              Why include the XML file just to display it without any modification?

              ~Brett

                Thanks for the thoughts on hosting... think I'll stick to using 'out of the box' functionality for the moment to be on the safe side.

                I'm using include to get the XML file because (to my mind anyway) its easier to quickly edit it if it's in a seperate file, rather than embedded in the main body of code.

                If its dumb to do that way, let me know - I'm totally new to all this... my previous experience is pretty much just very basic HTML and CSS.

                At the moment I've been testing using some example xml/xsl from a tutorial on www.w3schools.com.

                XML Extract;

                <?xml version="1.0" encoding="ISO-8859-1"?>
                <?xml-stylesheet type="text/xsl" href="cdcatalog_filter.xsl"?>
                <catalog>
                  <cd>
                    <title>Empire Burlesque</title>
                    <artist>Bob Dylan</artist>
                    <country>USA</country>
                    <company>Columbia</company>
                    <price>10.90</price>
                    <year>1985</year>
                  </cd>
                  <cd>
                    <title>Hide your heart</title>
                    <artist>Bonnie Tyler</artist>
                    <country>UK</country>
                    <company>CBS Records</company>
                    <price>9.90</price>
                    <year>1988</year>
                  </cd>
                  <cd>
                    <title>Greatest Hits</title>
                    <artist>Dolly Parton</artist>
                    <country>USA</country>
                    <company>RCA</company>
                    <price>9.90</price>
                    <year>1982</year>
                  </cd>
                </catalog>
                

                Full 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> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

                  So you're getting an xml file to include through PHP to show the xml with the applied stylesheet.... sounds like you DON'T need PHP to me...

                  ~Brett

                    The rest of the site so far is generated by php - my side nav bar for example is dynamic and I have a gallery which is generated by php too... to be honest, I'm making a website to learn php... this bit ended up as a sidetrack 'cos I thought XML/XSLT sounded like the right tool for the particular bit I was doing...

                    If anything it's been interesting learning the limitations of php in this area, it's a shame that php doesn't have more functionality for handling xml but I guess that will come with time.

                    Do other languages like ASP have similar limitations when dealing with XML?

                      PHP 5 has functionality with XML... and PHP 4 with extensions does too. Not sure about the other languages though.

                      ~Brett

                        5 days later

                        Yeah, if you use PHP5 it has the XML DOM functionality built straight into it.

                        I think that this could be caused by a couple of issues
                        1. Because you are including the file the final output is in a different directory to your stylesheet.
                        2. The browser is not getting the correct MIME type.

                        Whenever I use XML and XSLT I do the transformation on the server side. That way I have control over what gets sent to the users browser. When you do the transformation on the client side you are relying on the different versions of browsers to all handle it the same way.

                        If I were you I would investigate the different ways of doing the transformation server side. Either using Sablotron or DOM.

                          I believe DOM was built into some version of php4. Also
                          DOM (Document object model)is Sluggish as is SAX and salbatron(sounds kinda like a robot huh?). however if you are working in PHP5 read this tutorial on SimpleXML as it explains it best...

                            a month later

                            I've got PHP5 on my machine at home but have found out the webserver I'm using is PHP4 with no scope to upgrade :/

                            I'll certainly look at DOM etc as I'd like to get it working the way I'd planned if only for my own satisfaction!

                              Write a Reply...