<!-- This style sheet will approximate the newspaper presentation of the complete boxscore data. -->

<?xmls version="1.0" ?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="*">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="/">
  <HTML>
    <HEAD>
      <TITLE>
        Complete boxscore (newspaper-like presentation)
      </TITLE>
    </HEAD>
    <BODY>

<!-- Used the TT (typewriter text) tag to obtain a non-proportional font, standard for the table -->
<!--presentation. -->
      <TT>
      <P>
        <xsl:apply-templates/>
      </P>
      </TT>
    </BODY>
  </HTML>
</xsl:template>

<!-- Applied a heading tag to the scoreheader for a headline effect. -->

<xsl:template match="scoreheader">
  <h2>
  <xsl:apply-templates select="teamscore[@winning='winning']"/>,
  <xsl:apply-templates select="teamscore[@winning='losing']"/>
  <xsl:if test="./inningsnote">,</xsl:if>
  <xsl:value-of select="./inningsnote"/>
  </h2>
</xsl:template>

<!-- A smaller heading for the date and related data. -->

<xsl:template match="dateinfo">
  <h4>
  <xsl:apply-templates/>
  </h4>
</xsl:template>

<!-- Here's where the fun begins, with the teambatting sections, a table and a paragraph -->
<!-- for each team -->
<xsl:template match="batting/teambatting">

<!-- A table in which to insert the table data, -->
  <table cellspacing="0" cellpadding="0">

<!-- another tt tag, -->
    <tt>

<!-- with the team name and the stat-name labels into the first row. -->
      <tr>
        <td width="175"><xsl:apply-templates select="teamname"/></td>
        <td width="30" align="right">ab</td>
        <td width="30" align="right">r</td>
        <td width="30" align="right">h</td>
        <td width="30" align="right">rbi</td>
        <td width="30" align="right">bb</td>
        <td width="30" align="right">so</td>
        <td width="30" align="right">lob</td>
        <td width="45" align="right">avg</td>
      </tr>

<!-- In the succeeding rows, a for-each cycle through the batters. -->
    <xsl:for-each select="batter">
      <tr>

<!-- In the first cell, the footnotereference, if any -->
        <td><xsl:if test="footnotereference">
        <xsl:apply-templates select="footnotereference"/>-
        </xsl:if>

<!-- the player's name -->
        <xsl:apply-templates select="player"/>

<!-- and position. -->
      <xsl:apply-templates select="position"/></td>

<!-- Successive cells contain the qty for each mandatory stat. -->
      <td align="right"><xsl:apply-templates select="ab"/></td>
      <td align="right"><xsl:apply-templates select="r"/></td>
      <td align="right"><xsl:apply-templates select="h"/></td>
      <td align="right"><xsl:apply-templates select="rbi/qty"/></td>
      <td align="right"><xsl:apply-templates select="bb"/></td>
      <td align="right"><xsl:apply-templates select="so"/></td>
      <td align="right"><xsl:apply-templates select="lob"/></td>
      <td align="right"><xsl:apply-templates select="avg"/></td>
    </tr>

<!-- Then after the last batter come the team totals for each stat. -->
    </xsl:for-each>
      <tr height="30" valign="bottom">
        <td align="right"></td>
        <td align="right"><xsl:apply-templates select="ab"/></td>
        <td align="right"><xsl:apply-templates select="r"/></td>
        <td align="right"><xsl:apply-templates select="h"/></td>
        <td align="right"><xsl:apply-templates select="rbi"/></td>
        <td align="right"><xsl:apply-templates select="bb"/></td>
        <td align="right"><xsl:apply-templates select="so"/></td>
        <td align="right"><xsl:apply-templates select="lob"/></td>
      </tr>
    </tt>

<!-- And the end of the batters' table. -->
  </table>

<!-- The first item under the table is any batters' footnotes. -->
  <xsl:if test="footnote">
    <xsl:for-each select="footnote">
      <p>
      <xsl:value-of select="footnotereference"/>-<xsl:value-of select="footnotecontent"/>
      </p>
    </xsl:for-each>
  </xsl:if>

<!-- The most difficult item was likely moving stat items out of the "tabular" data and back -->
<!-- into the paragraph presentation. -->

<!-- A div style="width" tag keeps the paragraph about the same width as the table. -->
  <div style="width:430">

<!-- A new paragraph tag for the paragraph presentation, followed by the paragraph's -->
<!--introductory word "BATTING" -->
    <p>
      BATTING:

<!-- Then each optional stat was searched for among that team's player's. When found, that player's names  -->
<!-- and other information specific to that stat were printed. -->
      <xsl:if test="batter/x2b">
        2B - 
        <xsl:for-each select="batter/x2b">
          <xsl:value-of select="../player"/>

<!-- In the paragraph, if the qty is 1, only the player's name, not the qty is printed, by tradition, and to -->
<!-- save space. For 2 or more, a qty *is* given. -->
          <xsl:if test="../x2b[qty!='1']">
            <xsl:value-of select="../x2b/qty"/>
          </xsl:if>

<!-- The running total -->
          (<xsl:value-of select="runningtotal"/>,
<!--  and each "incident" element. A for-each loop was needed for the possibility of multiple incidents. -->
          <xsl:for-each select="../x2b/x2b3bi">
          <xsl:value-of select="against"/><xsl:value-of select="qty[not(qty='1')]"/></xsl:for-each>).
        </xsl:for-each>
      </xsl:if>
<!-- This is the end of the code for printing players who doubled, or acheived an x2b. -->

<!-- Then each of the other "optional" stats gets the same treatment, in the tradtional order of  -->
<!-- presentation. -->
      <xsl:if test="batter/x3b">
        3B - 
        <xsl:for-each select="batter/x3b">
          <xsl:value-of select="../player"/>
          <xsl:if test="../x3b[qty!='1']">
            <xsl:value-of select="../x3b/qty"/>
          </xsl:if>(<xsl:value-of select="runningtotal"/>,
          <xsl:for-each select="../x3b/x2b3bi">
            <xsl:value-of select="against"/><xsl:value-of select="qty[not(qty='1')]"/></xsl:for-each>).
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/hr">
        HR - 
        <xsl:for-each select="batter/hr">
          <xsl:value-of select="../player"/>
          <xsl:if test="../hr[qty!='1']">
            <xsl:value-of select="../hr/qty"/>
          </xsl:if>(<xsl:value-of select="runningtotal"/>,
          <xsl:for-each select="../hr/hri">
            <xsl:value-of select="inning"/>
            inning off
            <xsl:value-of select="against"/>
            <xsl:value-of select="on"/>
            on,
            <xsl:value-of select="out"/>
            out </xsl:for-each>).
          </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/sf">
        SF - 
        <xsl:for-each select="batter/sf">
          <xsl:value-of select="../player"/>
          <xsl:if test="../sf[qty!='1']">
            <xsl:value-of select="../sf/qty"/></xsl:if>.
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/sac">
        S - 
        <xsl:for-each select="batter/sac">
          <xsl:value-of select="../player"/>
          <xsl:if test="../sac[qty!='1']">
            <xsl:value-of select="../sac/qty"/></xsl:if>.
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/rbi">
        RBI - 
        <xsl:for-each select="batter/rbi[qty!='0']">
          <xsl:value-of select="../player"/>
          <xsl:if test="../rbi[qty!='1']">
            <xsl:value-of select="../rbi/qty"/>
          </xsl:if>
          (<xsl:value-of select="runningtotal"/>).
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/gidp">
        GIDP - 
        <xsl:for-each select="batter/gidp">
          <xsl:value-of select="../player"/>
          <xsl:if test="../gidp[qty!='1']">
            <xsl:value-of select="../gidp/qty"/></xsl:if>.
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/x2orbi">
        2-out RBI - 
        <xsl:for-each select="batter/x2orbi">
          <xsl:value-of select="../player"/>
            <xsl:if test="../x2orbi[qty!='1']">
            <xsl:value-of select="../x2orbi/qty"/></xsl:if>.
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="batter/rlsp2o">
        Runners left in scoring position, 2 out - 
        <xsl:for-each select="batter/rlsp2o">
          <xsl:value-of select="../player"/>
          <xsl:value-of select="../rlsp2o/qty"/>.
        </xsl:for-each>
      </xsl:if>

<!-- The tlob stat requires no if test, since it's always given, essentially a "mandatory" -->
<!-- paragraph stat. Thus it guarantees that the BATTING paragraph will always appear, not -->
<!-- true of the next two. -->
      Team LOB - 
        <xsl:value-of select="../teambatting/tlob/qty"/>.

<!-- The end of the BATTING paragraph. -->
    </p>
  </div>

<!-- Several basrunning stats appear in the BASERUNNING paragraph, but a test needs to be -->
<!-- run to see if any are present in the boxscore. If not the paragraph is omitted altogther. -->
  <xsl:if test="batter/sb|batter/cs|batter/po">
    <div style="width:410">
      <p>
        BASERUNNING: 

<!-- As with batting, each stat is tested for presence, then printed in its prescribed fashion. -->
<!-- sb and cs feature a triple-nested for-each loop. -->
        <xsl:if test="batter/sb">
          SB - 
          <xsl:for-each select="batter/sb">
            <xsl:value-of select="../player"/>
            <xsl:if test="../sb[qty!='1']">
              <xsl:value-of select="../sb/qty"/>
            </xsl:if>
            (<xsl:value-of select="runningtotal"/>,
            <xsl:for-each select="../sb/sbcspoi">
              <xsl:value-of select="base"/>
              base off
              <xsl:for-each select="against/player">
                <xsl:value-of select="."/>
              </xsl:for-each>).
            </xsl:for-each>
          </xsl:for-each>
        </xsl:if>

        <xsl:if test="batter/cs">
          CS - 
          <xsl:for-each select="batter/cs">
            <xsl:value-of select="../player"/>
            <xsl:if test="../cs[qty!='1']">
              <xsl:value-of select="../cs/qty"/>
            </xsl:if>
            (<xsl:value-of select="runningtotal"/>,
            <xsl:for-each select="../cs/sbcspoi">
              <xsl:value-of select="base"/>
              base off
              <xsl:for-each select="against/player">
                <xsl:value-of select="."/>
              </xsl:for-each>).
            </xsl:for-each>
          </xsl:for-each>
        </xsl:if>

       <xsl:if test="batter/po">
         Pickoff - 
         <xsl:for-each select="batter/po">
           <xsl:value-of select="../player"/>
           <xsl:if test="../po[qty!='1']">
             <xsl:value-of select="../po/qty"/>
           </xsl:if>
           (<xsl:for-each select="../po/sbcspoi">
             <xsl:value-of select="base"/>
             base
             <xsl:for-each select="against/player">
               <xsl:value-of select="."/>
             </xsl:for-each>).
           </xsl:for-each>
         </xsl:for-each>
       </xsl:if>

<!-- End of baserunning paragraph. -->
      </p>
    </div>
  </xsl:if>

<!-- The entire FIELDING paragraph is tested for presence of elements, then each element, as with -->
<!-- the BASERUNNING paragraph. -->
  <xsl:if test="batter/oa|batter/e|batter/pb|teambatting/dp|teambatting/tp">
    <div style="width:410">
      <p>
        FIELDING: 
        <xsl:if test="batter/oa">
          Outfield assist - 
         <xsl:for-each select="batter/oa">
           <xsl:value-of select="../player"/>
           <xsl:value-of select="../oa/qty"/>
           (<xsl:for-each select="../oa/oai">
             <xsl:value-of select="against"/>,
             <xsl:value-of select="base"/>
             base <xsl:value-of select="qty"/></xsl:for-each>).
        </xsl:for-each>
      </xsl:if>
      <xsl:if test="batter/e">
        E - 
        <xsl:for-each select="(batter/e|pitcher/e)">
          <xsl:value-of select="../player"/>
          <xsl:if test="../e[qty!='1']">
            <xsl:value-of select="../e/qty"/>
          </xsl:if>
          (<xsl:value-of select="runningtotal"/>,
          <xsl:for-each select="../e/ei">
            <xsl:value-of select="errortype"/><xsl:if test="../ei[qty!='1']"><xsl:value-of select="qty"/></xsl:if></xsl:for-each>).
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="teambatting/dp">
        DP - 
       <xsl:value-of select="../dp/qty"/>
       (<xsl:for-each select="../dp/dptpi">
         <xsl:for-each select="../dptpi/player">
           <xsl:value-of select="player"/>-
         </xsl:for-each>
         <xsl:value-of select="qty[not(qty='1')]"/></xsl:for-each>). 
       </xsl:if>

       <xsl:if test="teambatting/tp">
         TP - 
         <xsl:value-of select="../tp/qty"/>
         (<xsl:for-each select="../tp/dptpi">
           <xsl:for-each select="../dptpi/player">
             <xsl:value-of select="player"/>-
           </xsl:for-each>
           <xsl:value-of select="qty[not(qty='1')]"/></xsl:for-each>). 
       </xsl:if>

       <xsl:if test="batter/pb">
         PB - 
         <xsl:for-each select="batter/pb">
           <xsl:value-of select="../player"/>
           <xsl:if test="../pb[qty!='1']">
             <xsl:value-of select="../pb/qty"/></xsl:if></xsl:for-each>.
           </xsl:if>

<!-- End of FIELDING paragraph and batting section. -->
      </p>
    </div>
  </xsl:if>
</xsl:template>

<!-- The linescore is a table of its own, with team name, each inning score, and final score for -->
<!-- each team. The visiting always bats first and is listed first in the linescore. -->
<xsl:template match="linescore">
  <table cellspacing="0" cellpadding="0">
    <tt>
      <tr height="50" valign="bottom">
        <td width="175">
          <xsl:apply-templates select="teamline[@home='visiting']/teamname"/></td>

<!-- This for-each loop allows for a table cell for any number of innings. -->
          <xsl:for-each select="teamline[@home='visiting']/inningscore">
            <td width="20" align="right"><xsl:value-of select="."/></td>
          </xsl:for-each>
          <td width="50" align="center">--</td>
          <td width="15" align="right"><xsl:apply-templates select="teamline[@home='visiting']/teamfinalscore"/></td>
      </tr>

<!-- The home team. -->
      <tr height="50" valign="top"><td width="175">
        <xsl:apply-templates select="teamline[@home='home']/teamname"/></td>
        <xsl:for-each select="teamline[@home='home']/inningscore">
          <td width="20" align="right"><xsl:value-of select="."/></td>
        </xsl:for-each>
        <td width="50" align="center">--</td>
        <td width="15" align="right"><xsl:apply-templates select="teamline[@home='home']/teamfinalscore"/></td>
      </tr>
    </tt>
  </table>

<!-- A linescorenote, if any is inserted just beneath the table. -->
  <xsl:if test="../linescorenote">
    <xsl:value-of select="."/>
  </xsl:if>
</xsl:template>

<!-- The pitching section if much like the batting: first table, then paragraph. -->
<xsl:template match="pitching">
  <xsl:for-each select="teampitching/teamname">
  <table cellspacing="0" cellpadding="0">
    <tt>
      <tr height="30" valign="bottom">
        <td width="175"><xsl:apply-templates select="."/></td>
        <td width="30" align="right">ip</td>
        <td width="30" align="right">h</td>
        <td width="30" align="right">r</td>
        <td width="30" align="right">er</td>
        <td width="30" align="right">bb</td>
        <td width="30" align="right">so</td>
        <td width="30" align="right">hr</td>
        <td width="55" align="right">era</td>
      </tr>
      <xsl:for-each select="../pitcher">
        <tr>
          <td>
          <xsl:apply-templates select="player"/>
          <xsl:if test="decision">
            <xsl:for-each select="decision">
              (<xsl:value-of select="decisiontype"/>, <xsl:value-of select="runningtotal"/>)
            </xsl:for-each>
          </xsl:if></td>
          <td align="right"><xsl:apply-templates select="ip"/></td>
          <td align="right"><xsl:apply-templates select="rp"/></td>
          <td align="right"><xsl:apply-templates select="hp"/></td>
          <td align="right"><xsl:apply-templates select="er"/></td>
          <td align="right"><xsl:apply-templates select="bbp"/></td>
          <td align="right"><xsl:apply-templates select="sop"/></td>
          <td align="right"><xsl:apply-templates select="hrp"/></td>
          <td align="right"><xsl:apply-templates select="era"/></td>
        </tr>
        <tr><td> </td></tr>
      </xsl:for-each>
    </tt>
  </table>
  </xsl:for-each>
  <xsl:if test="teampitching/pitcher/pitchingnote">
    <div style="width:410">
      <p>
        <xsl:for-each select="teampitching/pitcher/pitchingnote">
          <xsl:value-of select="../player"/> pitched to
          <xsl:value-of select="numberbatters"/> batters in the
          <xsl:value-of select="battersinning"/>.
        </xsl:for-each>        
      </p>
    </div>
  </xsl:if>

<!-- For the pitching paragraph, it's possible that none of the elements will be present in a given -->
<!-- boxscore, so they need to all be tested for simultaneously to find the necessity of creating -->
<!-- the paragraph at all. -->
  <xsl:if test="teampitching/pitcher/ibb|teampitching/pitcher/hbp|teampitching/pitcher/wp|teampitching/pitcher/b|teampitching/pitcher/pi|teampitching/pitcher/gb|teampitching/pitcher/bf">
    <div style="width:410">
      <p>
        <xsl:if test="teampitching/pitcher/ibb">
          IBB - 
          <xsl:for-each select="teampitching/pitcher/ibb">
            <xsl:value-of select="against"/>
            <xsl:if test="../ibb[qty!='1']">
              <xsl:value-of select="../ibb/qty"/>
            </xsl:if>
            (by 
            <xsl:for-each select="../ibb">
              <xsl:value-of select="../player"/>).
            </xsl:for-each>
          </xsl:for-each>
        </xsl:if>
        <xsl:if test="teampitching/pitcher/hbp">
          HBP - 
          <xsl:for-each select="teampitching/pitcher/hbp">
            <xsl:value-of select="against"/>
            <xsl:if test="../hbp[qty!='1']">
              <xsl:value-of select="../hbp/qty"/>
            </xsl:if>
            (by 
            <xsl:for-each select="../hbp">
              <xsl:value-of select="../player"/>).
            </xsl:for-each>
          </xsl:for-each>
        </xsl:if>
        <xsl:if test="teampitching/pitcher/wp">
          WP - 
         <xsl:for-each select="teampitching/pitcher/wp">
           <xsl:value-of select="../player"/>
           <xsl:if test="../wp[qty!='1']">
             <xsl:value-of select="../wp/qty"/>
           </xsl:if>
         </xsl:for-each>.
       </xsl:if>
       <xsl:if test="teampitching/pitcher/b">
         Balk - 
           <xsl:for-each select="teampitching/pitcher/b">
             <xsl:value-of select="../player"/>
             <xsl:if test="../b[qty!='1']">
               <xsl:value-of select="../b/qty"/>
             </xsl:if>
           </xsl:for-each>.
       </xsl:if>
       <xsl:if test="teampitching/pitcher/pi">
         Pitches-strikes: 
         <xsl:for-each select="teampitching/pitcher">
           <xsl:value-of select="player"/>
           <xsl:value-of select="pi/qty"/>-<xsl:value-of select="st/qty"/>.
         </xsl:for-each>
       </xsl:if>
       <xsl:if test="teampitching/pitcher/gb">
         Ground balls-fly balls: 
         <xsl:for-each select="teampitching/pitcher">
           <xsl:value-of select="player"/>
           <xsl:value-of select="gb/qty"/>-<xsl:value-of select="fb/qty"/>.
         </xsl:for-each>
       </xsl:if>
       <xsl:if test="teampitching/pitcher/bf">
         Batters faced: 
         <xsl:for-each select="teampitching/pitcher">
           <xsl:value-of select="player"/>
           <xsl:value-of select="bf/qty"/>.
         </xsl:for-each>
       </xsl:if>
      </p>
    </div>
  </xsl:if>
</xsl:template>

<!-- The final matter, if present, gets two paragraphs: one for umpires and another for --> 
<!-- everything else. -->
<xsl:template match="finalmatter">
  <xsl:if test="umpires">
    <div style="width:410">
      <p>
        Umpires:
        <xsl:for-each select="umpires/umpire">
          <xsl:if test="position">
            <xsl:value-of select="position"/>--</xsl:if><xsl:value-of select="umpirename"/>.
        </xsl:for-each>
      </p>
    </div>
  </xsl:if>
    <div style="width:410">
      <p>
        <xsl:if test="time">
          T--<xsl:value-of select="time"/>.
        </xsl:if>
        <xsl:if test="attendance">
          Att--<xsl:value-of select="attendance"/>.
        </xsl:if>
        <xsl:if test="conditions">

<!-- xsl:choose/when/otherwise is used with the weather, to form an if/then/else pattern. -->
        <xsl:choose>
          <xsl:when test="indoors">
            <xsl:value-of select="indoors"/>
          </xsl:when>
          <xsl:otherwise>
            Weather:
            <xsl:value-of select="conditions/weather/temperature"/>,
            <xsl:value-of select="conditions/weather/skyconditions"/>.
            <xsl:if test="conditions/wind">
              Wind: <xsl:value-of select="conditions/wind/speed"/>, <xsl:value-of select="conditions/wind/direction"/>.
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </p>
  </div>
</xsl:template>

</xsl:stylesheet>


<!--
  Unsolved problems:
    comma after all but last in series
-->