- Home ›
- Amazon Web サービス入門 ›
- ページ管理 ›
- HERE
レビューの表示
商品のレビューを取得する方法を確認します。レビューを取得するにはレビューが含まれるResponseGroupプロパティの値を指定して下さい。(詳しくは「取得する情報の種類(ResponseGroup)」を参照して下さい)。
取得したデータの中でレビューに関するデータは「Item」要素の中に含まれています。
<CustomerReviews>
<AverageRating>4.0</AverageRating>
<TotalReviews>12</TotalReviews>
<TotalReviewPages>3</TotalReviewPages>
<Review>
<ASIN>4797339535</ASIN>
<Rating>4</Rating>
<HelpfulVotes>2</HelpfulVotes>
<CustomerId>AYNFYQIJXKZMA</CustomerId>
<Reviewer>
<CustomerId>AYNFYQIJXKZMA</CustomerId>
<Name>実務開発者</Name>
</Reviewer>
<TotalVotes>4</TotalVotes>
<Date>2009-06-12</Date>
<Summary>JAVA言語入門の上巻として最適な一冊</Summary>
<Content>Javaの言語解説としてこれほど正確かつ分かり易く(以下略)</Content>
</Review>
<Review>
...
</Review>
<Review>
...
</Review>
<Review>
...
</Review>
<Review>
...
</Review>
</CustomerReviews>
1つの商品に対して複数のレビューが投稿されている場合、「CustomerReviews」要素には複数の「Review」要素が含まれています。内には含まれている場合があります。各要素の意味は次の通りです。
| 要素名 | 内容 |
|---|---|
| AverageRating | おすすめ度の平均 |
| TotalReviews | レビュー数 |
| TotalReviewPages | レビューのページ数(1ページあたり最大で5つのレビュー) |
| Rating | レビューのおすすめ度 |
| HelpfulVotes | 「このレビューが参考になった」人の数 |
| TotalVotes | レビューの評価者数 |
| Date | レビューの投稿日 |
| Summary | レビューのサマリー |
| Content | レビュー内容全文 |
今回はレビュー全体に関しては「AverageRating」要素、個々のレビューについては「Rating」要素と「Summary」要素を取得してみます。
<xsl:template match="aws:ItemSearchResponse/aws:Items/aws:Item"> <p> [おすすめ度] <xsl:value-of select="aws:CustomerReviews/aws:AverageRating" /><br /> </p> <xsl:apply-templates select="aws:CustomerReviews/aws:Review"/> </xsl:template> <xsl:template match="aws:CustomerReviews/aws:Review"> <p> [評価:<xsl:value-of select="aws:Rating" />] <xsl:value-of select="aws:Summary" /> </p> </xsl:template>
今回はレビューの数だけ繰り返しを行いますので「Review」要素に対するテンプレートルールを別途記述してあります。
サンプル
では実際に試してみます。まず次のようなXSLTスタイルシートを用意します。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2009-07-01"
version="1.0">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html lang="ja">
<head>
<title>XSLサンプル</title>
</head>
<body>
<xsl:apply-templates select="aws:ItemSearchResponse/aws:Items/aws:Item"/>
</body>
</html>
</xsl:template>
<xsl:template match="aws:ItemSearchResponse/aws:Items/aws:Item">
<p>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:URL" />
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:Width" />
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:Height" />
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="aws:ItemAttributes/aws:Title" />
</xsl:attribute>
</xsl:element>
</p>
<p>
[おすすめ度] <xsl:value-of select="aws:CustomerReviews/aws:AverageRating" /><br />
</p>
<xsl:apply-templates select="aws:CustomerReviews/aws:Review"/>
</xsl:template>
<xsl:template match="aws:CustomerReviews/aws:Review">
<p>
[評価:<xsl:value-of select="aws:Rating" />]
<xsl:value-of select="aws:Summary" />
</p>
</xsl:template>
</xsl:stylesheet>
次のようなリクエストを送信します。(「Style」パラメータにはXSLTスタイルシートを設置したURLを指定して下さい)。
http://xml-jp.amznxslt.com/onca/xml? Service=AWSECommerceService &AWSAccessKeyId=[AccessKey] &Version=2009-07-01 &ResponseGroup=Small,Images,Reviews &Operation=ItemSearch &SearchIndex=Books &Keywords=Java &ContentType=text/html &Style=http://www.example.com/ecs9-1.xsl
結果として取得したXMLデータがXSLTスタイルシートによって変換され次のように出力されます。
( Written by Tatsuo Ikura )
AjaxTower