<?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"
>

  <xsl:output method="html" encoding="UTF-8" media-type="text/html"/>
  <xsl:variable name="xsltUrl" select="/hon:hon/hon:baseUrl/hon:xsltBaseUrl/@href"/>
  <xsl:variable name="honBaseUrl" select="/hon:hon/hon:baseUrl/hon:honBaseUrl/@href"/>

  <xsl:variable name="title">hon.jp - RESTサービスの呼び出し</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; }
         .main     { background-color:lightcyan; }
         .style    { float:left; }
         .param    { margin-top:0.5em; float:left; }
         .call     { clear:both; margin:0.5em 0.5em; text-align:center; }
         .callBtn  { background-color:darkslategray; color:white; font-size:xx-large; }
         th        { background-color:mediumturquoise; }
         td        { background-color:aquamarine; }
        </style>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="hon:hon">
    <form method="get" action="{concat($honBaseUrl,'?')}">
      <div class="title"><xsl:value-of select="$title"/></div>
      <hr />
      <div class="main">
        <xsl:call-template name="stylesheet"/>
        <xsl:call-template name="params"/>
        <xsl:call-template name="page"/>
        <div class="call">
          <input type="hidden" name="uid" value="{/hon:hon/hon:ids/hon:honUserId}"/>
          <input type="submit" value="    hon.jp呼び出し    " class="callBtn"/>
        </div>
      </div>
    </form>
  </xsl:template>

  <xsl:template name="stylesheet">
    <div class="style">
      <xsl:apply-templates select="hon:stylesheet"/>
    </div>
  </xsl:template>

  <xsl:template match="hon:stylesheet">
    <xsl:call-template name="style"/>
  </xsl:template>

  <xsl:template name="style">
    <table border="1">
      <tr>
        <th>通番</th>
        <th>スタイルシート名</th>
        <th>説明</th>
      </tr>
      
      <xsl:for-each select="hon:style">
        <xsl:variable name="style">
          <xsl:choose>
            <xsl:when test="@href">
              <xsl:choose>
                <xsl:when test="starts-with(@href,'http://')">
                  <xsl:value-of select="@href"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="concat($xsltUrl,@href)"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>xml</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:if test="@type='clientXslt'">
          <input type="hidden" name="ContentType" value="text/xml;charset=UTF-8"/>
        </xsl:if>
            
        <tr>
          <th>
            <xsl:text>【</xsl:text>
            <xsl:value-of select="position()"/>
            <xsl:text>】</xsl:text>
            <input type="radio" name="xslt" value="{$style}">
              <xsl:if test="position()=1">
                <xsl:attribute name="checked">checked</xsl:attribute>
              </xsl:if>
            </input>
          </th>
          <td>
              <xsl:choose>
                <xsl:when test="$style='xml'">
                  <xsl:text>XML形式</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <a href="{$style}">
                    <xsl:value-of select="@href"/>
                  </a>
                </xsl:otherwise>
              </xsl:choose>
          </td>
          <td>
            <xsl:apply-templates select="hon:description"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="params">
    <xsl:call-template name="param">
      <xsl:with-param name="param">keyword</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="param">
      <xsl:with-param name="param">max</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="param">
    <xsl:param name="param"/>

    <div class="param">
    <table border="1">
      <tr>
        <th>通番</th>
        <th>パラメタ：<xsl:value-of select="$param"/></th>
      </tr>
      <xsl:for-each select="hon:params/hon:param[@name=$param]/hon:data">
        <tr>
          <th>
            <xsl:text>【</xsl:text>
            <xsl:value-of select="position()"/>
            <xsl:text>】</xsl:text>
            <input type="radio" name="{$param}" value="{text()}">
              <xsl:if test="position()=1">
                <xsl:attribute name="checked">checked</xsl:attribute>
              </xsl:if>
            </input>
          </th>
          <td>
            <xsl:value-of select="text()"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
    </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: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: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 name="page">
    <div class="param">
      <table border="1">
        <tr>
          <th>頁:page</th>
        </tr>
        <tr>
          <td>
            <input type="text" name="page" value="1" size="10"/>
          </td>
        </tr>
      </table>
    </div>
  </xsl:template>

</xsl:stylesheet>
