WebReference.com - Part 1 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (6/7) | WebReference

WebReference.com - Part 1 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (6/7)

To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

Professional PHP4 XML

Finally we can write a filter that doesn't produce events but outputs the XML for testing:

class FilterOutput extends AbstractFilter 
{
    function StartElementHandler($name, $attribs) 
    {
        print ("<$name");
        foreach ($attribs as $key => $val) {
            print (" $key = '$val'");
        }
        print ("><br />");
    }
    function EndElementHandler($name) 
    {
        print ("</$name><br />");
    }
    function CharacterDataHandler($data) 
    {
        print ("$data");
    }
}

As we can see this filter can't be used in the middle of a chain because it consumes all the SAX events. It just outputs the XML data (the print() statements where it used to just write a short example).

This is the way we can use them:

$f1 = new ExpatParser("applications.xml");
$f1->ParserSetOption(XML_OPTION_CASE_FOLDING, 0);
$f2 = new FilterName();
$f3 = new FilterOutput();
$f2->SetListener($f3);
$f1->SetListener($f2);
$f1->Parse();

It is very important to build the chain in the proper order. We first have to set the listener of the $f2 filter as $f3 and then the listener of $f1 as $f2. The following figure shows what will happen:

Relationship of SAX filters to transformation flow

If we remember correctly, our transformation not only asked us to uppercase the <name> tags but also to place them in a <b> tag. We can write a filter that converts <name>some</name> elements into <name><b>some</b></name> elements as follows:

class FilterNameBold extends AbstractFilter 
{
    var $flag = 0;
    function StartElementHandler($name, $attribs) 
    {
        if (strtolower($name) == "name") {
            $this->flag = 1;
        } else {
            $this->flag = 0;
        }
        $this->listener->StartElementHandler($name, $attribs);

If we are in a <name> element (flag = 1) then we generate an artificial startElement event with a b tagname by calling the StartElementHandler() method in the listener:

        if ($this->flag) {$this->listener->
                          StartElementHandler("b", array());}
    }
    function EndElementHandler($name) 
    {
        if (strtolower($name) == "name") {
            $this->flag = 0;
            $this->listener->EndElementHandler("b");
        }
        $this->listener->EndElementHandler($name);
    }
    function CharacterDataHandler($data) 
    {
        if ($this->flag) {
            $data = strtoupper($data);
        }
        $this->listener->CharacterDataHandler($data);
    }
}

This is a nice example because it uses a common trick in SAX filters. To add an element we just invent an event for that element by passing it to the next filter. We can run the example instantiating the new chain of filters as follows:

$f1 = new ExpatParser("applications.xml");
$f1->ParserSetOption(XML_OPTION_CASE_FOLDING, 0);
$f2 = new FilterNameBold();
$f2b = new FilterName();
$f3 = new FilterOutput();
$f2b->SetListener($f3);
$f2->SetListener($f2b);
$f1->SetListener($f2);
$f1->Parse();

The result of this will be the XML file with composite transformation applied.


To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

Created: August 12, 2002
Revised: August 12, 2002

URL: https://webreference.com/programming/php/php4xml/chap10/1/6.html