Приглашаем посетить
Шмелев (shmelev.lit-info.ru)

13.9 Advanced XML Processing

Previous Table of Contents Next

13.9 Advanced XML Processing

SimpleXML is just the tip of PHP 5's new XML processing capabilities. The DOM functions give you exacting control over all aspects of an XML document, and you can also do XSL transformations, XPath queries, and XInclude processing, as well as execute an extravagant, exhaustive exaltation of other exciting and exotic XML exercises.

Example 13-10 shows an RSS feed-handling class based on the built-in DomDocument class. The addItem( ) method of the RSS class is used to add a new item to the feed.

Example 13-10. Extending DomDocument to handle an RSS feed
class RSS extends DomDocument {

    function _ _construct($title, $link, $description) {

        // Set this document up as XML 1.0 with a root

        // <rss> element that has a version="0.91" attribute

        parent::_ _construct('1.0');

        $rss = $this->createElement('rss');

        $rss->setAttribute('version', '0.91');

        $this->appendChild($rss);

        

        // Create a <channel> element with <title>, <link>,

        // and <description> sub-elements

        $channel = $this->createElement('channel');

        $channel->appendChild($this->makeTextNode('title', $title));

        $channel->appendChild($this->makeTextNode('link', $link));

        $channel->appendChild($this->makeTextNode('description',

                                                  $description));

        

        // Add <channel> underneath <rss>

        $rss->appendChild($channel);

        

        // Set up output to print with linebreaks and spacing

        $this->formatOutput = true;

    }

    

    // This function adds an <item> to the <channel>

    function addItem($title, $link, $description) {

        // Create an <item> element with <title>, <link>

        // and <description> sub-elements

        $item = $this->createElement('item');

        $item->appendChild($this->makeTextNode('title', $title));

        $item->appendChild($this->makeTextNode('link', $link));

        $item->appendChild($this->makeTextNode('description',

                                               $description));

        

        // Add the <item> to the <channel>

        $channel = $this->getElementsByTagName('channel')->item(0);

        $channel->appendChild($item);

    }

    

    // A helper function to make elements that consist entirely

    // of text (no sub-elements)

    private function makeTextNode($name, $text) {

        $element = $this->createElement($name);

        $element->appendChild($this->createTextNode($text));

        return $element;

    }

}



// Create a new RSS feed with the specified title, link and description

// for the channel.

$rss = new RSS("What's For Dinner", 'http://menu.example.com/', 

               'These are your choices of what to eat tonight.');

// Add three items

$rss->addItem('Braised Sea Cucumber',

              'http://menu.example.com/dishes.php?dish=cuke',

              'Gentle flavors of the sea that nourish and refresh you.');

$rss->addItem('Baked Giblets with Salt',

              'http://menu.example.com/dishes.php?dish=giblets',

              'Rich giblet flavor infused with salt and spice.');

$rss->addItem('Abalone with Marrow and Duck Feet',

              'http://menu.example.com/dishes.php?dish=abalone',

              "There's no mistaking the special pleasure of abalone.");

// Print the XML

print $rss->saveXML( );

Example 13-10 prints:

<?xml version="1.0"?>

<rss version="0.91">

  <channel>

    <title>What's For Dinner</title>

    <link>http://menu.example.com/</link>

    <description>These are your choices of what to eat tonight.</description>

    <item>

      <title>Braised Sea Cucumber</title>

      <link>http://menu.example.com/dishes.php?dish=cuke</link>

      <description>Gentle flavors of the sea that nourish and refresh you.

</description>

    </item>

    <item>

      <title>Baked Giblets with Salt</title>

      <link>http://menu.example.com/dishes.php?dish=giblets</link>

      <description>Rich giblet flavor infused with salt and spice.</description>

    </item>

    <item>

      <title>Abalone with Marrow and Duck Feet</title>

      <link>http://menu.example.com/dishes.php?dish=abalone</link>

      <description>There's no mistaking the special pleasure of abalone.

</description>

    </item>

  </channel>

</rss>

XSL transformations use the XSLTProcessor class. Example 13-11 makes an HTML document from the $rss object created in Example 13-10 with the XSL stylesheet in Example 13-12 (saved as rss.xsl).

Example 13-11. Transforming XML to HTML with XSL
// Create a new XSL Transformer

$xslt = new XSLTProcessor( );

// Load the stylesheet from the file rss.xsl

$xslt->importStyleSheet(DomDocument::load('rss.xsl'));



// Apply the stylesheet to the XML 

$html = $xslt->transformToDoc($rss);

// Print out the content of the new document

$html->formatOutput = true;

print $html->saveXML( );

Example 13-12. An XSL stylesheet for RSS feeds
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="/">

<h1><xsl:value-of select="/rss/channel/title"/></h1>

<h2><a><xsl:attribute name="href"><xsl:value-of select="/rss/channel/link"/></xsl:

attribute> 

<xsl:value-of select="/rss/channel/link"/></a></h2>

<h3><xsl:value-of select="/rss/channel/description"/></h3>

<hr/>

<ul>

<xsl:for-each select="/rss/channel/item">

<li>

<a><xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute> 

<xsl:value-of select="title"/></a>

 - <xsl:value-of select="description"/></li>

</xsl:for-each>

</ul>

</xsl:template>

</xsl:stylesheet>

Example 13-11 prints:

<?xml version="1.0"?>

<h1>What's For Dinner</h1>

<h2>

  <a href="http://menu.example.com/">http://menu.example.com/</a>

</h2>

<h3>These are your choices of what to eat tonight.</h3>

<hr/>

<ul>

  <li><a href="http://menu.example.com/dishes.php?dish=cuke">Braised Sea Cucumber</a>

 - Gentle flavors of the sea that nourish and refresh you.</li>

  <li><a href="http://menu.example.com/dishes.php?dish=giblets">Baked Giblets with 

Salt</a>

 - Rich giblet flavor infused with salt and spice.</li>

  <li><a href="http://menu.example.com/dishes.php?dish=abalone">Abalone with Marrow 

and Duck Feet</a>

 - There's no mistaking the special pleasure of abalone.</li>

</ul>

Read Chapter 5 of Upgrading to PHP 5 (O'Reilly) for more details on PHP 5's XML functions. Learning XSLT by Michael Fitzgerald (O'Reilly) is a good introduction to XSLT.

    Previous Table of Contents Next