<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:shibmeta="urn:mace:shibboleth:metadata:1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
                xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
		xmlns:mdattr="urn:oasis:names:tc:SAML:metadata:attribute"
		xmlns:samla="urn:oasis:names:tc:SAML:2.0:assertion"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl"
		xmlns:mdrpi="urn:oasis:names:tc:SAML:metadata:rpi"
                xmlns:shibmd="urn:mace:shibboleth:metadata:1.0">

  <xsl:output method="text"/>
  <xsl:variable name="eol">
<xsl:text>|</xsl:text><xsl:text>
</xsl:text>
  </xsl:variable>
  
  <xsl:template name="entity-name">
    <xsl:variable name="h"><xsl:value-of select="substring-after(@entityID,'://')"/></xsl:variable>
    <xsl:if test="contains($h,'/')">
      <xsl:value-of select="substring-before($h,'/')"/>
    </xsl:if>
    <xsl:if test="not(contains($h,'/'))">
      <xsl:value-of select="$h"/>
    </xsl:if> 
  </xsl:template>

  <xsl:template match="/md:EntitiesDescriptor">
    <xsl:text>||EntityID||Service Name||Organization||Contacts||Entity Categories||Assurance Certification||Registration Authority||
</xsl:text>
    <xsl:apply-templates select="md:EntityDescriptor[md:SPSSODescriptor]"/>
  </xsl:template>

  <xsl:template match="md:EntityDescriptor">
     <xsl:text> | </xsl:text><xsl:value-of select="@entityID"/>
     <xsl:text> | </xsl:text>
     	<xsl:apply-templates select="md:SPSSODescriptor/md:AttributeConsumingService/md:ServiceName"/><xsl:text> \\ </xsl:text>
        <xsl:apply-templates select="md:SPSSODescriptor/md:AttributeConsumingService/md:ServiceDescription"/>
     <xsl:text> | </xsl:text><xsl:apply-templates select="md:Organization"/>
     <xsl:text> | </xsl:text>
     	<xsl:apply-templates select="md:ContactPerson[@contactType='technical']"/>
     	<xsl:apply-templates select="md:ContactPerson[@contactType='administrative']"/>
     	<xsl:apply-templates select="md:ContactPerson[@contactType='support']"/>
     	<xsl:apply-templates select="md:ContactPerson[@contactType='other']"/>
     <xsl:text> | </xsl:text><xsl:apply-templates select="md:Extensions/mdattr:EntityAttributes/samla:Attribute[@Name='http://macedir.org/entity-category']/samla:AttributeValue"/>
     <xsl:text> | </xsl:text><xsl:apply-templates select="md:Extensions/mdattr:EntityAttributes/samla:Attribute[@Name='urn:oasis:names:tc:SAML:attribute:assurance-certification']/samla:AttributeValue"/><xsl:text> </xsl:text>
     <xsl:text> | </xsl:text><xsl:apply-templates select="md:Extensions/mdrpi:RegistrationInfo"/>
     <xsl:text> |
</xsl:text>
  </xsl:template>

  <xsl:template match="mdrpi:RegistrationInfo">
    <xsl:value-of select="@registrationAuthority"/><xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="md:AttributeConsumingService">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="md:ServiceName|md:ServiceDescription">
     <xsl:call-template name="string-replace-all">
       <xsl:with-param name="text" select="text()" />
       <xsl:with-param name="replace" select="'|'" />
       <xsl:with-param name="by" select="'\|'" />
     </xsl:call-template>
  </xsl:template>

  <xsl:template match="samla:AttributeValue">
     <xsl:call-template name="string-replace-all">
       <xsl:with-param name="text" select="text()" />
       <xsl:with-param name="replace" select="'-'" />
       <xsl:with-param name="by" select="'-'" />
     </xsl:call-template>
     <xsl:text> \\</xsl:text>
  </xsl:template> 

  <xsl:template match="md:ContactPerson">
    <xsl:value-of select="@contactType"/><xsl:text>: </xsl:text><xsl:apply-templates select="md:EmailAddress"/>
  </xsl:template> 

  <xsl:template match="md:Organization">
    <xsl:text>[</xsl:text>
    <xsl:apply-templates select="md:OrganizationDisplayName[1]"/><xsl:text>|</xsl:text>
    <xsl:apply-templates select="md:OrganizationURL[1]"/><xsl:text>]</xsl:text>
  </xsl:template>

  <xsl:template match="md:SPPSSODescriptor">
    <xsl:apply-templates select="md:Extensions/shibmd:Scope"/>
  </xsl:template>

  <xsl:template match="md:EmailAddress">
     <xsl:text>[</xsl:text><xsl:value-of select="text()"/><xsl:text>] \\</xsl:text>
  </xsl:template>

  <xsl:template match="md:OrganizationDisplayName">
     <xsl:value-of select="normalize-space(text())"/>
  </xsl:template>

  <xsl:template match="md:OrganizationURL">
     <xsl:value-of select="normalize-space(text())"/>
  </xsl:template>

  <xsl:template match="shibmd:Scope">
     <xsl:value-of select="text()"/><xsl:text> </xsl:text>
  </xsl:template>


  <xsl:template match="*"/>
  <xsl:template match="text()"/>

  <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>