<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
   Name:  EntityAndAttributeInformation.xsl
   Author:  James W. Allor
   Date:  20050908
   Description:  XSLT stylesheet that transforms the Entity and Attribute
                 Information from the MIF to the MRF.
                 Please reference the MIF Specification and the GPMS Table 1.
   Modification History:
     Initial    Date      Change Request ID   Description
      JWA      20050908    N/A                 Initial Version 
      JWA      20051115    N/A                 Updated to reflect current MIF Spec.
-->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>


<xsl:template name="Entity_and_Attribute_Information">

  <xsl:element name="Entity_and_Attribute_Information">

    <xsl:for-each-group select="Entity_Type_Label,
                                Entity_Type_Definition,
                                Entity_Type_Definition_Source,
                                Attribute_Label,
                                Attribute_Definition,
                                Attribute_Definition_Source,
                                Enumerated_Domain_Value,
                                Enumerated_Domain_Value_Definition,
                                Enumerated_Domain_Value_Definition_Source,
                                Range_Domain_Minimum,
                                Range_Domain_Maximum,
                                Attribute_Units_of_Measure,
                                Codeset_Name,
                                Codeset_Source,
                                Unrepresentable_Domain"
                                group-by="@gid1">
      <xsl:sort select="current-grouping-key()" data-type="number"/>
      <xsl:sort select="current-grouping-key()" data-type="text"/>

      <xsl:variable name="gid1" select="current-grouping-key()"/>

      <xsl:element name="Detailed_Description">

        <xsl:element name="Entity_Type">
          <xsl:for-each select="current-group()">
            <xsl:if test="name()='Entity_Type_Label'
                          or name()='Entity_Type_Definition'
                          or name()='Entity_Type_Definition_Source'">
              <xsl:copy>
                <xsl:apply-templates select="."/>
              </xsl:copy>
            </xsl:if>
          </xsl:for-each>
        </xsl:element>

        <xsl:for-each-group select="current-group()" group-by="@gid2">
          <xsl:sort select="current-grouping-key()" data-type="number"/>
          <xsl:sort select="current-grouping-key()" data-type="text"/>

          <xsl:variable name="gid2" select="current-grouping-key()"/>

          <xsl:element name="Attribute">
            <xsl:for-each select="current-group()">
              <xsl:if test="name()='Attribute_Label'
                            or name()='Attribute_Definition'
                            or name()='Attribute_Definition_Source'">
                <xsl:copy>
                  <xsl:apply-templates select="."/>
                </xsl:copy>
              </xsl:if>
            </xsl:for-each>

            <xsl:if test="/MIF/Enumerated_Domain_Value[@gid1=$gid1][@gid2=$gid2]">
              <xsl:element name="Enumerated_Domain">
                <xsl:for-each select="/MIF/Enumerated_Domain_Value[@gid1=$gid1][@gid2=$gid2],
                                      /MIF/Enumerated_Domain_Value_Definition[@gid1=$gid1][@gid2=$gid2],
                                      /MIF/Enumerated_Domain_Value_Definition_Source[@gid1=$gid1][@gid2=$gid2]">
                  <xsl:sort select="@gid3" data-type="number"/>
                  <xsl:sort select="@gid3" data-type="text"/>
                  <xsl:copy>
                    <xsl:apply-templates select="."/>
                  </xsl:copy>
                </xsl:for-each>
              </xsl:element>
            </xsl:if>

            <xsl:for-each-group select="current-group()" group-by="@gid3">
              <xsl:sort select="current-grouping-key()" data-type="number"/>
              <xsl:sort select="current-grouping-key()" data-type="text"/>
              <xsl:variable name="gid3" select="current-grouping-key()"/>
              <xsl:for-each select="current-group()">
                <xsl:if test="name()='Range_Domain_Minimum'">
                  <xsl:element name="Range_Domain">
                    <xsl:for-each select="/MIF/Range_Domain_Minimum[@gid1=$gid1][@gid2=$gid2][@gid3=$gid3],
                                          /MIF/Range_Domain_Maximum[@gid1=$gid1][@gid2=$gid2][@gid3=$gid3],
                                          /MIF/Attribute_Units_of_Measure[@gid1=$gid1][@gid2=$gid2][@gid3=$gid3]">
                      <xsl:copy>
                        <xsl:apply-templates select="."/>
                      </xsl:copy>
                    </xsl:for-each>
                  </xsl:element>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each-group>

            <xsl:for-each-group select="current-group()" group-by="@gid3">
              <xsl:sort select="current-grouping-key()" data-type="number"/>
              <xsl:sort select="current-grouping-key()" data-type="text"/>
              <xsl:variable name="gid3" select="current-grouping-key()"/>
              <xsl:for-each select="current-group()">
                <xsl:if test="name()='Codeset_Name'">
                  <xsl:element name="Codeset_Domain">
                    <xsl:for-each select="/MIF/Codeset_Name[@gid1=$gid1][@gid2=$gid2][@gid3=$gid3],
                                          /MIF/Codeset_Source[@gid1=$gid1][@gid2=$gid2][@gid3=$gid3]">
                      <xsl:copy>
                        <xsl:apply-templates select="."/>
                      </xsl:copy>
                    </xsl:for-each>
                  </xsl:element>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each-group>

            <xsl:for-each select="current-group()">
              <xsl:if test="name()='Unrepresentable_Domain'">
                <xsl:copy>
                  <xsl:apply-templates select="."/>
                </xsl:copy>
              </xsl:if>
            </xsl:for-each>

          </xsl:element>
                
        </xsl:for-each-group>

      </xsl:element>

    </xsl:for-each-group>


    <xsl:for-each-group select="Entity_and_Attribute_Overview,
                                Entity_and_Attribute_Detail_Citation"
                        group-by="@gid1">
      <xsl:sort select="current-grouping-key()" data-type="number"/>
      <xsl:sort select="current-grouping-key()" data-type="text"/>

      <xsl:element name="Overview_Description">
      <xsl:for-each select="current-group()">
        <xsl:copy>
          <xsl:apply-templates select="."/>
        </xsl:copy>
      </xsl:for-each>
      </xsl:element>

    </xsl:for-each-group> 


  </xsl:element>

</xsl:template>


</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios/><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->
