View Single Post
Old 07-07-2010, 09:11 PM   #4 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It is not really important that it's not your XML feed, so long as PHP can find, read and load the XML then you can perform some XSL transformation on it (assuming your PHP has XSL enabled).

For example, the following loads the feed for the General forum here and sorts the items in the reverse order that they are presented in the feed. Then it just grabs the title and links.

PHP Code:
<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('http://www.talkphp.com/external.php?type=RSS2&forumids=6');

$xsl = new DOMDocument;
$xsl->loadXML('<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="channel">
  <talkphp_threads>
   <xsl:for-each select="item">
    <xsl:sort data-type="number" order="descending" select="position()"/>
    <thread>
     <title><xsl:value-of select="title"/></title>
     <href><xsl:value-of select="link"/></href>
    </thread>
   </xsl:for-each>
  </talkphp_threads>
 </xsl:template>
</xsl:stylesheet>'
);

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

$sorted $proc->transformToDoc($xml);
$sorted->formatOutput true;
$sorted->preserveWhiteSpace false;
echo 
$sorted->saveXML();
And outputs XML similar to:
Code:
<?xml version="1.0"?>
<talkphp_threads>
  <thread>
    <title>Max Image width and height condition ignored by opera?</title>
    <href>http://www.talkphp.com/general/5440-max-image-width-height-condition-ignored-opera.html</href>
  </thread>
  <thread>
    <title>Setting "submit"</title>
    <href>http://www.talkphp.com/general/5442-setting-submit.html</href>
  </thread>
  <thread>
    <title>include in string ?</title>
    <href>http://www.talkphp.com/general/5454-include-string.html</href>
  </thread>
  <!-- snipped, you get the idea -->
  <thread>
    <title>sorting simplexml data by date</title>
    <href>http://www.talkphp.com/general/5511-sorting-simplexml-data-date.html</href>
  </thread>
</talkphp_threads>

Of course, precisely how the XML is loaded is up to you (e.g. using cURL to provide auth credentials. Also remember that once the new document is available as a DOMDocument (the return value from transformToDoc()) then you can switch over to using SimpleXML if that would make life easier (with the simplexml_import_dom() function)
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
webosb (07-08-2010)