AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOCoreBPMN / src / main / resources / normalize-namespaces.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3   ============LICENSE_START=======================================================
4   ECOMP MSO
5   ================================================================================
6   Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7   ================================================================================
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11   
12        http://www.apache.org/licenses/LICENSE-2.0
13   
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19   ============LICENSE_END=========================================================
20   -->
21
22 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" version="2.0" extension-element-prefixes="exsl">
23   <!--
24    Select one namespace node for each unique URI (almost), excluding
25    the implicit "xml" namespace. This does not filter out namespace
26    nodes declared on the same element with the same URI (thanks to
27    limitations of XPath 1.0); we take care of that later... Note that if
28    we had a distinct() function, this would be much, much simpler,
29    e.g. distinct(//namespace::*)
30   -->
31   <xsl:variable name="almost-unique-uri-namespace-nodes" select="//namespace::*[name()!='xml'][not(.=../preceding::*/namespace::* or .=ancestor::*[position()&gt;1]/namespace::*)]"/>
32   
33   <!-- EXSLT functions are not supported by Saxon HE.  Define the function we need here -->
34   <xsl:function name="exsl:node-set" as="node()">
35     <xsl:param name="n" as="node()"/>
36     <xsl:sequence select="$n"/>
37   </xsl:function>
38
39   <!-- Create a table of URI-prefix bindings -->
40   <xsl:variable name="almost-unique-uri-bindings-tree">
41     <xsl:for-each select="$almost-unique-uri-namespace-nodes">
42       <binding>
43         <prefix>
44           <xsl:choose>
45             <!--
46              If there are any unqualified element names or
47              attributes in this namespace in our document,
48              then force default namespaces to use an arbitrary
49              prefix, because we want to guarantee that the
50              only namespace declarations in our result will
51              be attached to the root element.
52             -->
53             <xsl:when test="not(name()) and (//*[namespace-uri()=''] or //@*[namespace-uri()=current()])">
54               <xsl:variable name="alternate-prefix-candidate" select="//namespace::*[count(.|current())!=1][.=current()][name()!=''][1]"/>
55               <xsl:choose>
56                 <xsl:when test="$alternate-prefix-candidate">
57                   <xsl:value-of select="name($alternate-prefix-candidate)"/>
58                 </xsl:when>
59                 <xsl:otherwise>
60                   <!--
61                    If no alternative candidates exist, then generate a
62                    "random" one.
63                   -->
64                   <xsl:value-of select="generate-id()"/>
65                 </xsl:otherwise>
66               </xsl:choose>
67             </xsl:when>
68             <xsl:otherwise>
69               <xsl:value-of select="name()"/>
70             </xsl:otherwise>
71           </xsl:choose>
72         </prefix>
73         <uri>
74           <xsl:value-of select="."/>
75         </uri>
76       </binding>
77     </xsl:for-each>
78   </xsl:variable>
79
80   <!-- Select the first binding from the table for each unique URI -->
81   <xsl:variable name="unique-uri-bindings" select="exsl:node-set($almost-unique-uri-bindings-tree)/binding[not(uri=preceding::uri)]"/>
82
83   <!--
84    Since there is no <xsl:namespace/> instruction, the only way we
85    can create the namespace nodes we want is to create elements in
86    a certain namespace and with a certain (prefixed) name.
87   -->
88   <xsl:variable name="created-namespace-nodes-tree">
89     <xsl:for-each select="$unique-uri-bindings">
90       <xsl:variable name="prefix">
91         <xsl:choose>
92           <!-- Replace a duplicated prefix with a different prefix. -->
93           <xsl:when test="prefix=preceding::prefix">
94             <xsl:value-of select="generate-id()"/>
95           </xsl:when>
96           <xsl:otherwise>
97             <xsl:value-of select="prefix"/>
98           </xsl:otherwise>
99         </xsl:choose>
100       </xsl:variable>
101       <xsl:variable name="maybe-colon">
102         <xsl:if test="string($prefix)">:</xsl:if>
103       </xsl:variable>
104       <xsl:element name="{$prefix}{$maybe-colon}temporary" namespace="{uri}"/>
105     </xsl:for-each>
106   </xsl:variable>
107
108   <!--
109    Select all the namespace nodes from our temporary tree
110    of namespace-decorated elements.
111   -->
112   <xsl:variable name="created-namespace-nodes" select="exsl:node-set($created-namespace-nodes-tree)//namespace::*"/>
113
114   <!--
115    Do for the root element the same thing we do for every element,
116    except that we explicitly copy all of our namespace nodes onto
117    the root element, eliminating the need for namespace declarations
118    to appear anywhere else in the output.
119   -->
120   <xsl:template match="/*">
121     <xsl:call-template name="copy">
122       <xsl:with-param name="insert-namespace-declarations" select="true()"/>
123     </xsl:call-template>
124     <!-- <xsl:call-template name="do-xsl-message-diagnostics"/> -->
125   </xsl:template>
126
127   <!--
128    For each element, create a new element with the same expanded name,
129    but not necessarily the same QName. We create a new element instead
130    of copying the original, because, besides potentially having a
131    QName we don't want, a copy would include with it all of the
132    namespace nodes attached to the original, and we don't necessarily
133    want that.
134   -->
135   <xsl:template match="*" name="copy">
136     <xsl:param name="insert-namespace-declarations"/>
137     <xsl:variable name="prefix" select="name($created-namespace-nodes[.=namespace-uri(current())])"/>
138     <xsl:variable name="maybe-colon">
139       <xsl:if test="$prefix">:</xsl:if>
140     </xsl:variable>
141     <xsl:element name="{$prefix}{$maybe-colon}{local-name()}" namespace="{namespace-uri()}">
142       <xsl:if test="$insert-namespace-declarations">
143         <xsl:copy-of select="$created-namespace-nodes"/>
144       </xsl:if>
145       <xsl:apply-templates select="@*|node()"/>
146     </xsl:element>
147   </xsl:template>
148
149   <!--
150    For each attribute, create a new attribute with the same expanded
151    name, but not necessarily the same QName.
152   -->
153   <xsl:template match="@*">
154     <xsl:variable name="prefix" select="name($created-namespace-nodes[.=namespace-uri(current())])"/>
155     <xsl:variable name="maybe-colon">
156       <xsl:if test="$prefix">:</xsl:if>
157     </xsl:variable>
158     <xsl:attribute name="{$prefix}{$maybe-colon}{local-name()}" namespace="{namespace-uri()}">
159       <xsl:value-of select="."/>
160     </xsl:attribute>
161   </xsl:template>
162
163   <!-- Do a simple copy of text, comments, and processing instructions -->
164   <xsl:template match="text()|comment()|processing-instruction()">
165     <xsl:copy/>
166   </xsl:template>
167
168   <!-- Print out some diagnostics to show what's going on beneath the covers. -->
169   <xsl:template name="do-xsl-message-diagnostics">
170     <xsl:message>
171       <diagnostics xml:space="preserve">
172         <diagnostic name="almost-unique-uri-bindings-tree">
173           <xsl:copy-of select="$almost-unique-uri-bindings-tree"/>
174         </diagnostic>
175         <diagnostic name="unique-uri-bindings">
176           <xsl:copy-of select="$unique-uri-bindings"/>
177         </diagnostic>
178         <diagnostic name="created-namespace-nodes-tree">
179           <xsl:copy-of select="$created-namespace-nodes-tree"/>
180         </diagnostic>
181       </diagnostics>
182     </xsl:message>
183   </xsl:template>
184
185 </xsl:transform>