du548397507 2013-03-18 20:26
浏览 52
已采纳

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

  • 写回答

1条回答 默认 最新

  • duanchi5078 2013-03-18 20:36
    关注

    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 );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog