0

我正在尝试将我的 XSLT 样式表应用于 Amazon API xml 输出并在页面上显示电影标题

这是我的 XSLT 文件 (amazon.xsl):

<?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">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" media-type="text/html"/>

<xsl:template match="/">
    <xml>
        <root>
            <xsl:apply-templates select="aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title" />
        </root>
        </xml>
</xsl:template>

<xsl:template match="aws:Title">
    <xsl:text>Movie title</xsl:text>
    <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

这是亚马逊 API XML 输出:

<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
   <OperationRequest>
       <HTTPHeaders>
          <Header Name="UserAgent" Value="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="ItemLookup"/>
    <Argument Name="Service" Value="AWSECommerceService"/>
    <Argument Name="Signature" Value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
    <Argument Name="AssociateTag" Value="xxxxxxxxxxxxxx"/>
    <Argument Name="Version" Value="2011-08-01"/>
    <Argument Name="ItemId" Value="B004LWZWGK"/>
    <Argument Name="AWSAccessKeyId" Value="xxxxxxxxxxxxxxxxxxxx"/>
    <Argument Name="Timestamp" Value="2013-03-21T13:56:55.000Z"/>
    <Argument Name="ResponseGroup" Value="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>

我找到并使用了以下 PHP 代码来搜索电影标题并将其显示在页面上。但是,它仅以纯文本形式显示。我想对其应用 XSLT 样式,但我不知道如何正确地将其包含在我的 php 代码中。

如果正确使用 XSLT,它将在实际标题上方显示“电影标题”。

亚马逊.php

include('aws_signed_request.php');

$public_key = 'XXXXXXXXXXXXXXXXXXXX';
$private_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$associate_tag = 'xxxxxxxxxxxxxxxxxxxxxxxxx';

// 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
    $xsl = new DOMDocument;
    $xsl->load('amazon.xsl');

    $proc = new XSLTProcessor();
    $proc->importStyleSheet($xsl);

    $doc = DOMDocument::loadXML($response);
    $transform = $proc->transformToXML($doc);
    $pxml = simplexml_load_string($transform);

    if ($pxml === FALSE) {
        echo "Response could not be parsed.\n";
    } else {
        if (isset($pxml->Items->Item->ItemAttributes->Title)) {
            echo $pxml->Items->Item->ItemAttributes->Title, "\n";
        }
    }

}

*aws_signed_request.php*

function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag=NULL, $version='2011-08-01')
{    
    /*
    Parameters:
        $region - the Amazon(r) region (ca,com,co.uk,de,fr,co.jp)
        $params - an array of parameters, eg. array("Operation"=>"ItemLookup",
                        "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
        $public_key - your "Access Key ID"
        $private_key - your "Secret Access Key"
        $version (optional)
    */

    // some paramters
    $method = 'GET';
    $host = 'webservices.amazon.'.$region;
    $uri = '/onca/xml';

    // additional parameters
    $params['Service'] = 'AWSECommerceService';
    $params['AWSAccessKeyId'] = $public_key;
    // GMT timestamp
    $params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
    // API version
    $params['Version'] = $version;
    if ($associate_tag !== NULL) {
        $params['AssociateTag'] = $associate_tag;
    }

    // sort the parameters
    ksort($params);

    // create the canonicalized query
    $canonicalized_query = array();
    foreach ($params as $param=>$value)
    {
        $param = str_replace('%7E', '~', rawurlencode($param));
        $value = str_replace('%7E', '~', rawurlencode($value));
        $canonicalized_query[] = $param.'='.$value;
    }
    $canonicalized_query = implode('&', $canonicalized_query);

    // create the string to sign
    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;

    // calculate HMAC with SHA256 and base64-encoding
    $signature = base64_encode(hash_hmac('sha256', $string_to_sign, $private_key, TRUE));

    // encode the signature for the request
    $signature = str_replace('%7E', '~', rawurlencode($signature));

    // create request
    $request = 'http://'.$host.$uri.'?'.$canonicalized_query.'&Signature='.$signature;

    return $request;
}

我知道这是很多代码,但是 PHP 中的所有内容都可以正常工作,并且我确实在页面上显示了电影标题。我只想将我的 XSLT 样式表也应用到它。我对 PHP 很陌生,无法弄清楚如何正确地做到这一点。

4

2 回答 2

1

您已经创建了处理器并在第 6 行和第 7 行导入了样式表,amazon.php但您仍然需要使用XSLTProcessor::transformToXML

// parse XML
$doc = DOMDocument::loadXML($response);
$transform = $proc->transformToXML($doc);
$pxml = simplexml_load_string($transform);
于 2013-03-21T14:50:33.303 回答
1

要从评论中回答您的问题,以下是如何从该输入生成 HTML 页面:

<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="html" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
      <html>
        <head>
          <title></title>
        </head>
        <body>
          <xsl:apply-templates />
        </body>
      </html>
    </xsl:template>

  <xsl:template match="aws:Title">
    <h1>
      <xsl:text>Movie Title</xsl:text>
    </h1>
    <xsl:value-of select="." />    
  </xsl:template>
  <xsl:template match="text()" />
</xsl:stylesheet>

在您的示例输入上运行时,这会产生:

<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
  </head>
  <body>
    <h1>Movie Title</h1>
          The Dark Knight Rises (Blu-ray/DVD Combo+UltraViolet Digital Copy)
        </body>
</html>

如果您只想生成一个 HTML 片段以添加到您正在显示的较大页面中,您可以使用以下命令:

<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="html" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="aws:Title">
    <div>
      <h1>
        <xsl:text>Movie Title</xsl:text>
      </h1>
      <xsl:value-of select="." />
    </div>
  </xsl:template>
  <xsl:template match="text()" />
</xsl:stylesheet>

产生:

<h1>Movie Title</h1>
      The Dark Knight Rises (Blu-ray/DVD Combo+UltraViolet Digital Copy)
于 2013-03-21T15:53:28.737 回答