<?xml version="1.0" encoding="UTF-8" ?>

<!--
   fractions.xsl - Display value as a fraction
   Copyright (C) 2011 Matous J. Fialka, <http://mjf.cz/>
   Relesed under the terms of The MIT License

   Note: values MUST be within 0 and 0.9
-->
      
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
   <xsl:template match="/data/number">
      <xsl:call-template name="fraction">
         <xsl:with-param name="value" select="." />
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="fraction">
      <xsl:param name="value" select="0" />
      <xsl:choose>
         <xsl:when test="$value &lt; 0.47">
            <xsl:choose>
               <xsl:when test="$value &lt; 0.25">
                  <xsl:choose>
                     <xsl:when test="$value &lt; 0.16">
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.13">
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.11">
                                    <xsl:text>1/10</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>1/9</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.14">
                                    <xsl:text>1/8</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>1/7</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.19">
                              <xsl:text>1/6</xsl:text>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.22">
                                    <xsl:text>1/5</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>2/9</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:otherwise>
                  </xsl:choose>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:choose>
                     <xsl:when test="$value &lt; 0.38">
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.29">
                              <xsl:text>1/4</xsl:text>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.31">
                                    <xsl:text>2/7</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>1/3</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.43">
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.40">
                                    <xsl:text>3/8</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>2/5</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.44">
                                    <xsl:text>3/7</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>4/9</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:otherwise>
                  </xsl:choose>
               </xsl:otherwise>
            </xsl:choose>
         </xsl:when>
         <xsl:otherwise>
            <xsl:choose>
               <xsl:when test="$value &lt; 0.71">
                  <xsl:choose>
                     <xsl:when test="$value &lt; 0.60">
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.56">
                              <xsl:text>1/2</xsl:text>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.57">
                                    <xsl:text>5/9</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>4/7</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.63">
                              <xsl:text>3/5</xsl:text>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.66">
                                    <xsl:text>5/8</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>2/3</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:otherwise>
                  </xsl:choose>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:choose>
                     <xsl:when test="$value &lt; 0.80">
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.74">
                              <xsl:text>5/7</xsl:text>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.78">
                                    <xsl:text>3/4</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>7/9</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:choose>
                           <xsl:when test="$value &lt; 0.86">
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.83">
                                    <xsl:text>4/5</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:text>5/6</xsl:text>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:choose>
                                 <xsl:when test="$value &lt; 0.88">
                                    <xsl:text>6/7</xsl:text>
                                 </xsl:when>
                                 <xsl:otherwise>
                                    <xsl:choose>
                                       <xsl:when test="$value &lt; 0.89">
                                          <xsl:text>7/8</xsl:text>
                                       </xsl:when>
                                       <xsl:otherwise>
                                          <xsl:choose>
                                             <xsl:when test="$value &lt; 0.90">
                                                <xsl:text>8/9</xsl:text>
                                             </xsl:when>
                                             <xsl:otherwise>
                                                <xsl:text>9/10</xsl:text>
                                             </xsl:otherwise>
                                          </xsl:choose>
                                       </xsl:otherwise>
                                    </xsl:choose>
                                 </xsl:otherwise>
                              </xsl:choose>
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:otherwise>
                  </xsl:choose>
               </xsl:otherwise>
            </xsl:choose>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>
