1

I am working on developing a Travel website which uses XML API's to get the data.

However i am relatively new to XML and outputting it. I have been experimenting with using PHP to output a test XML file, but currently the furthest iv got is to only output a few records.

As it the questions states i need to know which technology will be best for this project. Below iv included some points to take into consideration.

  • The website is going to be a large sized, heavy traffic site (expedia/lastminute size)
  • My skillset is PHP (intermediate/high skilled) & Javascript (intermediate/high skilled)

Below is an example of the XML that the API is outputting:

<?xml version="1.0"?>
<response method="###" success="Y">
    <errors>
    </errors>
    <request>
        <auth password="test" username="test" />
        <method action="###" sitename="###" />
    </request>
    <results>
        <line id="6" logourl="###" name="Line 1" smalllogourl="###">
            <ships>
                <ship id="16" name="Ship 1" />
                <ship id="453" name="Ship 2" />
                <ship id="468" name="Ship 3" />
                <ship id="356" name="Ship 4" />
            </ships>
        </line>
        <line id="63" logourl="###" name="Line 2" smalllogourl="###">
            <ships>
                <ship id="492" name="Ship 1" />
                <ship id="454" name="Ship 2" />
                <ship id="455" name="Ship 3" />
                <ship id="421" name="Ship 4" />
                <ship id="401" name="Ship 5" />
                <ship id="404" name="Ship 6" />
                <ship id="405" name="Ship 7" />
                <ship id="406" name="Ship 8" />
                <ship id="407" name="Ship 9" />
                <ship id="408" name="Ship 10" />
            </ships>
        </line>
        <line id="41" logourl="###">
            <ships>
                <ship id="229" name="Ship 1" />
                <ship id="230" name="Ship 2" />
                <ship id="231" name="Ship 3" />
                <ship id="445" name="Ship 4" />
                <ship id="570" name="Ship 5" />
                <ship id="571" name="Ship 6" />
            </ships>
        </line>
    </results>
</response>

If possible when suggesting which technlogy is best for this project, if you could provide some getting started guides or any information would be very much appreciated.

Thank you for taking the time to read this.

itsphil
  • 145
  • 2
  • 11
  • If you don't insist on XML, JSON is another choice. I prefer using JSON because it saves me the trouble of parsing XML. :) – MeanEYE Feb 09 '11 at 15:25
  • 1
    Im not sure what you mean by "insist on XML"?. The API outputs XML and we arent able to change that? – itsphil Feb 09 '11 at 15:35
  • Ohhh sorry i see, you mean convert the XML to JSON. Thats a possibility, but is that best practice? – itsphil Feb 09 '11 at 15:37
  • JSON would definitely be easier for the client-side browser to handle, since it's basically just javascript. XML also tends to be "fatter" (obese-ier?), which would lead to increased load times for the site. – Marc B Feb 09 '11 at 16:18

3 Answers3

1

XSLT works like a charm and is supported by PHP. It takes this XSLT script to output your XML file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
        <head>
            <title>Test</title>
        </head>
        <body>
            <h1>User: <xsl:value-of select="//request/auth/@username"/></h1>
            <xsl:apply-templates select="//results/line"/>
        </body>
    </html>
</xsl:template>
<xsl:template match="line">
    <div>
        <h3>Line ID: <xsl:value-of select="@id"/></h3>
        <xsl:apply-templates select="./ships/ship"/>
    </div>
</xsl:template>
<xsl:template match="ship">
    <div>
        <xsl:value-of select="@id"/>
        <xsl:text> - </xsl:text>
        <xsl:value-of select="@name"/>
    </div>
</xsl:template>
</xsl:stylesheet>

To run the script against your file use just 3 lines of PHP code:

<?php
$proc=new XsltProcessor;
$proc->importStylesheet(DOMDocument::load("test.xsl")); //load script
echo $proc->transformToXML(DOMDocument::load("test.xml")); //load your file
?>

You can even try this transformation in your browser without any PHP adding this
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
as second line in your XML file and opening the XML file in Firefox or IE having the XSL file in the same folder.
Changing the 3rd PHP line in this
$proc->transformToUri(DOMDocument::load("test.xml"),"test.html");
will save the output as static file.

Some good advice is suggested here:
https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online

Community
  • 1
  • 1
Andreas
  • 1,220
  • 8
  • 21
  • This seems like a good solution. How would this solution work with dynamic XML. Basically the XML will be being sent through the browser, now a specific XML file. If that makes any sense. – itsphil Feb 10 '11 at 11:43
  • To load the XML in another way, you have to change the line $proc->importStylesheet(DOMDocument::load("test.xsl")); which is a short code of the two lines $doc = new DOMDocument(); $doc->load('book.xml'); The "load" command gets the XML out of a file. You can also use "loadXML" to load from a string or "loadHTML"/"loadHTMLFile" to load HTML which may not be wellformed in terms of XML. For details see php.net/manual/en/class.domdocument.php The DOMDocument can be accessed via XPath in XSLT as shown but also with PHP via XPath or DOM API (getElementsByTagName etc.) – Andreas Feb 10 '11 at 15:09
0

I'd suggest to use PHP's SimpleXML extension. With SimpleXML it's a breeze to parse an XML datafeed and create a formatted output from that.

http://php.net/manual/en/intro.simplexml.php

And some easy to understand examples:

http://php.net/manual/en/simplexml.examples-basic.php

András Szepesházi
  • 6,483
  • 5
  • 45
  • 59
-1

I really like the XSLT solution offered in the last post. Great example of XSLT use.

mozillanerd
  • 568
  • 4
  • 9