XSLT - transform two different XML files into one without duplication of elements

ephargha

New Member
I have two different XML files: movies.xml and Amazon Product Advertising API XMLThis is my XSLT file that transforms both XML files:\[code\]<?xml version="1.0" encoding="UTF-8"?><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: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:apply-templates select="aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title"/> </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><xsl:template match="aws:Title"> <xsl:element name="h2">Amazon Movie 1</xsl:element> <xsl:value-of select="." /></xsl:template></xsl:stylesheet>\[/code\]apply-templates select="/movies/movie - Loads a list of 5 movie titles from movies.xml and displays them as hyperlinks.apply-templates select="aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title" - Loads the movie title from Amazon API xml file and displays it as plain text.This is my HTML output:\[code\]<html> <head> <h2>Movies list</h2> </head> <body> <a href="http://stackoverflow.com/questions/15559241/movie_details.php?movieID=1">The Dark Knight Rises</a> <br/> <a href="http://stackoverflow.com/questions/15559241/movie_details.php?movieID=2">Lawless</a> <br/> <a href="http://stackoverflow.com/questions/15559241/movie_details.php?movieID=3">Inception</a> <br/> <a href="http://stackoverflow.com/questions/15559241/movie_details.php?movieID=4">Looper</a> <br/> <a href="http://stackoverflow.com/questions/15559241/movie_details.php?movieID=5">Django Unchained</a> <br/> </body></html><html> <head> <h2>Movies list</h2> </head> <body><h2>Amazon Movie 1</h2>Dark Knight Rises [Blu-ray] [2012] [US Import]</body></html>\[/code\]As you can see these two templates are displayed in separate HTML tags which leads to the Head element "Movies list" being displayed twice for both templates. If I could somehow put both of these templates into a single HTML element, so nothing would be duplicated. I've tried to do it several ways but have not succeeded yet.This is my PHP file that creates two XML files and one XSLT stylesheet. It also generates a signed URL to the amazon product.\[code\]include('aws_signed_request.php');$public_key = 'XXXXXXXXXXXXXXXXXXXXXX';$private_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';$associate_tag = 'xxxxxxxxxxxxxxxxxxxxxxx';// generate signed URL$request = aws_signed_request('co.uk', array( 'Operation' => 'ItemLookup', 'ItemId' => 'B004LWZWGK', 'ResponseGroup' => 'Small'), $public_key, $private_key, $associate_tag);// do request (you could also use curl etc.)$response = @file_get_contents($request);if ($response === FALSE) { echo "Request failed.\n";} else { // parse XML $xml = new DOMDocument(); $xml->load('movies.xml'); $xml2 = new DOMDocument(); $xml2->loadXML($response); $xsl = new DOMDocument; $xsl->load('movies_list.xsl'); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); echo $proc->transformToXML($xml2);}\[/code\]movies.xml\[code\]<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="http://stackoverflow.com/questions/15559241/index.xsl"?><moviesxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="movies.xsd"><movie movieID="1"> <title>The Dark Knight Rises</title></movie><movie movieID="2"> <title>Lawless</title></movie><movie movieID="3"> <title>Inception</title></movie><movie movieID="4"> <title>Looper</title></movie><movie movieID="5"> <title>Django Unchained</title></movie></movies>\[/code\]Amazon product advertising API XML\[code\]<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"> <OperationRequest> <HTTPHeaders> <Header Name="UserAgent" Value="http://stackoverflow.com/questions/15559241/Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22"/> </HTTPHeaders> <RequestId>a1138e89-4335-4650-80f2-641e3c58b623</RequestId> <Arguments> <Argument Name="Operation" Value="http://stackoverflow.com/questions/15559241/ItemLookup"/> <Argument Name="Service" Value="http://stackoverflow.com/questions/15559241/AWSECommerceService"/> <Argument Name="Signature" Value="http://stackoverflow.com/questions/15559241/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/> <Argument Name="AssociateTag" Value="http://stackoverflow.com/questions/15559241/xxxxxxxxxxxxxx"/> <Argument Name="Version" Value="http://stackoverflow.com/questions/15559241/2011-08-01"/> <Argument Name="ItemId" Value="http://stackoverflow.com/questions/15559241/B004LWZWGK"/> <Argument Name="AWSAccessKeyId" Value="http://stackoverflow.com/questions/15559241/xxxxxxxxxxxxxxxxxxxx"/> <Argument Name="Timestamp" Value="http://stackoverflow.com/questions/15559241/2013-03-21T13:56:55.000Z"/> <Argument Name="ResponseGroup" Value="http://stackoverflow.com/questions/15559241/Small"/> </Arguments> <RequestProcessingTime>0.0189320000000000</RequestProcessingTime> </OperationRequest> <Items> <Item> <ItemAttributes> <Title> The Dark Knight Rises (Blu-ray/DVD Combo+UltraViolet Digital Copy) </Title> </ItemAttributes> </Item> </Items></ItemLookupResponse>\[/code\]
 
Back
Top