Recipe 12.8. Setting XSLT Parameters from PHP

12.8.1. Problem

You want to set parameters in your XSLT stylesheet from PHP.

12.8.2. Solution

Use the XSLTProcessor::setParameter( ) method:

// This could also come from $_GET['city']; $city = 'San Francisco'; $dom  = new DOMDocument $dom->load('address-book.xml'); $xsl  = new DOMDocument $xsl->load('stylesheet.xsl'); $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsl); $xslt->setParameter(NULL, 'city', $city); print $xslt->transformToXML($dom);

This code sets the XSLT city parameter to the value stored in the PHP variable $city.

12.8.3. Discussion

You can pass data from PHP into your XSLT stylesheet with the setParameter( ) method. This allows you to do things such as filter data in your stylesheet based on user input.

For example, the program in Example 12-16 allows you to find people based on their city.

Setting XSLT parameters from PHP

// This could also come from $_GET['city']; $city = 'San Francisco'; $dom  = new DOMDocument $dom->load('address-book.xml'); $xsl  = new DOMDocument $xsl->load('stylesheet.xsl'); $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsl); $xslt->setParameter(NULL, 'city', $city); print $xslt->transformToXML($dom);

The program uses the following stylesheet:

<?xml version="1.0" ?> <xsl:stylesheet version="1.0"   xmlns:xsl=""> <xsl:template match="@*|node()">   <xsl:copy>     <xsl:apply-templates select="@*|node()"/>   </xsl:copy> </xsl:template> <xsl:template match="/address-book/person">   <xsl:if test="city=$city">     <xsl:copy>       <xsl:apply-templates select="@*|node()"/>     </xsl:copy>   </xsl:if> </xsl:template> </xsl:stylesheet>

The program and stylesheet combine to produce the following results:

<?xml version="1.0"?> <address-book>     <person >         <!--Adam Trachtenberg-->         <firstname>Adam</firstname>         <lastname>Trachtenberg</lastname>         <city>San Francisco</city>         <state>CA</state>         <email></email>     </person> </address-book>

The PHP script does a standard XSLT transformation, except that it calls $xslt->setParameter(NULL, 'city', $city). The first argument is the parameter's namespace, the second is the parameter's name, and the third is the parameter's value.

Here, the value stored in the PHP variable $city'in this case, San Francisco'is assigned to the XSLT parameter city, which does not live under a namespace. This is equal to placing the following in an XSLT file:

<xsl:param name="city">San Francisco</xsl:param>

You usually access a parameter inside a stylesheet like you do a PHP variable, by placing a dollar sign ($) in front of its name. The stylesheet example creates a template that matches /address-book/person nodes.

Inside the template, you test whether city=$city; in other words, is the city child of the current node equal to the value of the city parameter? If there's a match, the children are copied along; otherwise, the records are eliminated.

In this case, city is set to San Francisco, so David's record is removed and Adam's remains.

12.8.4. See Also

Documentation on XSLTProcessor::setParameter at; XSLT by Doug Tidwell (O'Reilly).

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: