1 <?xml version="1.0" encoding="UTF-8"?>
3 ============LICENSE_START=======================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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">
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::*)
31 <xsl:variable name="almost-unique-uri-namespace-nodes" select="//namespace::*[name()!='xml'][not(.=../preceding::*/namespace::* or .=ancestor::*[position()>1]/namespace::*)]"/>
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"/>
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">
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.
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]"/>
56 <xsl:when test="$alternate-prefix-candidate">
57 <xsl:value-of select="name($alternate-prefix-candidate)"/>
61 If no alternative candidates exist, then generate a
64 <xsl:value-of select="generate-id()"/>
69 <xsl:value-of select="name()"/>
74 <xsl:value-of select="."/>
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)]"/>
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.
88 <xsl:variable name="created-namespace-nodes-tree">
89 <xsl:for-each select="$unique-uri-bindings">
90 <xsl:variable name="prefix">
92 <!-- Replace a duplicated prefix with a different prefix. -->
93 <xsl:when test="prefix=preceding::prefix">
94 <xsl:value-of select="generate-id()"/>
97 <xsl:value-of select="prefix"/>
101 <xsl:variable name="maybe-colon">
102 <xsl:if test="string($prefix)">:</xsl:if>
104 <xsl:element name="{$prefix}{$maybe-colon}temporary" namespace="{uri}"/>
109 Select all the namespace nodes from our temporary tree
110 of namespace-decorated elements.
112 <xsl:variable name="created-namespace-nodes" select="exsl:node-set($created-namespace-nodes-tree)//namespace::*"/>
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.
120 <xsl:template match="/*">
121 <xsl:call-template name="copy">
122 <xsl:with-param name="insert-namespace-declarations" select="true()"/>
124 <!-- <xsl:call-template name="do-xsl-message-diagnostics"/> -->
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
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>
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"/>
145 <xsl:apply-templates select="@*|node()"/>
150 For each attribute, create a new attribute with the same expanded
151 name, but not necessarily the same QName.
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>
158 <xsl:attribute name="{$prefix}{$maybe-colon}{local-name()}" namespace="{namespace-uri()}">
159 <xsl:value-of select="."/>
163 <!-- Do a simple copy of text, comments, and processing instructions -->
164 <xsl:template match="text()|comment()|processing-instruction()">
168 <!-- Print out some diagnostics to show what's going on beneath the covers. -->
169 <xsl:template name="do-xsl-message-diagnostics">
171 <diagnostics xml:space="preserve">
172 <diagnostic name="almost-unique-uri-bindings-tree">
173 <xsl:copy-of select="$almost-unique-uri-bindings-tree"/>
175 <diagnostic name="unique-uri-bindings">
176 <xsl:copy-of select="$unique-uri-bindings"/>
178 <diagnostic name="created-namespace-nodes-tree">
179 <xsl:copy-of select="$created-namespace-nodes-tree"/>