Jump to content

Web service xml unserialize results


kirbkrawler

Recommended Posts

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?

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.