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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c