<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<!-- Define Keys to Group Elements-->
<xsl:key name="SAPConnIDGroup" match="ObjectClassField[ @name = 'SAP ConnectionObject ID']" use="."/>
<xsl:key name="SAPPremiseIDGroup" match="ObjectClassField[ @name = 'SAP Premise ID']" use="."/>
<xsl:key name="SAPPremiseID"
         match="MapDocumentRelationships/ObjectClass/ObjectClassRows/ObjectClassRow"
   use="ObjectClassField[ @name = 'SAP Premise ID']"/>
<xsl:template match="/">
 <html>
  <head>
   <title>SAP ConnectionObject ID Report</title>
   <style type="text/xsl">
   body, td, th
  {
    font-size:80%;
    font-family:Verdana,Arial,Sans-serif;
    color:#000000;
    text-align:left;
  }
 table
  {  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;
  border-style: solid;
  }
 table, td
  {  border-width: 1px;
  }
 td
 {  color: black;
 font-family: Arial;
 font-size: x-small;
 border-right-style: none;
 border-left-style: none;
 border-top-style: solid;
 border-bottom-style: solid;
 }
.DarkBack
{  background-color: #0066FF;
   background-color: blue;
   color: white;
   font-weight: bold;
}
.LightBack
{  background-color: #FFFF00;
   color: yellow;
}
.RightJustified
{  text-align: right;
}
</style>
  </head>
  <body>
   <h3>SAP Connection ID at Selected Address(s)</h3>
   <xsl:for-each select="/*/*/MapDocumentRelationships//ObjectClassField[ @name = 'SAP ConnectionObject ID']">
  <xsl:sort select="." data-type="number"/>
   <xsl:if test="generate-id() = generate-id( key( 'SAPConnIDGroup', .)[1])">
    <table>
     <tr bgcolor="#ffff00">
      <th>
       <xsl:text>Connection Object ID: </xsl:text>
       <xsl:value-of select="."/>
      </th>
     </tr>
    </table>
   </xsl:if>
  <xsl:for-each select="/*/*/MapDocumentRelationships//ObjectClassField[ @name = 'SAP Premise ID']">
   <xsl:sort select="." data-type="number"/>
    <xsl:if test="generate-id() = generate-id( key( 'SAPPremiseIDGroup', .)[1])">
     <table>
      <tr bgcolor="#9acd32">
       <th>
        <xsl:text>Premise ID: </xsl:text>
        <xsl:value-of select="."/>
       </th>
      </tr>
     </table>
     <table border="2" cellpadding="2" cellspacing="1">
      <tr bgcolor="dod2c4">
       <th>Install Number</th>
       <th>Install Type</th>
      </tr>
   <!-- inspections -->
     <xsl:for-each select="key( 'SAPPremiseID', .)">
      <xsl:sort select="ObjectClassField[ @name = 'SAP Premise ID']" order="ascending"/>
      <tr>
       <xsl:apply-templates select="."/>
      </tr>
     </xsl:for-each>
     </table>
    </xsl:if>
  </xsl:for-each>
   </xsl:for-each>
  </body>
 </html>
</xsl:template>

  <xsl:template match="ObjectClassRow">
  <!-- Extend this to display more information. -->
  <td>
   <xsl:value-of select="ObjectClassField[ @name = 'SAP Install Number']"/>
  </td>
  <td>
   <xsl:variable name="sit" select="ObjectClassField[ @name = 'SAP Install Type']"/>
    <xsl:choose>
     <xsl:when test="$sit = 'ATTG'">AttenGen</xsl:when>
     <xsl:when test="$sit = 'ELEC'">Electric</xsl:when>
     <xsl:otherwise>UNK</xsl:otherwise>
    </xsl:choose>
  </td>
 </xsl:template>
</xsl:stylesheet>