WebReference.com - Part 3 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (6/7)
[previous] [next] |
Professional PHP4 XML, Chapter 10: Putting It Together
Using XSLT To Query a Document
XSLT is a very good tool to query a document. Whenever XPath is not enough to query a document we may think of using XSLT.
The same XSLT that is normally used to transform a document can be used to query it, obtaining a result instead of a transformation.
A query can be seen as a transformation where the document is transformed in the query result.
When using XSLT we can isolate each query into an XSLT stylesheet and write composite queries using <xsl:include>
.
For example, the following XSLT stylesheet solves the query of our example:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/cars/car">
<xsl:if test="normalize-space(maxspeed/text())>300 and
normalize-space(type/text())='F1'">
Brand: <xsl:value-of select="brand" />
Model: <xsl:value-of select="model" />
<br />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The output after processing the XML file with the above XSLT would have been the same as the PHP script using XPath.
Here are the advantages of XSLT:
Advantages | Disadvantages |
---|---|
More powerful than XPath, can perform very complex queries or computations | Can be slow for huge documents |
Fast enough for online processing of small- to mid-size documents |
Using SAX To Query a Document
When dealing with huge documents SAX is the only way out. The approach used to query a document is the same as when we transform or modify the document using SAX filters. We can write a filter for each query, and a combination of filters can be used for complex queries.
This SAX filter can be used to perform the query of our example:
class FilterQueryCars extends AbstractFilter
{
var $results = Array();
var $oneCar = Array();
var $prop;
function GetResults()
{
return $this->results;
}
function StartElementHandler($name, $attribs)
{
$this->prop = '';
if ($name <> "cars" && $name <> "car") {
$this->prop = $name;
$this->oneCar[$this->prop] = '';
}
}
function EndElementHandler($name)
{
if ($name == "car") {
if (((trim($this->oneCar["type"])) == "F1") &&
(trim($this->oneCar["maxspeed"])>300)) {
$this->results[] = $this->oneCar;
}
$this->oneCar = Array();
}
}
function CharacterDataHandler($data)
{
if ($this->prop) {
if (!empty($data)) {
$this->oneCar[$this->prop] .= $data;
}
}
}
}
Note that the filter doesn't propagate events to a listener, so this filter has to be used at the end of the chain; query filters are usually placed at the end of the chain. For complex queries we may want to try to build the query using more than one filter. Chaining them reduces the complexity of each filter, and makes it easy to maintain them as well.
Here are some of the advantages and disadvantages of using SAX:
Advantages | Disadvantages |
---|---|
Scales very well for huge documents | No querying features, everything must be hand-made |
Resource consumption is constant for documents of any size | Some complex queries may require a lot of code |
It is fast | |
Allows streaming |
[previous] [next] |
Created: August 26, 2002
Revised: August 26, 2002
URL: https://webreference.com/programming/php/php4xml/chap10/3/6.html