kirbkrawler Posted December 16, 2010 Share Posted December 16, 2010 Hi, I'm new to PHP and wondered if anyone could help... I am consuming an asmx webservice using PHP code. (Later to make a widget for wordpress). I can receive all the data, but it is in one very big xml file. I want to be able to convert this data, so I can make it more readable. Could really do with help as I don't have a clue how to convert it. ANy ideas? Quote Link to comment Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Hello, You didn't mention how you want to convert it. I assume you want to show it in website as (X)HTML. This tutorial might be useful to get an idea. You already have a xml file. So all you need to do is create stylesheet for it and transform it with PHP. Look into here: http://devzone.zend.com/article/1302 Quote Link to comment Share on other sites More sharing options...
kirbkrawler Posted December 16, 2010 Author Share Posted December 16, 2010 Hi, Thanks for the quick response, I would like to take the data and convert it into an HTML table. Can you provide any sample code at all? Thanks in advance Quote Link to comment Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 These XML and XSL files are taken form O'Reilly site: This is example XML (story.xml): <?xml version="1.0" encoding="utf-8" ?> <news xmlns:news="http://slashdot.org/backslash.dtd"> <story> <title>O'Reilly Publishes Programming PHP</title> <url>http://example.org/article.php?id=20020430/458566</url> <time>2002-04-30 09:04:23</time> <author>Rasmus and some others</author> </story> <story> <title>Transforming XML with PHP Simplified</title> <url>http://example.org/article.php?id=20020430/458566</url> <time>2002-04-30 09:04:23</time> <author>k.tatroe</author> </story> </news> This is example XSL (story.xsl): (XML Stylesheet) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="utf-8" /> <xsl:template match="/news"> <html> <head> <title>Current Stories</title> </head> <body bgcolor="white" > <xsl:call-template name="stories"/> </body> </html> </xsl:template> <xsl:template name="stories"> <xsl:for-each select="story"> <h1><xsl:value-of select="title" /></h1> <p> <xsl:value-of select="author"/> (<xsl:value-of select="time"/>)<br/> <xsl:value-of select="teaser"/> [ <a href="{url}">More</a> ] </p> <hr /> </xsl:for-each> </xsl:template> </xsl:stylesheet> And here is the code to process it. This will actually transform the XML file to HTML according to the XSL file. This is simplified example. But I chose to use the Object Oriented way instead of the tutorials procedural way. <?php $xml = new DOMDocument(); $xml->load("story.xml"); $xsl = new DOMDocument(); $xsl->load("story.xsl"); $transform = new XSLTProcessor(); $transform->importStylesheet($xsl); echo $transform->transformToXml($xml); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.