dsfdf854456 2013-03-21 14:34
浏览 37
已采纳

将XSLT样式表应用于Amazon Product Advertising API XML输出

I am trying to apply my XSLT stylesheet to Amazon API xml output and display a movie title on the page

This is my XSLT file (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>

This is Amazon API XML output:

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

I have found and used the following PHP code to search for the movie title and display it on the page. However, it only displays it in plain text. I would like to apply an XSLT style to it but I don't know how to properly include it in my php code.

If XSLT was used correctly, it would display "Movie title" right above the actual title.

amazon.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.
";
} 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.
";
    } else {
        if (isset($pxml->Items->Item->ItemAttributes->Title)) {
            echo $pxml->Items->Item->ItemAttributes->Title, "
";
        }
    }

}

*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."
".$host."
".$uri."
".$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;
}

I know it's a lot of code, but everything in PHP works perfectly fine and I do get a movie title displayed on the page. I just want to apply my XSLT stylesheet to it as well. I am very new to PHP and cannot figure out how to do it properly.

  • 写回答

2条回答 默认 最新

  • dongyinglan8707 2013-03-21 15:53
    关注

    To answer your question from the comments, here is how you could generate an HTML page from that input:

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

    When run on your sample input, this produces:

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

    If you just want to generate an HTML snipped to add to a larger page that you are displaying, you could just use this:

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

    which produces:

    <h1>Movie Title</h1>
          The Dark Knight Rises (Blu-ray/DVD Combo+UltraViolet Digital Copy)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog