<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.ivoa.net/xml/VOResource/v0.10" 
                xmlns:vr="http://www.ivoa.net/xml/VOResource/v0.10" 
                xmlns:vs="http://www.ivoa.net/xml/VODataService/v0.5"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                version="1.0">

   <!--
     - Stylesheet to create a plain-text, human readable rendering of
     - a VOResource description
     -->
   <xsl:output method="text"/>

   <!--
     - Print our display envelope.  
     -->
   <xsl:template match="/">
Resource Description Record
<xsl:apply-templates select="vr:Resource" />
   </xsl:template>

   <!--
     - Render the Resource description data.  In this template, 
     - we take control of the spacing explicitly using xsl:text elements
     -->
    <xsl:template match="vr:Resource" >
       <xsl:value-of select="substring-after(@xsi:type,':')"/>
       <xsl:text>
</xsl:text>
       <xsl:value-of select="vr:title"/>
       <xsl:text> (</xsl:text>
       <xsl:value-of select="vr:shortName"/>
       <xsl:text>)
  IVOA Identifier:    </xsl:text>
       <xsl:value-of select="vr:identifier"/>

       <xsl:apply-templates select="vr:content" />
       <xsl:apply-templates select="vr:curation" />
    </xsl:template>

    <xsl:template match="vr:content">
       <xsl:apply-templates select="vr:description" />
       <xsl:text>
</xsl:text>
       
       <xsl:text>Target Communities: </xsl:text>
       <xsl:for-each select="vr:contentLevel">
           <xsl:value-of select="."/>

           <!--
             -  if this is not the last one in the list, insert a 
             -  comma
             -->
           <xsl:if test="position()!=last()">
               <xsl:text>, </xsl:text>
           </xsl:if>

       </xsl:for-each>
    </xsl:template>

    <!--
      - format the description into a nice paragraph.
      -->
    <xsl:template match="vr:description">
       <xsl:text>

Description:
</xsl:text>

       <!--
         - This named function is called like a function
         -->
       <xsl:call-template name="fmttxt">
          <xsl:with-param name="text" select="normalize-space(.)"/>
       </xsl:call-template>
       
    </xsl:template>

    <!--
      -  render the curation information
      -->
    <xsl:template match="vr:curation">
        <xsl:text>
Published by:  </xsl:text>
        <xsl:value-of select="vr:publisher"/>
    </xsl:template>

    <!-- ================================================== -->

    <!--
      - Format a long string of text in to a set of lines not longer than
      - a certain length.
      - Arguments:
      -   text   the text to format
      -   width  the maximum number of characters in a line (default=75)
      -   pre    a string to insert in front of each line (default: empty)
      -->   
    <xsl:template name="fmttxt">
       <xsl:param name="text"/>
       <xsl:param name="width" select="75"/>
       <xsl:param name="pre"></xsl:param>

       <xsl:choose>
          <xsl:when test="string-length($text) &gt; $width">

             <!-- input is longer than one line.  First, lop off and print 
                  the first line.  -->
             <xsl:variable name="cutpoint">
                <xsl:call-template name="indexOfLast">
                   <xsl:with-param name="text" 
                                   select="substring($text, 1, $width)"/>
                </xsl:call-template>
             </xsl:variable>

             <xsl:value-of select="$pre"/>
             <xsl:value-of select="substring($text, 1, number($cutpoint)-1)"/>
             <xsl:text>
</xsl:text>

             <xsl:if test="number($cutpoint) &lt; string-length($text)">
             <!-- now recurse on the remaining text -->
                <xsl:call-template name="fmttxt">
                   <xsl:with-param name="text" 
                                 select="substring($text,number($cutpoint)+1)"/>
                   <xsl:with-param name="width" select="$width"/>
                   <xsl:with-param name="pre" select="$pre"/>
                </xsl:call-template>
             </xsl:if>
          </xsl:when>

          <xsl:otherwise>
             <!-- input line is less than max width, so just print it -->
             <xsl:value-of select="$pre"/>
             <xsl:value-of select="$text"/>
             <xsl:text>
</xsl:text>
           
        </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

  <xsl:template name="indexOfLast">
     <xsl:param name="text"/>
     <xsl:param name="pat" select="' '"/>
     <xsl:param name="sum" select="0"/>
     <xsl:param name="patlen" select="0"/>

     <xsl:choose>
        <xsl:when test="contains($text,$pat)">
           <xsl:variable name="pre" select="substring-before($text, $pat)"/>
           <xsl:variable name="post" select="substring-after($text, $pat)"/>
           <xsl:variable name="newsum" 
                select="$sum + number($patlen) + string-length($pre)"/>
           <xsl:call-template name="indexOfLast">
              <xsl:with-param name="text" select="$post"/>
              <xsl:with-param name="pat" select="$pat"/>
              <xsl:with-param name="sum" select="$newsum"/>
              <xsl:with-param name="patlen" select="string-length($pat)"/>
           </xsl:call-template>
        </xsl:when>
        <xsl:when test="$sum=0">
           <xsl:value-of select="string-length($text)+1"/>
        </xsl:when>
        <xsl:otherwise>
           <xsl:value-of select="$sum+1"/>
        </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

</xsl:stylesheet>