<?xml version="1.0"?>
<!--
Copyright (c) 2000-2002 Carnegie Mellon University. All rights reserved.
Redistribution and use in source and binary forms, with or without   
modification, are permitted provided that the following conditions are met:
   
1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.  

3. The name "Carnegie Mellon University" must not be used to endorse or
   promote products derived from this software without prior written
   permission. For permission or any legal details, please contact:  
     Office of Technology Transfer
     Carnegie Mellon University
     5000 Forbes Avenue
     Pittsburgh, PA 15213-3890
     (412) 268-4387, fax: (412) 268-7395
     tech-transfer@andrew.cmu.edu

4. Redistributions of any form whatsoever must retain the following
   acknowledgment: "This product includes software developed by Computing
   Services at Carnegie Mellon University (http://www.cmu.edu/computing/)."

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE  
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
-->
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:sage="http://www.net.cmu.edu/netsage/xml/main" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:attr="http://www.net.cmu.edu/netsage/xml/attributes">
  <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  <xsl:key name="hostid" match="/dbidump/resultset[@table='hosts']/rows/row"
    use="@id"/>
  <xsl:key name="hostname" match="/dbidump/resultset[@table='hosts']/rows/row"
    use="@name"/>
  <xsl:key name="hgid" match="/dbidump/resultset[@table='hostgroups']/rows/row"
    use="@id"/>
  <xsl:key name="hgmemgrp" 
    match="/dbidump/resultset[@table='hostgroup_memberships']/rows/row" 
    use="@hostgroup"/>
  <xsl:key name="userid" match="/dbidump/resultset[@table='users']/rows/row"
    use="@id"/>
  <xsl:key name="username" match="/dbidump/resultset[@table='users']/rows/row"
    use="@name"/>
  <xsl:key name="ugmemgrp" 
    match="/dbidump/resultset[@table='usergroup_memberships']/rows/row" 
    use="@usergroup"/>
  <xsl:key name="servtypid" 
    match="/dbidump/resultset[@table='server_types']/rows/row" 
    use="@id"/>
  <xsl:key name="serverid" 
    match="/dbidump/resultset[@table='servers']/rows/row" 
    use="@id"/>
  <xsl:key name="attrtypid" 
    match="/dbidump/resultset[@table='attribute_types']/rows/row" 
    use="@id"/>
  <xsl:key name="actionid" 
    match="/dbidump/resultset[@table='actions']/rows/row" 
    use="@id"/>
  <xsl:key name="svctypid" 
    match="/dbidump/resultset[@table='service_types']/rows/row" 
    use="@id"/>
  <xsl:key name="svcid" match="/dbidump/resultset[@table='services']/rows/row"
    use="@id"/>
  <xsl:key name="svchg" match="/dbidump/resultset[@table='services']/rows/row"
    use="@hostgroup"/>
  <xsl:key name="attr_tsf" 
    match="/dbidump/resultset[@table='attributes']/rows/row" 
    use="concat(@attribute_type,',',@server,',',@server_file)"/>
  <xsl:key name="attr_tsf_serv" 
    match="/dbidump/resultset[@table='attributes']/rows/row" 
    use="concat(@attribute_type,',',@server,',',@server_file,',',@service)"/>
  <xsl:key name="attr_tsf_stype" 
    match="/dbidump/resultset[@table='attributes']/rows/row" 
    use="concat(@attribute_type,',',@server,',',@server_file,',',@service_type)"/>
  <xsl:key name="attr_sf_period" 
    match="/dbidump/resultset[@table='attributes']/rows/row" 
    use="concat(@server,',',@server_file,',',@alert_period)"/>
  <xsl:key name="alertpsvc" 
    match="/dbidump/resultset[@table='alert_period']/rows/row" 
    use="@service"/>
  <xsl:key name="alertpsvct" 
    match="/dbidump/resultset[@table='alert_period']/rows/row" 
    use="@service_type"/>
  <xsl:key name="subperbyalertp" 
    match="/dbidump/resultset[@table='subperiods']/rows/row" 
    use="@alert_period"/>
  <xsl:key name="subperid" 
    match="/dbidump/resultset[@table='subperiods']/rows/row" 
    use="@id"/>
  <xsl:key name="depid" 
    match="/dbidump/resultset[@table='dependency_tree']/rows/row" 
    use="@id"/>
  <xsl:key name="file_stype" 
    match="/dbidump/resultset[@table='server_files']/rows/row" 
    use="@server_type"/>

  <xsl:key name="criticalityid"
    match="/dbidump/resultset[@table='service_class']/rows/row"
    use="@id"/>
  <xsl:key name="responsibilityid"
    match="/dbidump/resultset[@table='service_responsibility']/rows/row"
    use="@id"/>

  <xsl:variable name="hosts" select="/dbidump/resultset[@table='hosts']/rows"/>
  <xsl:variable name="hostgroups" select="/dbidump/resultset[@table='hostgroups']/rows"/>
  <xsl:variable name="servers" select="/dbidump/resultset[@table='servers']/rows"/>
  <xsl:variable name="attrtypes" select="/dbidump/resultset[@table='attribute_types']/rows"/>


  <xsl:template match="/dbidump">
    <sage:top xsi:schemaLocation="http://www.net.cmu.edu/netsage/xml/main netsage.xsd http://www.net.cmu.edu/netsage/xml/attributes attributes.xsd">
      <sage:hosts>
        <xsl:for-each select="$hosts/row">
          <sage:host id="{@id}" name="{@host_name}" ip_address="{@ip_address}"/>
        </xsl:for-each>
      </sage:hosts>
      <xsl:for-each select="$servers/row">
        <xsl:variable name="server" select="@id"/>
        <xsl:variable name="serverhost" select="@host"/>
        <xsl:variable name="servtype" select="@server_type"/>
<!--        <xsl:text>&#xA;   </xsl:text> -->
        <sage:server server_hostname="{key('hostid',$serverhost)/@host_name}"
          server_software="{key('servtypid',$servtype)/@software}"
          server_swversion="{key('servtypid',$servtype)/@software_version}">
          <xsl:for-each select="key('file_stype',$servtype)">
            <xsl:variable name="srvfile" select="@id"/>
            <sage:server_file file_name="{@file}">
<!--              <xsl:text>&#xA;      </xsl:text> -->
              <sage:globalattrs>
                <xsl:for-each select="$attrtypes/row">
                  <xsl:if test="@parent_table=''">
                    <xsl:variable name="attypid" select="@id"/>
                    <xsl:for-each select="key('attr_tsf',concat($attypid,',',$server,',',$srvfile))">
<!--                      <xsl:text>&#xA;         </xsl:text> -->
                      <xsl:call-template name="format-attribute"/>
                    </xsl:for-each>
                  </xsl:if>
                </xsl:for-each>
<!--                <xsl:text>&#xA;      </xsl:text> -->
              </sage:globalattrs>
              <xsl:for-each select="$hostgroups/row">
                <xsl:variable name="hostgroupid" select="@id"/>
<!--                <xsl:text>&#xA;      </xsl:text> -->
                <sage:watchgroup name="{@name}" description="{@description}">
                  <xsl:for-each select="key('hgmemgrp',$hostgroupid)">
<!--                    <xsl:text>&#xA;          </xsl:text> -->
                    <sage:host ref="{@host}"/>
                  </xsl:for-each>
                  <xsl:for-each select="key('svchg',$hostgroupid)">
                    <xsl:variable name="serviceid" select="@id"/>
                    <xsl:variable name="svctypid" select="@service_type"/>
                    <xsl:variable name="service" select="."/>
<!--                    <xsl:text>&#xA;          </xsl:text> -->
                    <sage:service name="{key('svctypid',$svctypid)/@name}">
                      <xsl:if test="@alert_dependency_tree != 'NULL'">
                        <sage:dependencies type="alert">
                          <xsl:call-template name="extract-depends">
                            <xsl:with-param name="root" select="@alert_dependency_tree"/>
                          </xsl:call-template>
                        </sage:dependencies>
                      </xsl:if>
                      <xsl:if test="@monitor_dependency_tree != 'NULL'">
                        <sage:dependencies type="monitor">
                          <xsl:call-template name="extract-depends">
                            <xsl:with-param name="root" select="@monitor_dependency_tree"/>
                          </xsl:call-template>
                        </sage:dependencies>
                      </xsl:if>
                      <xsl:if test="@host_dependency_tree != 'NULL'">
                        <sage:dependencies type="host">
                          <xsl:call-template name="extract-depends">
                            <xsl:with-param name="root" select="@host_dependency_tree"/>
                          </xsl:call-template>
                        </sage:dependencies>
                      </xsl:if>
                      <xsl:variable name="defcriticality" 
                        select="key('hgid',$hostgroupid)/@service_class"/>
                      <xsl:variable name="defresp" 
                        select="key('hgid',$hostgroupid)/@service_responsibility"/>
                      <xsl:choose>
                        <xsl:when test="@class != 'NULL' and @responsibility != 'NULL'">
                          <xsl:call-template name="format-svc-attributes">
                            <xsl:with-param name="server" select="$server"/>
                            <xsl:with-param name="srvfile" select="$srvfile"/>
                            <xsl:with-param name="service" select="$service"/>
                            <xsl:with-param name="criticality" select="@class"/>
                            <xsl:with-param name="responsibility" select="@responsibility"/>
                          </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="@class != 'NULL'">
                          <xsl:call-template name="format-svc-attributes">
                            <xsl:with-param name="server" select="$server"/>
                            <xsl:with-param name="srvfile" select="$srvfile"/>
                            <xsl:with-param name="service" select="$service"/>
                            <xsl:with-param name="criticality" select="@class"/>
                            <xsl:with-param name="responsibility" select="$defresp"/>
                          </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="@responsibility != 'NULL'">
                          <xsl:call-template name="format-svc-attributes">
                            <xsl:with-param name="server" select="$server"/>
                            <xsl:with-param name="srvfile" select="$srvfile"/>
                            <xsl:with-param name="service" select="$service"/>
                            <xsl:with-param name="criticality" select="$defcriticality"/>
                            <xsl:with-param name="responsibility" select="@responsibility"/>
                          </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:call-template name="format-svc-attributes">
                            <xsl:with-param name="server" select="$server"/>
                            <xsl:with-param name="srvfile" select="$srvfile"/>
                            <xsl:with-param name="service" select="$service"/>
                            <xsl:with-param name="criticality" select="$defcriticality"/>
                            <xsl:with-param name="responsibility" select="$defresp"/>
                          </xsl:call-template>
                        </xsl:otherwise>
                      </xsl:choose>
<!--                      <xsl:text>&#xA;          </xsl:text> -->
                    </sage:service>
                  </xsl:for-each>
<!--                  <xsl:text>&#xA;      </xsl:text> -->
                </sage:watchgroup>
              </xsl:for-each>
<!--              <xsl:text>&#xA;   </xsl:text> -->
            </sage:server_file>
          </xsl:for-each>
        </sage:server>
      </xsl:for-each>
    </sage:top>
  </xsl:template>

  <xsl:template name="format-svc-attributes">
    <xsl:param name="server" select="''"/>
    <xsl:param name="srvfile" select="''"/>
    <xsl:param name="service" select="''"/>
    <xsl:param name="criticality" select="0"/>
    <xsl:param name="responsibility" select="0"/>
    <xsl:variable name="serviceid" select="$service/@id"/> 
    <xsl:variable name="svctypid" select="$service/@service_type"/>
    <xsl:for-each select="$attrtypes/row">
      <xsl:variable name="attypid" select="@id"/>
      <xsl:variable name="attypname" select="@name"/>
      <xsl:variable name="keyprefix" select="concat($attypid,',',$server,',',$srvfile,',')"/>
      <xsl:choose>
        <xsl:when test="count(key('attr_tsf_serv',concat($keyprefix,$serviceid)))>0">
          <xsl:for-each select="key('attr_tsf_serv',concat($keyprefix,$serviceid))">
            <xsl:if test="@service_class and @service_class != 'NULL' and @service_class != $criticality">
              <xsl:variable name="attrcriticality" select="@service_class"/>
              <xsl:message terminate="yes">
                <xsl:text>Service </xsl:text>
                <xsl:value-of select="$service/@name"/>
                <xsl:text> has criticality </xsl:text>
                <xsl:value-of select="key('criticalityid', $criticality)/@name"/>
                <xsl:text> but has </xsl:text>
                <xsl:value-of select="$attypname"/>
                <xsl:text> attribute with criticality </xsl:text>
                <xsl:value-of select="key('criticalityid', $attrcriticality)/@name"/>
              </xsl:message>
            </xsl:if>
            <xsl:if test="@service_responsibility and @service_responsibility != 'NULL' 
              and @service_responsibility != $responsibility">
              <xsl:variable name="attrresponsibility" select="@service_responsibility"/>
              <xsl:message terminate="yes">
                <xsl:text>Service </xsl:text>
                <xsl:value-of select="$service/@name"/>
                <xsl:text> has responsibility </xsl:text>
                <xsl:value-of select="key('responsibilityid', $responsibility)/@name"/>
                <xsl:text> but has </xsl:text>
                <xsl:value-of select="$attypname"/>
                <xsl:text> attribute with responsibility </xsl:text>
                <xsl:value-of select="key('responsibilityid', $attrresponsibility)/@name"/>
              </xsl:message>
            </xsl:if>
            <!--                              <xsl:text>&#xA;             </xsl:text> -->
            <xsl:call-template name="format-attribute"/>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="key('attr_tsf_stype',concat($keyprefix,$svctypid))">
            <xsl:if test="(@service_class = 'NULL' or @service_class=$criticality) and (@service_responsibility = 'NULL' or @service_responsibility = $responsibility)">
              <!--                              <xsl:text>&#xA;             </xsl:text> -->
              <xsl:call-template name="format-attribute"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:choose>
      <xsl:when test="count(key('alertpsvc',$serviceid))>0">
        <xsl:for-each select="key('alertpsvc',$serviceid)">
          <xsl:call-template name="format-period">
            <xsl:with-param name="server" select="$server"/>
            <xsl:with-param name="srvfile" select="$srvfile"/>
            <xsl:with-param name="service" select="$service"/>
            <xsl:with-param name="criticality" select="$criticality"/>
            <xsl:with-param name="responsibility" select="$responsibility"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="key('alertpsvct',$svctypid)">
          <xsl:call-template name="format-period">
            <xsl:with-param name="server" select="$server"/>
            <xsl:with-param name="srvfile" select="$srvfile"/>
            <xsl:with-param name="service" select="$service"/>
            <xsl:with-param name="criticality" select="$criticality"/>
            <xsl:with-param name="responsibility" select="$responsibility"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="format-period">
    <xsl:param name="server" select="''"/>
    <xsl:param name="srvfile" select="''"/>
    <xsl:param name="service" select="''"/>
    <xsl:param name="criticality" select="0"/>
    <xsl:param name="responsibility" select="0"/>
    <xsl:variable name="periodid" select="@id"/>
<!--    <xsl:text>&#xA;             </xsl:text> -->
    <sage:period>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <xsl:for-each select="key('subperbyalertp',$periodid)">
<!--        <xsl:text>&#xA;                </xsl:text> -->
        <sage:subperiod>
          <xsl:if test="@weekday_start and @weekday_start!='NULL'">
            <xsl:attribute name="weekday_start">
              <xsl:value-of select="@weekday_start"/>
            </xsl:attribute>
            <xsl:attribute name="weekday_end">
              <xsl:value-of select="@weekday_end"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="@time_start and @time_start!='NULL'">
            <xsl:attribute name="time_start">
              <xsl:value-of select="@time_start"/>
            </xsl:attribute>
            <xsl:attribute name="time_end">
              <xsl:value-of select="@time_end"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="@month_start and @month_start!='NULL'">
            <xsl:attribute name="month_start">
              <xsl:value-of select="@month_start"/>
            </xsl:attribute>
            <xsl:attribute name="month_end">
              <xsl:value-of select="@month_end"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="@monthday_start and @monthday_start!='NULL'">
            <xsl:attribute name="monthday_start">
              <xsl:value-of select="@monthday_start"/>
            </xsl:attribute>
            <xsl:attribute name="monthday_end">
              <xsl:value-of select="@monthday_end"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="@monthday_start and @monthday_start!='NULL'">
            <xsl:attribute name="monthday_start">
              <xsl:value-of select="@monthday_start"/>
            </xsl:attribute>
            <xsl:attribute name="monthday_end">
              <xsl:value-of select="@monthday_end"/>
            </xsl:attribute>
          </xsl:if>
        </sage:subperiod>
      </xsl:for-each>
      <xsl:for-each select="key('attr_sf_period',concat($server,',',$srvfile,',',$periodid))">
<!--        <xsl:text>&#xA;                </xsl:text> -->
        <xsl:variable name="attypname" select="key('attrtypid',@attribute_type)/@name"/> 
        <xsl:variable name="attrcriticality" select="@service_class"/>
        <xsl:variable name="attrresponsibility" select="@service_responsibility"/>
<!--
        <xsl:message terminate="no">
          <xsl:text>Group </xsl:text>
          <xsl:value-of select="key('hgid',$service/@hostgroup)/@name"/>
          <xsl:text> service </xsl:text>
          <xsl:value-of select="key('svctypid',$service/@service_type)/@name"/>
          <xsl:text> criticality </xsl:text>
          <xsl:value-of select="key('criticalityid', $criticality)/@name"/>
          <xsl:text> responsibility </xsl:text>
          <xsl:value-of select="key('responsibilityid', $responsibility)/@name"/>
          <xsl:text> attribute </xsl:text>
          <xsl:value-of select="$attypname"/>
          <xsl:text> source </xsl:text>
          <xsl:value-of select="concat(@service,'/',@service_type)"/>
          <xsl:if test="@service_class != 'NULL'">
            <xsl:text> criticality </xsl:text>
            <xsl:value-of select="key('criticalityid', $attrcriticality)/@name"/>
          </xsl:if>
          <xsl:if test="@service_responsibility != 'NULL'">
            <xsl:text> responsibility </xsl:text>
            <xsl:value-of select="key('responsibilityid', $attrresponsibility)/@name"/>
          </xsl:if>
          <xsl:if test="@service_class = 'NULL'">
            <xsl:text> defcriticality</xsl:text>
          </xsl:if>
          <xsl:if test="@service_responsibility = 'NULL'">
            <xsl:text> defresponsibility</xsl:text>
          </xsl:if>
        </xsl:message>
-->
        <xsl:choose>
          <xsl:when test="@service != 'NULL'">
            <xsl:if test="@service_class and @service_class != 'NULL' and @service_class != $criticality">
<!--              <xsl:variable name="attrcriticality" select="@service_class"/> -->
              <xsl:message terminate="yes">
                <xsl:text>Group </xsl:text>
                <xsl:value-of select="key('hgid',$service/@hostgroup)/@name"/>
                <xsl:text> service </xsl:text>
                <xsl:value-of select="key('svctypid',$service/@service_type)/@name"/>
                <xsl:text> has criticality </xsl:text>
                <xsl:value-of select="key('criticalityid', $criticality)/@name"/>
                <xsl:text> but has </xsl:text>
                <xsl:value-of select="$attypname"/>
                <xsl:text> attribute with criticality </xsl:text>
                <xsl:value-of select="key('criticalityid', $attrcriticality)/@name"/>
              </xsl:message>
            </xsl:if>
            <xsl:if test="@service_responsibility and @service_responsibility != 'NULL' 
              and @service_responsibility != $responsibility">
<!--              <xsl:variable name="attrresponsibility" select="@service_responsibility"/> -->
              <xsl:message terminate="yes">
                <xsl:text>Group </xsl:text>
                <xsl:value-of select="key('hgid',$service/@hostgroup)/@name"/>
                <xsl:text> service </xsl:text>
                <xsl:value-of select="key('svctypid',$service/@service_type)/@name"/>
                <xsl:text> has responsibility </xsl:text>
                <xsl:value-of select="key('responsibilityid', $responsibility)/@name"/>
                <xsl:text> but has </xsl:text>
                <xsl:value-of select="$attypname"/>
                <xsl:text> attribute with responsibility </xsl:text>
                <xsl:value-of select="key('responsibilityid', $attrresponsibility)/@name"/>
              </xsl:message>
            </xsl:if>
            <!--                              <xsl:text>&#xA;             </xsl:text> -->
            <xsl:call-template name="format-attribute"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="(@service_class = 'NULL' or @service_class=$criticality) and (@service_responsibility = 'NULL' or @service_responsibility = $responsibility)">
              <!--                              <xsl:text>&#xA;             </xsl:text> -->
              <xsl:call-template name="format-attribute"/>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
<!--      <xsl:text>&#xA;             </xsl:text> -->
    </sage:period>
  </xsl:template>
  
  <xsl:template name="format-attribute">
    <xsl:param name="node" select="."/>
    <xsl:variable name="attypid" select="$node/@attribute_type"/>
    <xsl:variable name="attyp" select="key('attrtypid',$attypid)"/>
    <xsl:choose>
      <xsl:when test="$attyp/@value_has_usergroup = 1 and $node/@usergroup != 'NULL'">
        <xsl:variable name="ugroupid" select="$node/@usergroup"/>
        <xsl:for-each select="key('ugmemgrp',$ugroupid)">
          <xsl:variable name="uid" select="@user"/>
          <xsl:if test="position() != 1">
<!--            <xsl:text>&#xA;                </xsl:text> -->
          </xsl:if>
          <xsl:element name="attr:{$attyp/@name}">
            <xsl:attribute name="user">
              <xsl:value-of select="key('userid',$uid)/@name"/>
            </xsl:attribute>
            <xsl:if test="$attyp/@value_has_string = 1 and $node/@string != 'NULL'">
              <xsl:attribute name="string">
                <xsl:value-of select="$node/@string"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="$attyp/@value_has_number = 1 and $node/@number != 'NULL'">
              <xsl:attribute name="number">
                <xsl:value-of select="$node/@number"/>
              </xsl:attribute>
            </xsl:if>
          </xsl:element>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="attr:{$attyp/@name}">
          <xsl:if test="$attyp/@value_has_string = 1 and $node/@string != 'NULL'">
            <xsl:attribute name="string">
              <xsl:value-of select="$node/@string"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_number = 1 and $node/@number != 'NULL'">
            <xsl:attribute name="number">
              <xsl:value-of select="$node/@number"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_time = 1 and $node/@time != 'NULL'">
            <xsl:attribute name="time">
              <xsl:value-of select="$node/@time"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_range = 1 and $node/@range_start != 'NULL'">
            <xsl:attribute name="range_start">
              <xsl:value-of select="$node/@range_start"/>
            </xsl:attribute>
            <xsl:attribute name="range_end">
              <xsl:value-of select="$node/@range_end"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_boolean = 1 and $node/@boolean != 'NULL'">
            <xsl:attribute name="boolean">
              <xsl:value-of select="$node/@boolean"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_period = 1 and $node/@period != 'NULL'">
            <xsl:variable name="period" select="$node/@period"/>
            <xsl:variable name="pnode" select="key('subperid',$period)"/>
            <xsl:if test="count($pnode) != 1">
              <xsl:message terminate="yes">
                <xsl:value-of select="concat('Cannot find period node ', $period)"/>
              </xsl:message>
            </xsl:if>
            <xsl:if test="$pnode/@weekday_start and $pnode/@weekday_start!='NULL'">
              <xsl:attribute name="weekday_start">
                <xsl:value-of select="$pnode/@weekday_start"/>
              </xsl:attribute>
              <xsl:attribute name="weekday_end">
                <xsl:value-of select="$pnode/@weekday_end"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="$pnode/@time_start and $pnode/@time_start!='NULL'">
              <xsl:attribute name="time_start">
                <xsl:value-of select="$pnode/@time_start"/>
              </xsl:attribute>
              <xsl:attribute name="time_end">
                <xsl:value-of select="$pnode/@time_end"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="$pnode/@month_start and $pnode/@month_start!='NULL'">
              <xsl:attribute name="month_start">
                <xsl:value-of select="$pnode/@month_start"/>
              </xsl:attribute>
              <xsl:attribute name="month_end">
                <xsl:value-of select="$pnode/@month_end"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="$pnode/@monthday_start and $pnode/@monthday_start!='NULL'">
              <xsl:attribute name="monthday_start">
                <xsl:value-of select="$pnode/@monthday_start"/>
              </xsl:attribute>
              <xsl:attribute name="monthday_end">
                <xsl:value-of select="$pnode/@monthday_end"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="$pnode/@monthday_start and $pnode/@monthday_start!='NULL'">
              <xsl:attribute name="monthday_start">
                <xsl:value-of select="$pnode/@monthday_start"/>
              </xsl:attribute>
              <xsl:attribute name="monthday_end">
                <xsl:value-of select="$pnode/@monthday_end"/>
              </xsl:attribute>
            </xsl:if>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_action = 1 and $node/@action != 'NULL'">
            <xsl:variable name="action" select="$node/@action"/>
            <xsl:attribute name="action">
              <xsl:value-of select="key('actionid',$action)/@name"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$attyp/@value_has_host = 1 and $node/@host != 'NULL'">
            <xsl:attribute name="host">
              <xsl:value-of select="$node/@host"/>
            </xsl:attribute>
          </xsl:if>
          <!-- Could do defaulting this way
          <xsl:if test="$attyp/@value_has_usergroup = 1 and $node/@user == 'NULL'">
            <xsl:attribute name="user">
              <xsl:value-of select="'any'"/>
            </xsl:attribute>
          </xsl:if>
          -->
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="extract-depends">
    <xsl:param name="root" select="''"/>
    <xsl:variable name="node" select="key('depid',$root)"/>
    <xsl:if test="count($node) != 1">
      <xsl:message terminate="yes">
        <xsl:value-of select="concat('Cannot find dependency node ', $root)"/>
      </xsl:message>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="$node/@oper ='AND'">
        <sage:dep-and>
          <xsl:call-template name="extract-depends2">
            <xsl:with-param name="node" select="$node"/>
          </xsl:call-template>
        </sage:dep-and>
      </xsl:when>
      <xsl:when test="$node/@oper ='OR'">
        <sage:dep-or>
          <xsl:call-template name="extract-depends2">
            <xsl:with-param name="node" select="$node"/>
          </xsl:call-template>
        </sage:dep-or>
      </xsl:when>
      <xsl:when test="$node/@oper ='NOT'">
        <sage:dep-not>
          <xsl:call-template name="extract-depends2">
            <xsl:with-param name="node" select="$node"/>
          </xsl:call-template>
        </sage:dep-not>
      </xsl:when>
      <xsl:when test="$node/@oper ='NONE'">
        <xsl:call-template name="extract-depends2">
          <xsl:with-param name="node" select="$node"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:value-of select="concat('cannot process dependency node with operator ', $node/@oper)"/>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="extract-depends2">
    <xsl:param name="node" select="."/>
    <xsl:if test="$node/@left_type='LEAF'">
      <xsl:call-template name="extract-depends3">
        <xsl:with-param name="link" select="$node/@left_service_link"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$node/@right_type='LEAF'">
      <xsl:call-template name="extract-depends3">
        <xsl:with-param name="link" select="$node/@right_service_link"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$node/@left_type='TREE'">
      <xsl:call-template name="extract-depends">
        <xsl:with-param name="root" select="$node/@left_dep_link"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$node/@right_type='TREE'">
      <xsl:call-template name="extract-depends">
        <xsl:with-param name="root" select="$node/@right_dep_link"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="extract-depends3">
    <xsl:param name="link" select="0"/>
    <xsl:variable name="service" select="key('svcid',$link)"/>
    <sage:dep-svc>
      <xsl:attribute name="hostgroup">
        <xsl:value-of select="key('hgid',$service/@hostgroup)/@name"/>
      </xsl:attribute>
      <xsl:attribute name="service">
        <xsl:value-of select="key('svctypid',$service/@service_type)/@name"/>
      </xsl:attribute>
    </sage:dep-svc>
  </xsl:template>
  
</xsl:transform>

