doumu5023
2013-04-01 18:40
浏览 43

XSLT - 显示位于不同XML文件中的子元素

I have the following movies.xml file that stores movie titles and Amazon API ItemId for dvd and 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 displays movie titles as hyperlinkes that take the user to the movie_details page where they can see the movie title they have clicked on AND its corresponding Amazon dvd and bluray product information:

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 looks up and displays movie information (dvd and bluray) from Amazon API XML:

$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);

}

Instead of hardcoding the actual ItemId values into the array, I have created $dvd and $bluray variables that look up ItemId for dvd and bluray stored in the local movies.xml file.

This is my movie_details.xsl file that displays all the movie details information: title (from movies.xml) and Amazon DVD and Bluray pictures (from 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>

Based on the movie title that the user has clicked on in the movies_list page, it should take him to the movie_details page that will display that specific movie title and its corresponding Amazon dvd and bluray URL (link to the product cover picture) which in turn will be displayed as a picture.

I do get the movie title displayed on the movie_details page based on the movie title the user has clicked on in the movies_list page.

However, I am not sure where do I need to put:

<xsl:apply-templates select="../aws:ItemLookupResponse/aws:Items/aws:Item/aws:MediumImage/aws:URL"/>

To also display that movie's Amazon DVD and bluray product information.

The end result that I want to achieve:

display

Movie ID 2 should work the same way except it will display Lawless movie information instead of the Dark Knight.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyi9447 2013-04-01 19:57
    已采纳

    One thing you can do here is store a reference to the root of the input XML:

    <xsl:variable name="moviesXML" select="document('movies.xml')"/>
    <xsl:variable name="inputRoot" select="/" />
    

    Then you can do this in your second template:

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

    It looks like you also need to modify the way you get the DVD and Blu-Ray IDs for your Amazon API request:

    $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
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题