1

movieID在以下 XSLT 代码中传递参数

<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>

我想通过并将其显示在名为movie_details.php.

这是我的 movie_details.php 代码:

<?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 页面顶部包含以下参数:

<xsl:param name="movieID"/>

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

我可以通过使用以下 ColdFusion 代码 (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>

但是,我想对 PHP 做同样的事情。

4

1 回答 1

4

问题:

  • 参数名称
  • 将参数传递给变压器

参数名称

使用$movieID(而不是@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>

传递参数

您将不得不更改您的 PHP 代码以调用setParameter,因为transformToXML不采用其他参数。

<?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 );
?>
于 2013-03-18T20:36:08.060 回答