extensions need to go to the head
[swamid-metadata.git] / xslt / normalize.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0"
3                 xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
4                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6                 xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
7                 xmlns:exsl="http://exslt.org/common"
8                 xmlns:mdrpi="urn:oasis:names:tc:SAML:metadata:rpi"
9                 extension-element-prefixes="exsl"
10                 xmlns:shibmd="urn:mace:shibboleth:metadata:1.0">
11
12   <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
13
14   <xsl:template match="/md:EntitiesDescriptor">
15     <xsl:if test="$tou='true'"><xsl:comment>
16
17        IMPORTANT NOTICE: READ CAREFULLY
18
19 You are reading this because You wish to use the technical information (the “Metadata”) 
20 published on behalf of the Registrars. These Terms of Access and Use (these “Terms”) govern 
21 the use of the Metadata. By accessing or using the Metadata You accept that the access and 
22 use will be on and subject to these Terms. These Terms will be binding and enforceable on 
23 You as contractual obligations.
24
25 The SWAMID Metadata Terms of Access and Use can be found here:
26
27 - http://md.swamid.se/md/swamid-tou-sv.txt Swedish version - Legally Binding!
28 - http://md.swamid.se/md/swamid-tou-en.txt English version - Non-normative translation
29
30 If You do not accept these Terms, then You must not continue to use this Metadata.
31
32     </xsl:comment></xsl:if>
33     <md:EntitiesDescriptor>
34       <xsl:attribute name="ID"><xsl:value-of select="$ID"/></xsl:attribute>
35       <xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
36       <xsl:if test="@cacheDuration">
37          <xsl:attribute name="cacheDuration"><xsl:value-of select="@cacheDuration"/></xsl:attribute>
38       </xsl:if>
39       <xsl:if test="@validUntil">
40          <xsl:attribute name="validUntil"><xsl:value-of select="$date"/></xsl:attribute>
41       </xsl:if>
42       <xsl:if test="$target">
43         <md:Extensions><xsl:call-template name="add-swamid-pi"/></md:Extensions>
44       </xsl:if>
45       <xsl:apply-templates/>
46     </md:EntitiesDescriptor>
47   </xsl:template>
48
49   <xsl:template match="md:EntityDescriptor">
50     <xsl:variable name="path"><xsl:value-of select="substring-after(@entityID,'://')"/></xsl:variable>
51     <xsl:variable name="host">
52       <xsl:if test="contains($path,'/')">
53         <xsl:value-of select="substring-before($path,'/')"/>
54       </xsl:if>
55       <xsl:if test="not(contains($path,'/'))">
56         <xsl:value-of select="$path"/>
57       </xsl:if>
58     </xsl:variable>
59     <xsl:variable name="domain">
60        <xsl:value-of select="substring-after($host,'.')"/>
61     </xsl:variable>
62     <xsl:variable name="orginfo">
63        <xsl:value-of select="concat('../organization','/',$domain,'.xml')"/>
64     </xsl:variable>
65     <md:EntityDescriptor>
66        <xsl:apply-templates select="@*"/>
67        <xsl:if test="$rpi='true' and not(md:Extensions)">
68           <md:Extensions><xsl:call-template name="add-swamid-ri"/></md:Extensions>
69        </xsl:if>
70        <xsl:apply-templates select="text()|comment()|md:Extensions|md:RoleDescriptor|md:IDPSSODescriptor|md:SPSSODescriptor|md:AuthnAuthorityDescriptor|md:AttributeAuthorityDescriptor|md:PDPDescriptor|md:AffiliationDescriptor"/>
71        <xsl:if test="$org='true' and not(md:Organization)">
72           <xsl:apply-templates select="document($orginfo)"/>
73        </xsl:if>
74        <xsl:apply-templates select="md:Organization"/>
75        <xsl:if test="$defaultContact='true' and not(md:ContactPerson)">
76           <md:ContactPerson contactType="technical"><md:EmailAddress>operations@swamid.se</md:EmailAddress></md:ContactPerson>
77        </xsl:if>
78        <xsl:apply-templates select="md:ContactPerson"/>
79        <xsl:apply-templates select="md:AdditionalMetadataLocation"/>
80     </md:EntityDescriptor>
81   </xsl:template>
82
83   <xsl:template match="md:EntityDescriptor/md:Extensions">
84      <md:Extensions>
85        <xsl:call-template name="add-swamid-ri"/>
86        <xsl:apply-templates select="text()|comment()|node()"/>
87      </md:Extensions>
88   </xsl:template>
89
90   <xsl:template name="add-swamid-ri">
91      <xsl:if test="$rpi='true' and not(mdrpi:RegistrationInfo[@registrationAuthority='http://swamid.se/'])">
92        <mdrpi:RegistrationInfo registrationAuthority="http://www.swamid.se/">
93           <mdrpi:RegistrationPolicy xml:lang="en">http://www.swamid.se/download/18.248ad5af12aa8136533800012293/SWAMID+Metadata+Registration+Practice+Statement-20110714.pdf</mdrpi:RegistrationPolicy>
94        </mdrpi:RegistrationInfo>
95      </xsl:if>
96   </xsl:template>
97
98   <xsl:template name="add-swamid-pi">
99      <mdrpi:PublicationInfo>
100         <xsl:attribute name="creationInstant"><xsl:value-of select="$now"/></xsl:attribute>
101         <xsl:attribute name="publisher"><xsl:value-of select="$target"/></xsl:attribute>
102         <mdrpi:UsagePolicy xml:lang="en">https://www.sunet.se/wp-content/uploads/2015/12/SWAMID-Terms-of-Use-20110714.pdf</mdrpi:UsagePolicy>
103      </mdrpi:PublicationInfo>
104   </xsl:template>
105
106   <xsl:template match="@xml:base|@ID|@validUntil|@cacheDuration"/>
107
108   <xsl:template match="text()|comment()|@*">
109     <xsl:copy/>
110   </xsl:template>
111   
112   <xsl:template match="*">
113     <xsl:copy>
114       <xsl:apply-templates select="node()|@*"/>
115     </xsl:copy>
116   </xsl:template>
117
118 </xsl:stylesheet>