使用PHP的XSLT显示参数

I am passing the parameter movieID in the following XSLT code

<xsl:template match="movie">
  <xsl:element name="a">
  <xsl:attribute name="href">movie_details.php?movieID=<xsl:value-of select="@movieID"/></xsl:attribute>
  <xsl:value-of select="title"/>
  </xsl:element>
  <xsl:element name="br" />
</xsl:template>

I want to pass and display it on the page called movie_details.php.

This is my movie_details.php code:

<?php
$xml = new DOMDocument();
$xml->load('movies.xml');

$xsl = new DOMDocument;
$xsl->load('movie_details.xsl');

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

$params = $_GET['movieID'];

echo $proc->transformToXML($xml,$params);
?>

movie_details.xsl page contains the following parameter at the top:

<xsl:param name="movieID"/>

I get a blank page with no information displayed at all.

I am able to get it to work by using the following ColdFusion code (movie_details.cfm)

<cfset MyXmlFile = Expandpath("movies.xml")>
<cffile action="READ" variable="xmlInput"  file="#MyXmlFile#">
<cfset MyXslFile = Expandpath("movie_details.xsl")>
<cffile action="READ" variable="xslInput"  file="#MyXslFile#">

<cfset xslParam = StructNew() >
<cfset xslParam["movieID"] = "#url.movieID#" >

<cfset xmlOutput = XMLTransform(xmlInput, xslInput, xslParam )>
<!--- data is output --->
<cfcontent type="text/html" reset="yes">
<cfoutput>#xmloutput#</cfoutput>

However, I want to do the same with PHP.

Issues:

  • Parameter name
  • Passing parameters to transformer

Parameter Name

Use $movieID (instead of @movieID):

<xsl:stylesheet>
<xsl:param name="movieID" />

<xsl:template match="movie">
  <xsl:element name="a">
  <xsl:attribute name="href">movie_details.php?movieID=<xsl:value-of select="$movieID"/></xsl:attribute>
  <xsl:value-of select="title"/>
  </xsl:element>
  <xsl:element name="br" />
</xsl:template>

</xsl:stylesheet>

Passing Parameters

You will have to change your PHP code to call setParameter because transformToXML does not take additional parameters.

<?php
$xml = new DOMDocument();
$xml->load('movies.xml');

$xsl = new DOMDocument;
$xsl->load('movie_details.xsl');

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

$params = $_GET['movieID'];
$proc->setParameter('', 'movieID', $params );

echo $proc->transformToXML( $xml );
?>