<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" encoding="utf-8"  />
  
  <xsl:template match="/">
    <assessment_report:AssessmentReport xsi:schemaLocation="http://metadata.dod.mil/mdr/ns/netops/shared_data/assessment_report/0.41 assessment_report.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/geolocation/0.41 geolocation.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/organization/0.41 organization.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/ext_net_data/0.41 ext_net_data.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/person/0.41 person.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/ops_attributes/0.41 ops_attributes.xsd http://metadata.dod.mil/mdr/ns/netops/shared_data/device/0.41 device_record.xsd" 
                                        xmlns:assessment_report="http://metadata.dod.mil/mdr/ns/netops/shared_data/assessment_report/0.41" 
                                        xmlns:device="http://metadata.dod.mil/mdr/ns/netops/shared_data/device/0.41" 
                                        xmlns:cpe-record_0.1="http://scap.nist.gov/schema/cpe-record/0.1" 
                                        xmlns:ext_net_data="http://metadata.dod.mil/mdr/ns/netops/shared_data/ext_net_data/0.41" 
                                        xmlns:tagged_value="http://metadata.dod.mil/mdr/ns/netops/shared_data/tagged_value/0.41" 
                                        xmlns:geoloc="http://metadata.dod.mil/mdr/ns/netops/shared_data/geolocation/0.41" 
                                        xmlns:ops_attributes="http://metadata.dod.mil/mdr/ns/netops/shared_data/ops_attributes/0.41" 
                                        xmlns:organization="http://metadata.dod.mil/mdr/ns/netops/shared_data/organization/0.41" 
                                        xmlns:cndc="http://metadata.dod.mil/mdr/ns/netops/net_defense/cnd-core/0.41" 
                                        xmlns:person="http://metadata.dod.mil/mdr/ns/netops/shared_data/person/0.41" 
                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                                        xmlns:p="http://placeholder/ianaprotocols">

      <xsl:for-each select="/nmaprun/host">
        <assessment_report:reportObject>
          <assessment_report:device>
            <device:device_ID>
              <cndc:resource></cndc:resource>
              <cndc:record_identifier></cndc:record_identifier>
            </device:device_ID>
            <device:identifiers>
              <xsl:for-each select="hostnames/hostname">
                <xsl:choose>
                  <xsl:when test="@type = 'PTR'">
                    <device:FQDN>
                      <device:realm></device:realm>
                      <device:host_name>
                        <xsl:value-of select="@name"/>
                      </device:host_name>
                    </device:FQDN>
                  </xsl:when>
                </xsl:choose>
              </xsl:for-each>
            </device:identifiers>
            <device:configuration>
              <device:network_configuration>
                <device:network_interface_ID></device:network_interface_ID>
                <xsl:for-each select="address[@addrtype != '']">
                  <device:host_network_data>
                    <xsl:choose>
                      <xsl:when test="@addrtype = 'ipv4'">
                        <device:connection_ip>
                          <cndc:IPv4>
                            <xsl:value-of select="@addr"/>
                          </cndc:IPv4>
                        </device:connection_ip>
                      </xsl:when>
                      <xsl:when test="@addrtype = 'ipv6'">
                        <device:connection_ip>
                          <cndc:IPv6>
                            <xsl:value-of select="@addr"/>
                          </cndc:IPv6>
                        </device:connection_ip>
                      </xsl:when>
                      <xsl:when test="@addrtype = 'mac'">
                        <device:connection_mac_address>
                          <xsl:value-of select="@addr"/>
                        </device:connection_mac_address>
                      </xsl:when>
                    </xsl:choose>
                  </device:host_network_data>
                </xsl:for-each>
              </device:network_configuration>
              <device:cpe_inventory>
                <!-- This platformName is available but not in cpe format ... -->
                <!--<xsl:for-each select="os/osmatch">
                  <device:cpe_record>
                    <cpe-record_0.1:platformName >
                      <cpe-record_0.1:assessedName name="{@name}" />
                    </cpe-record_0.1:platformName>
                  </device:cpe_record>
                </xsl:for-each>-->
                <xsl:for-each select="ports/port">
                  <device:cpe_record>
                    <xsl:element name="cpe-record_0.1:service">
                      <xsl:attribute name="name">
                        <xsl:value-of select="service/@product"/> 
                      </xsl:attribute>
                      <xsl:attribute name="active">true</xsl:attribute>
                      <cpe-record_0.1:protocolAndPortRange protocol="{@protocol}">
                        <xsl:attribute name="protocol">
                          <xsl:choose>
                            <xsl:when test="@protocol = 'tcp'">6</xsl:when>
                            <xsl:when test="@protocol = 'udp'">17</xsl:when>
                            <xsl:when test="@protocol = 'ip'">4</xsl:when>
                          </xsl:choose>
                        </xsl:attribute>
                        <cpe-record_0.1:portRange>
                          <cpe-record_0.1:portIsARange>false</cpe-record_0.1:portIsARange>
                          <cpe-record_0.1:portStart>
                            <xsl:value-of select="@portid"/>
                          </cpe-record_0.1:portStart>
                          <cpe-record_0.1:portStop>
                            <xsl:value-of select="@portid"/>
                          </cpe-record_0.1:portStop>
                        </cpe-record_0.1:portRange>
                      </cpe-record_0.1:protocolAndPortRange>
                    </xsl:element>
                  </device:cpe_record>
                </xsl:for-each>
              </device:cpe_inventory>
            </device:configuration>
          </assessment_report:device>
        </assessment_report:reportObject> 
      </xsl:for-each>
      
    </assessment_report:AssessmentReport>
  </xsl:template>
</xsl:stylesheet>


