<?xml version="1.0" encoding="ascii"?>
<!--

Copyright (c) 2002,2003 The Regenstrief Institute.  All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Written by Gunther Schadow.

$Id: hl7-spextract-html.xsl,v 1.1.1.1 2005/02/17 00:43:51 gschadow Exp $

-->
<xsl:transform version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:gut="http://aurora.regenstrief.org/XML/GenericUpTranslator"
   xmlns:saxon="http://saxon.sf.net/"
   extension-element-prefixes="saxon"
   exclude-result-prefixes="gut">

  <xsl:import href="spextract.xsl"/>
  <xsl:import href="escape2text.xsl"/>

  <xsl:output method="xml" indent="no" media-type="text/html"
     saxon:next-in-chain="spextract-html.xsl"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="node()"/>
    </root>
  </xsl:template>

  <!-- null transform -->       
  <xsl:template match="node()|@*">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <!-- ignore all segments that come after the first OBR in
       a message. I.e., where the closest preceding OBR comes
       after the closest preceding MSH. 

       MSH ... OBR /ignore/ MSH /don't-ignore/ OBR /ignore/ MSH ...
  -->
  <xsl:template match="*[not(self::MSH) and
                         (   preceding-sibling::OBR[1] 
                          >> preceding-sibling::MSH[1])]" priority="2"/>

  <!-- we intercept OBRs and from there step up to process the
       entire group of OBR up to the following MSH. Note that
       because we have everything after the first OBR ignored through
       the above template, this template will only be executed for
       the FIRST OBR in the group.
  -->
  <xsl:template match="OBR[following-sibling::MSH]">
    <xsl:variable name="next-msh" select="following-sibling::MSH[1]"/>
    <xsl:variable name="rest-of-message" 
       select="current()|following-sibling::node()[. &lt;&lt; $next-msh]"/>
    <xsl:for-each-group select="$rest-of-message"
                        group-starting-with="OBR">
      <!-- emit (perhaps edit) the existing segments in that group -->
      <xsl:apply-templates mode="obr-group" select="current-group()"/>
      <!-- generate new segments in that group -->
      <xsl:apply-templates mode="extract-info" select="current-group()"/>
    </xsl:for-each-group>
  </xsl:template>
  <xsl:template match="OBR">
    <xsl:variable name="rest-of-message" 
       select="current()|following-sibling::node()"/>
    <xsl:for-each-group select="$rest-of-message"
                        group-starting-with="OBR">
      <!-- emit (perhaps edit) the existing segments in that group -->
      <xsl:apply-templates mode="obr-group" select="current-group()"/>
      <!-- generate new segments in that group -->
      <xsl:apply-templates mode="extract-info" select="current-group()"/>
    </xsl:for-each-group>
  </xsl:template>


  <!-- OBR-GROUP mode, does all the edits to the existing segments
       and fields in an OBR-group. -->

  <!-- null transform -->       
  <xsl:template mode="obr-group" match="/|node()|@*">
    <xsl:apply-templates mode="obr-group" select="node()"/>
  </xsl:template>
  
  <!-- add edits in OBR-group mode here. -->


  <!-- EXTRACT-INFORMATION mode, here is where we generate
       new structured information from what we find in the 
       message. -->

  <!-- null transform -->
  <xsl:template mode="extract-info" match="/|node()|@*">
    <xsl:apply-templates mode="extract-info" select="node()"/>
  </xsl:template>

  <xsl:template mode="extract-info" match="OBX/field[5]">
    <xsl:variable name="text">
      <xsl:apply-templates mode="escape2text" select="node()"/>
    </xsl:variable>
    <!-- combine all text nodes into one (at least that's what I hope) -->
    <xsl:variable name="string"> 
      <xsl:value-of select="string-join($text,'')"/>
    </xsl:variable>
    <xsl:variable name="new-info">    
      <xsl:apply-templates mode="gut:top" select="$string/text()"/>
    </xsl:variable>    
    <spextract>
      <text><xsl:copy-of select="$string"/></text>
      <found><xsl:copy-of select="$new-info"/></found>
    </spextract>
  </xsl:template>

</xsl:transform>


