summaryrefslogtreecommitdiffstats
path: root/xml/upgrade-2.10-leave.xsl
blob: 6e3fe65eda4fe6511013eb69cdbea97a1235d062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!--
 Copyright 2018 Red Hat, Inc.
 Author: Jan Pokorny <jpokorny@redhat.com>
 Part of pacemaker project
 SPDX-License-Identifier: GPL-2.0-or-later
 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:cibtr="http://clusterlabs.org/ns/pacemaker/cibtr-2"
                exclude-result-prefixes="cibtr"
		cibtr:filename="upgrade-2.10-leave.xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

<xsl:param name="cibtr:label-debug"   select="'DEBUG: '"/>

<!--

 GENERIC UTILITIES

 -->

<!--
 Recursive implementation of "basename"

 Merely parameter driven, no implicit context taken into account:
 - Uri: input in it's current phase of processing
-->
<xsl:template name="cibtr:WrapSpecificBasename">
  <xsl:param name="Uri"/>
  <xsl:choose>
    <xsl:when test="not(contains($Uri, '/'))">
      <xsl:value-of select="$Uri"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="cibtr:WrapSpecificBasename">
        <xsl:with-param name="Uri"
                        select="substring-after($Uri, '/')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--
 Id-mangling-enriched identity template.
 -->
<xsl:template name="cibtr:HelperIdentityLeave">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="cibtr:leave"/>
  </xsl:copy>
</xsl:template>


<!--

 ACTUAL TRANSFORMATION

 Extra modes: cibtr:leave
              cibtr:leave-serialize

 -->

<xsl:variable name="cibtr:WrapSpecificPrefix">
  <!-- no sleek way to fetch this, top-level xmlns:cibtr disappears early -->
  <xsl:call-template name="cibtr:WrapSpecificBasename">
    <xsl:with-param name="Uri"
      select="namespace-uri(document('')/xsl:stylesheet/@cibtr:filename)"/>
  </xsl:call-template>
</xsl:variable>

<xsl:variable name="cibtr:WrapSpecificPrefixInitialRoot"
              select="concat('_', $cibtr:WrapSpecificPrefix, '_')"/>

<!--
 cibtr:leave-serialize mode
 -->

<xsl:template match="@*|node()" mode="cibtr:leave-serialize">
  <xsl:choose>
    <xsl:when test="count(.|../@*)=count(../@*)
                    and
                    name() = 'id'">
      <!-- intentionally skip -->
    </xsl:when>
    <xsl:when test="count(.|../@*)=count(../@*)
                    or
                    self::processing-instruction()">
      <xsl:variable name="Mark">
        <xsl:if test="count(.|../@*)=count(../@*)">A</xsl:if>
        <xsl:if test="self::processing-instruction()">P</xsl:if>
      </xsl:variable>
      <xsl:value-of select="concat($Mark, '(', name(), '=', ., '),')"/>
    </xsl:when>
    <xsl:when test="self::*">
      <xsl:value-of select="concat('E(', name(), ',')"/>
      <xsl:apply-templates select="@*|node()"
                           mode="cibtr:leave-serialize"/>
      <xsl:value-of select="'),'"/>
    </xsl:when>
    <xsl:when test="self::comment()|self::text()">
      <xsl:variable name="Mark">
        <xsl:if test="self::comment()">C</xsl:if>
        <xsl:if test="self::text()">T</xsl:if>
      </xsl:variable>
      <xsl:value-of select="concat($Mark, '(', ., '),')"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<!--
 cibtr:leave mode
 -->

<!--
 This is to cover elements with the internal structure characterized
 with the following RelaxNG Compact encoded grammar:

 > attribute id-ref { xsd:IDREF }
 > | (attribute id { xsd:ID },
 >    (rule?
 >     & nvpair*
 >     & attribute score {
 >         xsd:integer
 >         | xsd:token "INFINITY"
 >         | xsd:token "+INFINITY"
 >         | xsd:token "-INFINITY"
 >       }?))

 The context node corresponds to "@id-ref" branch prior to unfolding
 in the preceding "unfold" XSLT counterpart, and Original to the other,
 and the task here is to recursively (and deterministically) compare
 the content of the two, and when there's a match, to collapse the
 former back to a mere empty @id-ref link.

 NOTE: name of this template may be a bit misleading, but it's meant
       to really mean "after upgrade of the same trailing numbers",
       which effectively means we are playing per 3.X CIB schema
       rules, which needs to be considered, should any element be
       renamed, etc.
 -->
<xsl:template match="*[
                       contains(
                         concat('|cluster_property_set',
                                '|instance_attributes|',
                                '|meta_attributes|'),
                         concat('|', name(), '|')
                       )
                     ]"
              mode="cibtr:leave">
  <xsl:variable name="Original"
                select="//*[
                          name() = name(current())
                          and
                          @id = substring-after(current()/@id,
                                                $cibtr:WrapSpecificPrefixInitialRoot)
                        ]"/>
  <xsl:choose>
    <xsl:when test="not(
                      starts-with(@id, $cibtr:WrapSpecificPrefixInitialRoot)
                    )">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"
                             mode="cibtr:leave"/>
      </xsl:copy>
    </xsl:when>
    <xsl:when test="count($Original) = 0">
      <xsl:if test="string($cibtr:label-debug) != string(false())">
        <xsl:message>
          <xsl:value-of select="concat($cibtr:label-debug, name(),
                                       ': original element pointed to with',
                                       ' @id-ref (',
                                       substring-after(@id,
                                                       $cibtr:WrapSpecificPrefixInitialRoot),
                                       ') disappeared during upgrade')"/>
        </xsl:message>
      </xsl:if>
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"
                             mode="cibtr:leave"/>
      </xsl:copy>
    </xsl:when>
    <xsl:when test="count($Original) != 1">
      <xsl:message terminate="yes">
        <xsl:value-of select="concat('INTERNAL ERROR:',
                                     name(), ': found several elements',
                                     ' that possibly were originally',
                                     ' pointed to with @id-ref (',
                                     substring-after(@id,
                                                     $cibtr:WrapSpecificPrefixInitialRoot),
                                     '); unexpected ambiguity')"/>
      </xsl:message>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="SerializedOriginal">
        <xsl:apply-templates select="$Original/@*[name() != 'id']
                                     |$Original/node()"
                             mode="cibtr:leave-serialize"/>
      </xsl:variable>
      <xsl:variable name="SerializedDependant">
        <xsl:apply-templates select="@*[name() != 'id']
                                     |node()"
                             mode="cibtr:leave-serialize"/>
      </xsl:variable>
      <xsl:copy>
        <xsl:choose>
          <xsl:when test="$SerializedOriginal = $SerializedDependant">
            <xsl:attribute name="id-ref">
              <xsl:value-of select="substring-after(@id,
                                                    $cibtr:WrapSpecificPrefixInitialRoot)"/>
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="@*|node()"
                                 mode="cibtr:leave">
            </xsl:apply-templates>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:copy>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="@*|node()" mode="cibtr:leave">
  <xsl:call-template name="cibtr:HelperIdentityLeave"/>
</xsl:template>

<!-- mode-less, easy to override kick-off -->
<xsl:template match="/">
  <xsl:call-template name="cibtr:HelperIdentityLeave"/>
</xsl:template>

</xsl:stylesheet>