- Home ›
- Amazon Web サービス入門 ›
- ページ管理 ›
- HERE
Amazon詳細ページへのリンク
取得した商品に関するAmazonのページへリンクを設定する方法を確認します。
Amazonのページへのリンクは「Item」要素の中の「DetailPageURL」要素の中に記述されています。
<Item>
<ASIN>...</ASIN>
<DetailPageURL>http://www.amazon.co.jp/(以下省略)</DetailPageURL>
<ItemLinks>
...
</ItemLinks>
<ItemAttributes>
<Author>柴田 望洋</Author>
<Manufacturer>ソフトバンククリエイティブ</Manufacturer>
<ProductGroup>Book</ProductGroup>
<Title>明解Java 入門編</Title>
</ItemAttributes>
</Item>
<xsl:value-of>を使って「DetailPageURL」要素の値を取得したあと、リンクを作成します。
リンクを作成するにはいくつか方法がありますが、今回は<xsl:element>を使って指定した名前を持つ要素を作成します。書式は次の通りです。
<xsl:element name="要素名"> <!-- 内容 --> </xsl:element>
リンクを作成するにはまず「a」要素を作成します。
<xsl:element name="a"> <!-- 内容 --> </xsl:element>
次に作成した要素に対して属性を設定します。書式は次の通りです。
<xsl:element name="要素名">
<xsl:attribute name="属性名">
<!-- 属性値 -->
</xsl:attribute>
<!-- 内容 -->
</xsl:element>
「a」要素に対しては「href」属性に対してリンク先URLを設定します。
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="aws:DetailPageURL" />
</xsl:attribute>
<!-- 内容 -->
</xsl:element>
「href」属性には「DetailPageURL」要素で取得した値を設定しました。
あとはリンクを設定する対象を記述します。今回は「Title」要素の値に対してリンクを設定します。
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="aws:DetailPageURL" />
</xsl:attribute>
<xsl:value-of select="aws:ItemAttributes/aws:Title" />
</xsl:element>
以上を「Item」要素に対するテンプレートルール内に記述することで商品毎にリンクが設定されたタイトルが表示されます。
サンプル
では実際に試してみます。まず次のような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="a">
<xsl:attribute name="href">
<xsl:value-of select="aws:DetailPageURL" />
</xsl:attribute>
<xsl:value-of select="aws:ItemAttributes/aws:Title" />
</xsl:element>
[著者] <xsl:value-of select="aws:ItemAttributes/aws:Author" />
</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 &Operation=ItemSearch &SearchIndex=Books &Keywords=Java &ContentType=text/html &Style=http://www.example.com/ecs6-1.xsl
結果として取得したXMLデータがXSLTスタイルシートによって変換され次のように出力されます。
本のタイトルに設定されたリンクをクリックすると、該当商品のAmazonのページへ飛びます。
( Written by Tatsuo Ikura )
AjaxTower