<!--

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.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="gut:org.regenstrief.xhl7.HL7XMLWriter"/>
  <!-- xsl:output method="xml" indent="yes"/ -->
  <xsl:strip-space elements="*"/>

  <xsl:template match="/|node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </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. -->

  <!-- identity transform -->       
  <xsl:template mode="obr-group" match="/|node()|@*">
    <xsl:copy>
      <xsl:apply-templates mode="obr-group" select="node()"/>
    </xsl:copy>
  </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>    
    <!-- xsl:message>
      <spextract>
        <text><xsl:copy-of select="$string"/></text>
        <found><xsl:copy-of select="$new-info"/></found>
      </spextract>
    </xsl:message -->
    <OBR>
      <field/>
      <field/>
      <field/>
      <field>
        <component>text sections</component>
        <component/>
      </field>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field>
          <xsl:apply-templates mode="component" select="../field[3]"/>
        <component>
          <xsl:apply-templates mode="component" select="../field[4]"/>
        </component>
      </field>
      <field/>
    </OBR>
    <xsl:apply-templates mode="new-section-segments" select="$new-info"/>
    <xsl:apply-templates mode="new-specimen-segments" select="$new-info"/>
  </xsl:template>


  <!-- COMPONENT push components into subcomponents -->

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

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

  <xsl:template mode="subcomponent" match="component">
    <xsl:message>
      <WARNING message="subcomponent lost">
        <xsl:copy-of select="."/>
      </WARNING>
    </xsl:message>
  </xsl:template>

  <!-- NEW-SECTION-SEGMENTS mode, add HL7 v2 segments for the 
       sections we found. -->

  <!-- null transform, we can't pass through unhandled junk here -->
  <xsl:template mode="new-section-segments" match="/|node()|@*">
    <xsl:apply-templates mode="new-section-segments" select="node()"/>
  </xsl:template>

  <xsl:template mode="new-section-segments" match="section">
    <OBX>
      <field/>
      <field>ST</field>
      <field>
        <component>
	  <xsl:value-of select="@title"/>
	</component>
        <component/>
      </field>
      <field><xsl:number/></field>
      <field>
        <xsl:copy-of select="text()|escape"/>
      </field>
      <field/>
    </OBX>
    <xsl:apply-templates mode="new-section-segments" select="node()"/>
  </xsl:template>
  

  <!-- NEW-SPECIMEN-SEGMENTS mode, add HL7 v2 segments for the 
       specimens and observations we may have found. -->

  <!-- null transform, we can't pass through unhandled junk here -->
  <xsl:template mode="new-specimen-segments" match="/|node()|@*">
    <xsl:apply-templates mode="new-specimen-segments" select="node()"/>
  </xsl:template>

  <xsl:template mode="new-specimen-segments" match="specimen">
    <OBR>
      <field/>
      <field/>
      <field/>
      <field>
        <component>extracted specimen information</component>
        <component/>
      </field>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field>
            <xsl:value-of select="tissue-type/@code"/>	  
          <subcomponent>
            <xsl:copy-of select="tissue-type/text()"/>
          </subcomponent>
          <subcomponent>
            <xsl:value-of select="tissue-type/@codeSystem"/>	  
          </subcomponent>
        <component/>
        <component/>
        <component>
            <xsl:value-of select="site-modifier/@code"/>	  
          <subcomponent>
            <xsl:copy-of select="site-modifier/text()"/>
          </subcomponent>
          <subcomponent>
            <xsl:value-of select="site-modifier/@codeSystem"/>	  
          </subcomponent>
        </component>
        <component>
            <xsl:value-of select="collection-method/@code"/>	  
          <subcomponent>
            <xsl:copy-of select="collection-method/text()"/>
          </subcomponent>
          <subcomponent>
            <xsl:value-of select="collection-method/@codeSystem"/>	  
          </subcomponent>
        </component>
      </field>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field/>
      <field>
          <subcomponent>
	    <xsl:value-of select="parent::section/@title"/>
	  </subcomponent>
          <subcomponent/>
        <component>
          <xsl:number count="section"/>
	</component>
      </field>
      <field/>
    </OBR>
    <xsl:apply-templates mode="new-specimen-segments" select="node()"/>
  </xsl:template>

  <xsl:template mode="new-specimen-segments" match="observation[value/@code]">
    <OBX>
      <field/>
      <field>CE</field>
      <field>
          <xsl:value-of select="code/@code"/>
        <component>
          <xsl:copy-of select="code/text()"/>	  
        </component>
        <component>
          <xsl:value-of select="code/@codeSystem"/>	  
        </component>
      </field>
      <field/>
      <field>
          <xsl:value-of select="value/@code"/>
        <component>
          <xsl:copy-of select="value/text()"/>	  
        </component>
        <component>
          <xsl:value-of select="value/@codeSystem"/>	  
        </component>
      </field>
      <field/>
    </OBX>
    <xsl:apply-templates mode="new-specimen-segments" select="node()"/>
  </xsl:template>

  <xsl:template mode="new-specimen-segments" match="observation[value/@value]">
    <OBX>
      <field/>
      <field>NM</field>
      <field>
          <xsl:value-of select="code/@code"/>
        <component>
          <xsl:copy-of select="code/text()"/>	  
        </component>
        <component>
          <xsl:value-of select="code/@codeSystem"/>	  
        </component>
      </field>
      <field/>
      <field>
        <xsl:value-of select="value/@value"/>	  
      </field>
      <field>
        <xsl:value-of select="value/@unit"/>	  
      </field>
      <field/>
    </OBX>
    <xsl:apply-templates mode="new-specimen-segments" select="node()"/>
  </xsl:template>

  <xsl:template mode="new-specimen-segments" match="observation">
    <OBX>
      <field/>
      <field>ST</field>
      <field>
          <xsl:value-of select="code/@code"/>
        <component>
          <xsl:copy-of select="code/text()"/>	  
        </component>
        <component>
          <xsl:value-of select="code/@codeSystem"/>	  
        </component>
      </field>
      <field/>
      <field>
        <xsl:copy-of select="text/text()"/>
      </field>
      <field/>
    </OBX>
    <xsl:apply-templates mode="new-specimen-segments" select="node()"/>
  </xsl:template>
  
</xsl:transform>


