XML输出-PHP vs JS vs其他吗? [英] XML Outputting - PHP vs JS vs Anything Else?

查看:72
本文介绍了XML输出-PHP vs JS vs其他吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个旅游网站,该网站使用XML API来获取数据.

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

但是我对XML还是比较陌生并输出它.我一直在尝试使用PHP来输出测试XML文件,但是目前最远的方法是仅输出一些记录.

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.

由于存在这些问题,我需要知道哪种技术最适合该项目. iv下方包括一些要考虑的要点.

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.

  • 该网站将是一个大型,人流量大的网站(expedia/lastminute大小)
  • 我的技能是PHP(中级/高级),并且Javascript(中级/高级)

以下是API输出的XML的示例:

<?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.

推荐答案

XSLT就像一个超级按钮一样受PHP支持.需要这个XSLT脚本来输出您的XML文件:

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>

要针对文件运行脚本,只需使用3行PHP代码:

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
?>

您甚至可以在浏览器中尝试进行此转换,而无需任何PHP添加此操作
<?xml-stylesheet type ="text/xsl" href ="test.xsl"?>
作为XML文件中的第二行,并在Firefox或IE中打开XML文件,而Firefox或IE中的XSL文件位于同一文件夹中.
在此更改第3行PHP
$ proc-> transformToUri(DOMDocument :: load("test.xml"),"test.html");
将输出保存为静态文件.

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.

这里建议一些好的建议:
https://stackoverflow.com/questions/339930/any-good- xslt-tutorial-book-blog-site-online

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

这篇关于XML输出-PHP vs JS vs其他吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆