Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

javascript - XML Outputting - PHP vs JS vs Anything Else?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...