<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:hon="urn:hon"
     xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2007-02-22"
>
  <xsl:output method="html" encoding="UTF-8" media-type="text/html"/>
  <xsl:variable name="honBaseUrl" select="/hon:hon/hon:baseUrl/hon:honBaseUrl/@href"/>

  <xsl:variable name="accessUrl">
     <xsl:value-of select="$honBaseUrl"/>
     <xsl:value-of select="/hon:hon/hon:params/hon:param[@name='keyword']/@value"/>
     <xsl:text>/</xsl:text>
     <xsl:value-of select="/hon:hon/hon:ids/hon:honUserId"/>
     <xsl:text>/</xsl:text>
     <xsl:for-each select="/hon:hon/hon:params/hon:param[@name!='keyword']">
       <xsl:if test="position()!=1">
         <xsl:text>&amp;</xsl:text>
       </xsl:if>
       <xsl:value-of select="@name"/>
       <xsl:text>=</xsl:text>
       <xsl:value-of select="@value"/>
     </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="honjpDoc" select="document($accessUrl)"/>

  <xsl:variable name="amzonIsbnCallUrlBase">
    <xsl:text>http://webservices.amazon.co.jp/onca/xml</xsl:text>
    <xsl:text>?</xsl:text><xsl:text>Service=AWSECommerceService</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>Version=2007-02-22</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>AWSAccessKeyId=0KM461ZPKCYB1JA0MX02</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>Operation=ItemLookup</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>ResponseGroup=Medium</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>IdType=ASIN</xsl:text>
    <xsl:text>&amp;</xsl:text><xsl:text>ItemId=</xsl:text>
  </xsl:variable>

  <xsl:variable name="title">
    <xsl:value-of select="/hon:hon/hon:stylesheet/hon:style/hon:description[@xml:lang='ja']"/>
  </xsl:variable>


  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-type"  content="text/html; charset=UTF-8" ></meta>
        <title><xsl:value-of select="$title"/></title>
        <style>
         .title    { font-size:large; font-weight:bold; float:none; text-align:center; background-color:antiquewhite;}
         .main     { background-color:lightcyan; text-align:center; }
         .result   { background-color:lightyellow; text-align:center; }
         .header   { background-color:darkkhaki; text-align:right; font-size:x-small; }
         .footer   { background-color:darkkhaki; text-align:right; font-size:x-small; }
         th        { background-color:mediumturquoise; }
         td        { background-color:aquamarine; }
         .main  th { background-color:tan; }
         .main  td { background-color:wheat; }
         .amazon   { background-color:palegreen; text-align:center; }
        </style>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="hon:hon">
    <div class="title"><xsl:value-of select="$title"/></div>
    <hr />
    <div class="main">
      <table border="1">
        <tr>
          <th>パラメタ名</th>
          <th>値</th>
        </tr>
        <tr>
          <td>ID値</td>
          <td><xsl:value-of select="hon:ids/hon:honUserId"/></td>
        </tr>
        <xsl:for-each select="hon:params/hon:param">
          <tr>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="@value"/></td>
          </tr>
        </xsl:for-each>
        <tr>
          <th colspan="2">アクセスポイント</th>
        </tr>
        <tr>
          <td>URL</td>
          <td>
            <a href="{$accessUrl}">
              <xsl:value-of select="$accessUrl"/>
            </a>
          </td>
        </tr>
      </table>
    </div>

    <hr />

    <div class="result">
      <xsl:for-each select="$honjpDoc">
        <xsl:apply-templates select="ProductInfo"/>
      </xsl:for-each>
    </div>

    <div class="footer">
      <span>
        <xsl:text>このページは，</xsl:text>
        <a href="http://hon.jp/doc/about_rest.html">hon.jpのRESTサービス</a>
        <xsl:text>及び</xsl:text>
        <a href="http://www.amazon.com/gp/aws/landing.html">Amazon E-Commerceサービス</a>
        <xsl:text>を利用しています。</xsl:text>
      </span>
    </div>

  </xsl:template>

  <xsl:template match="ProductInfo">
      <table>
      <xsl:for-each select="results">
         <xsl:apply-templates select=".">
           <xsl:with-param name="nbr" select="position()"/>
         </xsl:apply-templates>
      </xsl:for-each>
      </table>
  </xsl:template>
 
  <xsl:template match="results">
    <xsl:param name="nbr"/>
    <xsl:variable name="awsAccessUrl">
        <xsl:choose>
          <xsl:when test="sourceISBN_10 != ''">
            <xsl:value-of select="$amzonIsbnCallUrlBase"/>
            <xsl:value-of select="translate(sourceISBN_10,'-','')"/>
          </xsl:when>
          <xsl:otherwise/>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="ansXml" select="document($awsAccessUrl)"/>

    <tr>
      <th rowspan="2"><xsl:value-of select="$nbr"/></th>
      <td colspan="3">
        <a href="{url}">
          <xsl:value-of select="title"/>
        </a>
      </td>
      <td rowspan="2" class="amazon">
        <xsl:choose>
          <xsl:when test="$awsAccessUrl != ''">
                <xsl:variable name="imgUrl"
                    select="$ansXml/aws:ItemLookupResponse/aws:Items/aws:Item/aws:SmallImage/aws:URL        "/>
                <xsl:attribute name="title">amazon.co.jpからの情報</xsl:attribute>
                <a href="{$awsAccessUrl}">
                <xsl:choose>
                  <xsl:when test="$imgUrl != ''">
                    <img src="{$imgUrl}"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:text>画像無し</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
                </a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>ISBN無し</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>
    <tr>
      <td><xsl:value-of select="creator"/></td>
      <td><xsl:value-of select="sourceISBN_10"/></td>
      <td><xsl:value-of select="issue_date"/></td>
    </tr>
  </xsl:template>
 
</xsl:stylesheet>
