[xsl] roblems with passing variables from PHP to XSLT

Subject: [xsl] roblems with passing variables from PHP to XSLT
From: tech@xxxxxxxxxxxxxxxx
Date: Tue, 06 Jun 2006 07:36:39 -0500
Hi, I'm a newcomer to XML and XSLT and to this list, so please bear with
me if anything I write seems painfully obvious or just plain daft!

I'm working with a large XML file containing data about tours in
hundreds of cities worldwide. What I'm trying to do is select (via PHP)
only the data for one city at a time. However, I'm having a nightmare of
a time trying to work out how to pass the relevant variable to the XSLT
stylesheet.

The server my site's running on is compiled with PHP 4.3.9, with domxml
support:

DOM/XML: enabled
DOM/XML API Version: 20020815
libxml Version: 20611
HTML Support, XPath Support, XPointer Support, DOM/XSLT, DOM/EXSLT: all
enabled
libxslt Version: 1.1.8
libxslt compiled against libxml Version: 2.6.11
libexslt Version 1.1.8



The XML skeleton looks like this:

   <?xml version="1.0" encoding="iso-8859-1"?>
   <XMLData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
   xsi:noNamespaceSchemaLocation="affiliate.xsd"
   CreationDate="2006-05-07T03:10:01+10:00" SchemaVersion="1.0" >
       <Currency>GBP</Currency>
       <IATA code="PEN">
           <ProductItem Type="SIC">
               <ProductType>Night Tour</ProductType>
                . . .
   <closing tags>

So it seems pretty obvious that the best way to select the data is by
using the "code" attribute of the "IATA" node.

The XSLT stylesheet is this:

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:output method="html"/>

<xsl:template match="IATA[@code='PEN']/ProductItem">
   <table width="480">
       <tr>
           <td>
               <h1><xsl:value-of
select="ProductItemDetail/ItemTitle"/></h1>
           </td>
       </tr>
       <tr>
           <td>
               <h3><xsl:value-of
select="ProductItemDetail/PriceDesciption"/></h3>
               <p><img src="{ProductItemDetail/URLList/URL/URLLink}"
alt="{ProductItemDetail/ItemTitle}" align="right" /><xsl:value-of
select="ProductItemDetail/ProductText"  disable-output-escaping="yes"/></p>
               <p>Duration: <xsl:value-of
select="ProductItemDetail/Duration"/></p>
               <p>Starts from: <xsl:value-of
select="ProductItemDetail/Commences"/></p>
               <p><a href="{ProductPageURL}4656" target="_blank">Find
out more here...</a></p>
           </td>
       </tr>
   </table>
</xsl:template>

<xsl:template match="IATA[@code!='PEN']/ProductItem">
</xsl:template>

</xsl:stylesheet>

My PHP code, so far, is this:
<?php

// Create a DomDocument object from an xml file.
if( !$domXmlObj = domxml_open_file( "data.xml" ) )
{
   die( "Cannot parse the xml file." );
}

// Create a DomDocument object from the xslt file.
$domXsltObj = domxml_xslt_stylesheet_file( "stylesheet.xsl" );

// Create a DomDocument object from the xslt transformation of the xml
and xslt file.
$domTranObj = $domXsltObj->process( $domXmlObj );

// Display the output of the DomDocument object from the xslt
transformation.
echo $domXsltObj->result_dump_mem( $domTranObj );

?>

This works fine, as far as it goes. But I've tried several different
ways of getting the PHP script to set the "code" attribute - including
by adding parameters to the "$domXsltObj->process( $domXmlObj )"
instruction and by calling a PHP function from the xslt stylesheet.
Nothing's worked so far.

I'd appreciate any pointers in the right direction - I've been banging
my head against a brick wall for days now!

Cheers

Dave McMahon

Current Thread