我有以下movies.xml
文件存储电影标题和亚马逊API ItemId for dvd和bluray:
<movies
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="movies.xsd">
<movie movieID="1">
<title>The Dark Knight</title>
<amazon>
<dvd>B004Q9SZGC</dvd>
<bluray>B004Q9T6CO</bluray>
</amazon>
</movie>
<movie movieID="2">
<title>Lawless</title>
<amazon>
<dvd>B008OPZ83C</dvd>
<bluray>B008OPZD8C</bluray>
</amazon>
</movie>
</movies>
movies_list.xsl
将电影标题显示为超链接,将用户带到他们可以看到的movie_details
页面
他们点击过的电影名称及其相应的亚马逊DVD和蓝光产品信息:
movies_list.xsl
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="head">
<xsl:element name="h2">Movies list</xsl:element>
</xsl:element>
<xsl:element name="body">
<xsl:apply-templates select="movies/movie"/>
</xsl:element>
</xsl:element>
</xsl:template>
<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
查找并显示来自Amazon API XML的电影信息(dvd和bluray):
$movies = file_get_contents('movies.xml');
$xml = new SimpleXmlElement($movies);
$movieId = $_GET['movieID'];
$movie = $xml->xpath("/movies/movie[@movieID = '$movieId']")[0];
if($movie) {
$dvd = $movie->amazon->dvd;
$bluray = $movie->amazon->bluray;
$request = aws_signed_request('co.uk', array(
'Operation' => 'ItemLookup',
'ItemId' => $dvd.', '.$bluray,
'ResponseGroup' => 'Medium, Offers',
'MerchantId' => 'All'), $public_key, $private_key, $associate_tag);
$response = @file_get_contents($request);
$xml2 = new DOMDocument();
$xml2->loadXML($response);
$xsl = new DOMDocument;
$xsl->load('movie_details.xsl');
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);
$params = $_GET['movieID'];
$proc->setParameter('', 'movieID', $params);
echo $proc->transformToXML($xml2);
}
我没有将实际的ItemId值硬编码到数组中,而是创建了$ dvd和$ bluray变量
查找存储在本地movies.xml
文件中的dvd和蓝光的ItemId。
这是我的movie_details.xsl
文件,显示所有电影详细信息:title(来自movies.xml)和Amazon DVD和Bluray图片(来自Amazon API):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2011-08-01"
exclude-result-prefixes="aws">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="moviesXML" select="document('movies.xml')"/>
<xsl:param name="movieID"/>
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="head">
<title>Movie details</title>
</xsl:element>
<xsl:element name="body">
<xsl:apply-templates select="$moviesXML/movies/movie[@movieID=$movieID]" />
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="movie">
<xsl:value-of select="title"/>
<xsl:element name="br" />
<xsl:apply-templates select="../aws:ItemLookupResponse/aws:Items/aws:Item/aws:MediumImage/aws:URL"/>
</xsl:template>
<xsl:template match="aws:URL">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:element>
<xsl:element name="br" />
</xsl:template>
</xsl:stylesheet>
根据用户在movies_list
页面中点击的电影标题,应该将他带到movie_details
页面
这将显示特定的电影标题及其相应的亚马逊DVD和蓝光URL(链接到产品封面图片),后者将显示为图片。
根据用户在movie_details
页面中点击的电影标题,我确实在movies_list
页面上显示了电影标题。
但是,我不确定我需要放在哪里:
<xsl:apply-templates select="../aws:ItemLookupResponse/aws:Items/aws:Item/aws:MediumImage/aws:URL"/>
还要显示该电影的亚马逊DVD和蓝光产品信息。
我想达到的最终结果:
电影ID 2应该以相同的方式工作,除了它将显示无法无天的电影信息而不是黑暗骑士。
答案 0 :(得分:1)
您可以在这里做的一件事是存储对输入XML的根的引用:
<xsl:variable name="moviesXML" select="document('movies.xml')"/>
<xsl:variable name="inputRoot" select="/" />
然后你可以在第二个模板中执行此操作:
<xsl:template match="movie">
<xsl:value-of select="title"/>
<xsl:element name="br" />
<xsl:apply-templates
select="$inputRoot/aws:ItemLookupResponse/aws:Items
/aws:Item/aws:MediumImage/aws:URL"/>
</xsl:template>
您似乎还需要修改获取Amazon API请求的DVD和蓝光ID的方式:
$movies = file_get_contents('movies.xml');
$xml = new SimpleXmlElement($movies);
$movieId = $_GET['movieID'];
$movieSelect = $xml->xpath("/movies/movie[@movieID = '$movieId']");
$movie = $movieSelect[0];
if($movie) {
$dvd = $movie->amazon->dvd;
$bluray = $movie->amazon->bluray;
// the rest of your code
}