<?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/2007-02-22"
>
  <xsl:output method="html" encoding="UTF-8" media-type="text/html"/>
  <xsl:param name="honBaseUrl">http://hon.jp/rest/1.0/</xsl:param>

  <xsl:variable name="pageTitle">hon.jp - RESTサービスの利用例</xsl:variable>
  <xsl:variable name="thisPageNo">
    <xsl:choose>
      <xsl:when test="/ProductInfo/request/args/arg[@name='page']/@value">
        <xsl:value-of select="/ProductInfo/request/args/arg[@name='page']/@value"/>
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="totalPages" select="/ProductInfo/totalPages"/>
  <xsl:variable name="xsltUrl">
    <xsl:choose>
      <xsl:when test="/ProductInfo/request/args/arg[@name='xslt']/@value">
        <xsl:value-of select="/ProductInfo/request/args/arg[@name='xslt']/@value"/>
      </xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-type"  content="text/html;charset=UTF-8" ></meta>
        <title><xsl:value-of select="$pageTitle"/></title>
        <style>
         .header   { background-color:darkkhaki; text-align:right; font-size:x-small; }
         .footer   { background-color:darkkhaki; text-align:right; font-size:x-small; }
         .title    { font-size:large; font-weight:bold; }
         .ProdInfo { background-color:lightyellow; }
         .request  { background-color:skyblue; margin-top:1em; }
         .totals   { background-color:cyan; font-size:large; font-weight:bold; margin-top:1em; }
         .pageInd  { background-color:lightgreen; text-align:right; }
         .xmlReq   { text-align:right; font-size:small; }
         .error    { background-color:orange; }
         th        { background-color:mediumturquoise; }
         td th     { background-color:cyan; }
         td        { background-color:aquamarine; }
         td td     { background-color:lightcyan; }
         form      { display:inline; margin:0; }
         .callAwsXML  { background-color:darkslategray; color:white; }
         .callAwsXSLT { background-color:teal; color:white; }
         .label    { color:navy; font-size:small; }
         .value    { color:brown; font-size:small; font-weight:bold; }
         .label1   { color:navy; font-size:large; }
         .value1   { color:brown; font-size:large; font-weight:bold; }
         .label2   { color:navy; font-size:medium; }
         .value2   { color:black; font-size:medium; font-weight:bold; }
         .label3   { color:navy; font-size:small; }
         .value3   { color:darkgreen; font-size:small; font-weight:bold; }
         .bookBody   { background-color:lightblue; margin:0.5em 0; }
         .bookTitle  { color:brown; font-size:large; font-weight:bold; }
         .bookInfo   { background-color:aqua; margin-left:1em;
                       color:brown; font-size:medium; font-weight:bold; }
         .bookDetail { background-color:lightblue; margin-left:1em; }
        </style>
      </head>
      <body>
        <xsl:call-template name="header"/>
        <xsl:apply-templates select="ProductInfo"/>
        <xsl:call-template name="footer"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="ProductInfo">
    <div class="ProdInfo">
      <div class="title"><xsl:value-of select="$pageTitle"/></div>

      <xsl:call-template name="dispMsgStyle"/>

      <xsl:call-template name="request"/>

      <xsl:choose>
      <xsl:when test="error_msg">
        <xsl:apply-templates select="error_msg"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$totalPages &gt; 1">
          <div class="pageInd">
            <xsl:call-template name="pageIndicator">
              <xsl:with-param name="pageNo" select="$thisPageNo"/>
            </xsl:call-template>
          </div>
        </xsl:if>
        <xsl:call-template name="results"/>
        <xsl:if test="$totalPages &gt; 1">
          <div class="pageInd">
            <xsl:call-template name="pageIndicator">
              <xsl:with-param name="pageNo" select="$thisPageNo"/>
            </xsl:call-template>
          </div>
        </xsl:if>
      </xsl:otherwise>
      </xsl:choose>

    </div>
  </xsl:template>

  <xsl:template name="request">
    <div class="request">

      <div class="title">
        <span class="label">検索文字列：</span>
        <span class="value"><xsl:value-of select="request/args/arg[@name='keyword']/@value"/></span>
      </div>

      <div class="title">
        <span class="label">見つかった書籍数：</span>
        <span class="value"><xsl:value-of select="totalResults"/></span>
        <span class="label">冊， </span>
        <span class="label">総ページ数：</span>
        <span class="value"><xsl:value-of select="$totalPages"/></span>
        <span class="label">ページ， </span>
        <span class="label">表示最大件数：</span>
        <span class="value"><xsl:value-of select="request/args/arg[@name='max']/@value"/></span>
      </div>

    </div>
  </xsl:template>

  <xsl:template name="results">
    <div>
      <xsl:apply-templates select="results"/>
    </div>
  </xsl:template>

  <xsl:template match="results">
    <div class="bookBody">
      
      <xsl:call-template name="bookTitleLine"/>
      
      <xsl:call-template name="bookInfoLine"/>
      
      <xsl:call-template name="bookDetail"/>
      
    </div>
  </xsl:template>

  <xsl:template name="bookTitleLine">
      <div class="bookTitle">
        <span class="label1">書籍【</span>
        <span class="value1"><xsl:value-of select="position()"/></span>
        <span class="label1">】</span>
        <span title="{title_kana}">
          <a href="{url}">
            <xsl:value-of select="title"/>
          </a>
        </span>
      </div>
  </xsl:template>

  <xsl:template name="bookInfoLine">
      <div class="bookInfo">
        <span class="label2">著者：</span>
        <span class="value2" title="{creator_kana}">
          <xsl:value-of select="creator"/>
        </span>
        <span class="label2">，発行者：</span>
        <span class="value2" title="id:{publisher/@id}">
          <xsl:value-of select="publisher"/>
        </span>
        <span class="label2">，発行日：</span>
        <span class="value2" title="{issue_date_rfc822}">
          <xsl:value-of select="issue_date"/>
        </span>
        <span class="label2">，ISBN：</span>
        <span class="value2">
          <xsl:value-of select="sourceISBN_10"/>
        </span>
        <span class="label2">，ジャンル：</span>
        <span class="value2" title="ジャンル1:{type/type1/@id}">
          <xsl:value-of select="type/type1"/>
        </span>
        <xsl:if test="type/type2/@id">
          <span class="label2">，</span>
          <span class="value2" title="ジャンル2:{type/type2/@id}">
            <xsl:value-of select="type/type2"/>
          </span>
        </xsl:if>
        <xsl:if test="type/type3/@id">
          <span class="label2">，</span>
          <span class="value2" title="ジャンル3:{type/type3/@id}">
            <xsl:value-of select="type/type3"/>
          </span>
        </xsl:if>
      </div>
  </xsl:template>

  <xsl:template name="bookDetail">
      <div class="bookDetail">
        <table>
          <tr>
            <th rowspan="2"><span class="label3">No</span></th>
            <th rowspan="2"><span class="label3">電子書店</span></th>
            <th rowspan="2"><span class="label3">決済方法</span></th>
            <th rowspan="2"><span class="label3">発売日</span></th>
            <th colspan="3"><span class="label3">プラットフォーム</span></th>
          </tr>
          <tr>
            <th><span class="label3">1</span></th>
            <th><span class="label3">2</span></th>
            <th><span class="label3">3</span></th>
          </tr>
          <xsl:for-each select="details">
            <tr>
              <td>
              <span title="{hon_jp_cd}">
                <a href="{url}">
                  <span>【</span>
                  <span><xsl:value-of select="position()"/></span>
                  <span>】</span>
                </a>
              </span>
              </td>
              <td>
              
              <span class="value3" title="電子書店id:{shop/@id}">
                <xsl:value-of select="shop"/>
              </span>
              </td>
              <td>
              
              <span class="value3" title="決済方法id:{point/@id}">
                <xsl:value-of select="point"/>
              </span>
              </td>
              <td>
              
              <span class="value3" title="発売日:{sale_date_rfc822}">
                <xsl:value-of select="sale_date"/>
              </span>
              </td>
              <td>
                <span class="value3" title="プラットフォーム1id:{format1/@id}">
                  <xsl:value-of select="format1"/>
                </span>
              </td>
              <td>
                <span class="value3" title="プラットフォーム2id:{format2/@id}">
                  <xsl:value-of select="format2"/>
                </span>
              </td>
              <td>
                <span class="value3" title="プラットフォーム3id:{format3/@id}">
                  <xsl:value-of select="format3"/>
                </span>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </div>
  </xsl:template>

  <xsl:template name="pageIndicator">
    <xsl:param name="pageNo"/>

    <xsl:if test="$pageNo &gt;2">
      <a title="先頭ページへ">
        <xsl:attribute name="href">
          <xsl:call-template name="genRestUrl4RequestPageNo">
            <xsl:with-param name="pageNo" select="1"/>
          </xsl:call-template>
        </xsl:attribute>
        <span>先頭</span>
      </a>
      <xsl:text>&lt;&lt; </xsl:text>
    </xsl:if>
    <xsl:if test="$pageNo &gt;1">
      <a>
        <xsl:attribute name="title">
          <xsl:text>前(</xsl:text>
          <xsl:value-of select="$pageNo - 1"/>
          <xsl:text>)ページ</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="href">
          <xsl:call-template name="genRestUrl4RequestPageNo">
            <xsl:with-param name="pageNo" select="$pageNo - 1"/>
          </xsl:call-template>
        </xsl:attribute>
        <span>前</span>
      </a>
      <xsl:text>&lt; </xsl:text>
    </xsl:if>
    <xsl:value-of  select="$pageNo"/>
    <xsl:if test="($totalPages &gt; 1) and ($pageNo &lt; $totalPages)">
      <xsl:text> &gt;</xsl:text>
      <a>
        <xsl:attribute name="title">
          <xsl:text>次(</xsl:text>
          <xsl:value-of select="$pageNo + 1"/>
          <xsl:text>)ページ</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="href">
          <xsl:call-template name="genRestUrl4RequestPageNo">
            <xsl:with-param name="pageNo" select="$pageNo + 1"/>
          </xsl:call-template>
        </xsl:attribute>
        <span>次</span>
      </a>
    </xsl:if>
    <xsl:if test="($totalPages &gt; 2) and ($pageNo &lt; $totalPages - 1)">
      <xsl:text> &gt;&gt;</xsl:text>
      <a>
        <xsl:attribute name="title">
          <xsl:text>最終(</xsl:text>
          <xsl:value-of select="$totalPages"/>
          <xsl:text>)ページ</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="href">
          <xsl:call-template name="genRestUrl4RequestPageNo">
            <xsl:with-param name="pageNo" select="$totalPages"/>
          </xsl:call-template>
        </xsl:attribute>
        <span>最終</span>
      </a>
    </xsl:if>
  </xsl:template>

  <xsl:template name="dispMsgStyle">
    <div class="xmlReq">
      <a>
        <xsl:attribute name="href">
          <xsl:call-template name="genRestUrl4RequestPageNo">
            <xsl:with-param name="pageNo" select="$thisPageNo"/>
            <xsl:with-param name="xslt">xml</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text>応答メッセージ(XML)</xsl:text>
      </a>
      <xsl:if test="$xsltUrl != ''">
        <a href="{$xsltUrl}">スタイルシート(XSLT)</a>
      </xsl:if>
    </div>
  </xsl:template>

  <xsl:template name="genRestUrl4RequestPageNo">
    <xsl:param name="pageNo">1</xsl:param>
    <xsl:param name="xslt"/>

    <xsl:variable name="request" select="/ProductInfo/request"/>
    <xsl:if test="$request">
      <xsl:value-of select="$honBaseUrl"/>
      <xsl:value-of select="$request/args/arg[@name='keyword']/@value"/>
      <xsl:text>/</xsl:text>
      <xsl:value-of select="$request/args/arg[@name='uid']/@value"/>
      <xsl:text>/</xsl:text>
      <xsl:for-each select="$request/args/arg">
        <xsl:choose>
          <xsl:when test="@name='xslt'"/>
          <xsl:when test="@name='UserAgent'"/>
          <xsl:when test="@name='page'"/>
          <xsl:when test="@name='keyword'"/>
          <xsl:when test="@name='uid'"/>
          <xsl:otherwise>
            <xsl:text>&amp;</xsl:text>
            <xsl:value-of select="@name"/>
            <xsl:text>=</xsl:text>
            <xsl:value-of select="@value"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <xsl:text>&amp;page=</xsl:text>
      <xsl:value-of select="$pageNo"/>
      <xsl:choose>
        <xsl:when test="$xslt">
          <xsl:text>&amp;</xsl:text>
          <xsl:text>xslt=</xsl:text>
          <xsl:value-of select="$xslt"/>
        </xsl:when>
        <xsl:when test="$xsltUrl">
          <xsl:text>&amp;</xsl:text>
          <xsl:text>xslt=</xsl:text>
          <xsl:value-of select="$xsltUrl"/>
        </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
      </xsl:choose>
      
    </xsl:if>
  </xsl:template>

  <xsl:template match="error_msg">
    <div class="error">
      <div><xsl:value-of select="."/></div>
    </div>
  </xsl:template>

  <xsl:template match="error">
    <div class="error">
      <div><xsl:value-of select="errorMsg"/></div>
    </div>
  </xsl:template>

  <xsl:template name="header">
    <div class="header">
      <span>
        <xsl:text>このページは，</xsl:text>
        <a href="http://hon.jp/doc/about_rest.html">hon.jpのRESTサービス</a>
        <xsl:text>を利用しています。</xsl:text>
      </span>
    </div>
  </xsl:template>

  <xsl:template name="footer">
    <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>
      </span>
    </div>
  </xsl:template>

</xsl:stylesheet>
