summaryrefslogtreecommitdiffstats
path: root/xml/upgrade-2.10-enter.xsl
blob: e7eb1ba323109f03c6d5763a1eeda1975583b159 (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
<!--
 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-enter.xsl">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>


<!--

 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:HelperIdentityEnter">
  <xsl:param name="TargetIdPrefix" select="''"/>
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"
                         mode="cibtr:enter">
      <xsl:with-param name="TargetIdPrefix" select="$TargetIdPrefix"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>


<!--

 ACTUAL TRANSFORMATION

 Extra modes: cibtr:enter

 -->

<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:enter 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, and Original to the other,
 and the task here is to recursively copy anything from Original to target
 (with new, unique IDs, of course), and to flip @id-ref to full-fledged,
 now valid @id, which will be likewise unique, and importantly, reversibly
 mappable back to original in "leave" XSLT counterpart.
 -->
<xsl:template match="*[
                       @id-ref
                       and
                       contains(
                         concat('|cluster_property_set',
                                '|instance_attributes|',
                                '|meta_attributes|'),
                         concat('|', name(), '|')
                       )
                     ]"
              mode="cibtr:enter">
  <xsl:variable name="Original"
                select="//*[
                          name() = name(current())
                          and
                          @id = current()/@id-ref
                        ]"/>
  <xsl:choose>
    <xsl:when test="count($Original) = 0">
      <xsl:message terminate="yes">
        <xsl:value-of select="concat('INTERNAL ERROR:',
                                     name(), ': dangling @id-ref (',
                                     @id-ref, '): no such @id found',
                                     ' within the same element class')"/>
      </xsl:message>
    </xsl:when>
    <xsl:when test="count($Original) != 1">
      <xsl:message terminate="yes">
        <xsl:value-of select="concat('INTERNAL ERROR:',
                                     name(), ': dangling @id-ref (',
                                     @id-ref, '): more than one @id found',
                                     ' within the same element class')"/>
      </xsl:message>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy>
        <xsl:attribute name="id">
          <xsl:value-of select="concat($cibtr:WrapSpecificPrefixInitialRoot,
                                       $Original/@id)"/>
        </xsl:attribute>
        <xsl:apply-templates select="$Original/@*[name() != 'id']
                                     |$Original/node()"
                             mode="cibtr:enter">
          <xsl:with-param name="TargetIdPrefix"
                          select="concat('__', $cibtr:WrapSpecificPrefix, '_',
                                         $Original/@id, '__')"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--
 (uniformity as an unattainable goal)
 -->
<xsl:template match="@id[
                       name(..) != 'resource_ref'
                     ]"
              mode="cibtr:enter">
  <xsl:param name="TargetIdPrefix"/>
  <xsl:attribute name="{name()}">
    <xsl:value-of select="concat($TargetIdPrefix, .)"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="@*|node()" mode="cibtr:enter">
  <xsl:param name="TargetIdPrefix" select="''"/>
  <xsl:call-template name="cibtr:HelperIdentityEnter">
    <xsl:with-param name="TargetIdPrefix" select="$TargetIdPrefix"/>
  </xsl:call-template>
</xsl:template>

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

</xsl:stylesheet>