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 wi
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
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
I really like the XSLT solution offered in the last post. Great example of XSLT use.