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>

パラメータを渡す

transformToXMLは追加のパラメータを取らないため、setParameterを呼び出すように 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'];
$proc->setParameter('', 'movieID', $params );

echo $proc->transformToXML( $xml );
?>
于 2013-03-18T20:36:08.060 に答える