使用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.

我在以下XSLT代码中传递参数 movieID code> p> &lt; xsl:template match =“movie”&gt; &lt; xsl:element name =“a”&gt; &lt; xsl:attribute name =“href”&gt; movie_details。 php?movieID =&lt; xsl:value-of select =“@ movieID”/&gt;&lt; / xsl:attribute&gt; &lt; xsl:value-of select =“title”/&gt; &lt; / xsl :element&gt; &lt; xsl:element name =“br”/&gt; &lt; / xsl:template&gt; code> pre>

我想传递并显示它 在名为 movie_details.php code>的页面上。 p>

这是我的movie_details.php代码: p>

 &lt;?php 
 $ xml = new DOMDocument(); 
 $  xml-&gt; load('movies.xml'); 
 
 $ xsl = new DOMDocument; 
 $ xsl-&gt; load('movie_details.xsl'); 
 
 $ proc = new XSLTProcessor()  ; 
 $ proc-&gt; importStyleSheet($ xsl); 
 
 $ params = $ _GET ['movieID']; 
 
echo $ proc-&gt; transformToXML($ xml,$ params); 
?  &gt; 
  code>  pre> 
 
 

movie_details.xsl页面顶部包含以下参数: p>

 &lt; xsl:param  name =“movieID”/&gt; 
  code>  pre> 
 
 

我得到一个空白页面,根本没有显示任何信息。 p>

我是 能够通过使用以下ColdFusion代码(movie_details.cfm) p>

 &lt; cfset MyXmlFile = Expandpath(“movies.xml”)&gt; 
&lt; cffile使其工作 action =“READ”variable =“xmlInput”file =“#MyXmlFile#”&gt; 
&lt; cfset MyXslFile = Expandpath(“movie_details.xsl”)&gt; 
&lt; cffile action =“READ”variable =“xslInput”file  =“#MyXslFile#”&gt; 
 
&lt; cfset xslParam = StructNew()&gt; 
&lt; cfset xslParam [“movieID  “] =”#url.movi​​eID#“&gt; 
 
&lt; cfset xmlOutput = XMLTransform(xmlInput,xslInput,xslParam)&gt; 
&lt;!---数据输出---&gt; 
&lt; cfcontent类型 =“text / html”reset =“yes”&gt; 
&lt; cfoutput&gt; #xmloutput#&lt; / cfoutput&gt; 
  code>  pre> 
 
 

但是,我想这样做 与PHP相同。 p> div>

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 );
?>