Debian Codegraph @

xsl/xccdf_1.1_to_1.2.xsl

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!--
4 Copyright 2012 Red Hat Inc., Durham, North Carolina.
5 All Rights Reserved.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 Authors:
22 Martin Preisler <[email protected]>
23 -->
24
25 <xsl:stylesheet
26 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
27 xmlns:xccdf_11="http://checklists.nist.gov/xccdf/1.1"
28 xmlns:xccdf_12="http://checklists.nist.gov/xccdf/1.2"
29
30 version="1.0">
31
32 <xsl:output method="xml" indent="yes"/>
33
34 <xsl:param name="reverse_DNS"/>
35
36 <xsl:key name="profiles" match="/xccdf_11:Benchmark/xccdf_11:Profile" use="@id"/>
37 <xsl:key name="rules" match="/xccdf_11:Benchmark//xccdf_11:Rule" use="@id"/>
38 <xsl:key name="groups" match="/xccdf_11:Benchmark//xccdf_11:Group" use="@id"/>
39 <xsl:key name="values" match="/xccdf_11:Benchmark//xccdf_11:Value" use="@id"/>
40
41 <!-- This is the generic matched template that by default copies everything
42 verbatim, every template that is more specifically matched that this
43 will take precedence -->
44 <xsl:template match="@*|node()">
45 <xsl:copy>
46 <xsl:apply-templates select="@*|node()"/>
47 </xsl:copy>
48 </xsl:template>
49
50 <!-- Deals with namespace change (xccdf 1.1 -> xccdf 1.2) -->
51 <xsl:template match="xccdf_11:*"> <!-- select just elements from xccdf 1.1 -->
52 <xsl:choose>
53 <!-- Remove deprecated elements -->
54 <xsl:when test="local-name()='platform-definitions' or local-name()='Platform-Specification' or local-name()='cpe-list' or local-name()='impact-metric'">
55 <!-- We will do a trick here to "comment" the deprecated elements,
56 XSLT itself ignores all nodes inside <xsl:comment> except TEXT
57 nodes, so we can't use that. -->
58 <xsl:text disable-output-escaping="yes">&lt;--</xsl:text>
59 This element has been deprecated and was removed from XCCDF 1.2!
60
61 <xsl:element name="{local-name()}">
62 <xsl:apply-templates select="@*"/>
63 <xsl:apply-templates/>
64 </xsl:element>
65 <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
66 </xsl:when>
67 <xsl:otherwise>
68 <xsl:element name="{local-name()}" namespace="http://checklists.nist.gov/xccdf/1.2">
69 <!-- We have basically added an element with the same local name but xccdf 1.2 namespace,
70 now we want to apply templates to all attributes of it and all subelements -->
71 <xsl:apply-templates select="@*"/>
72 <xsl:apply-templates/>
73 </xsl:element>
74 </xsl:otherwise>
75 </xsl:choose>
76 </xsl:template>
77
78 <!-- Deals with the new enforced id format of Benchmark -->
79 <xsl:template match="@id[parent::xccdf_11:Benchmark]">
80 <xsl:variable name="old_id" select="."/>
81
82 <xsl:attribute name="id">
83 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_benchmark_', $old_id)"/>
84 </xsl:attribute>
85 </xsl:template>
86
87 <!-- Deals with the new enforced id format of Rule -->
88 <xsl:template match="@id[parent::xccdf_11:Profile]">
89 <xsl:variable name="old_id" select="."/>
90
91 <xsl:attribute name="id">
92 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_profile_', $old_id)"/>
93 </xsl:attribute>
94 </xsl:template>
95
96 <!-- Deals with the new enforced id format of Rule -->
97 <xsl:template match="@id[parent::xccdf_11:Rule]">
98 <xsl:variable name="old_id" select="."/>
99
100 <xsl:attribute name="id">
101 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_rule_', $old_id)"/>
102 </xsl:attribute>
103 </xsl:template>
104
105 <!-- Deals with the new enforced id format of Group -->
106 <xsl:template match="@id[parent::xccdf_11:Group]">
107 <xsl:variable name="old_id" select="."/>
108
109 <xsl:attribute name="id">
110 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_group_', $old_id)"/>
111 </xsl:attribute>
112 </xsl:template>
113
114 <!-- Deals with the new enforced id format of Value -->
115 <xsl:template match="@id[parent::xccdf_11:Value]">
116 <xsl:variable name="old_id" select="."/>
117
118 <xsl:attribute name="id">
119 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_value_', $old_id)"/>
120 </xsl:attribute>
121 </xsl:template>
122
123 <!-- Deals with the new enforced id format of TestResult -->
124 <xsl:template match="@id[parent::xccdf_11:TestResult]">
125 <xsl:variable name="old_id" select="."/>
126
127 <xsl:attribute name="id">
128 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_testresult_', $old_id)"/>
129 </xsl:attribute>
130 </xsl:template>
131
132 <!-- Change style from SCAP_1.1 to SCAP_1.2 -->
133 <xsl:template match="@style[parent::xccdf_11:Benchmark][.='SCAP_1.1']">
134 <xsl:attribute name="style">SCAP_1.2</xsl:attribute>
135 </xsl:template>
136
137 <!-- idrefs -->
138 <!-- we want to migrate all @idrefs except platform's, we don't migrate requires/@idref here, it's handled elsewhere -->
139 <xsl:template match="@idref[parent::xccdf_11:*][not(parent::xccdf_11:platform)][not(parent::xccdf_11:requires)]">
140 <xsl:variable name="old_idref" select="."/>
141
142 <xsl:attribute name="idref">
143 <!-- Our goal here is to figure out which item the idref references and
144 change its format accordingly -->
145
146 <xsl:choose>
147 <xsl:when test="key('profiles', $old_idref)">
148 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_profile_', $old_idref)"/>
149 </xsl:when>
150
151 <xsl:when test="key('rules', $old_idref)">
152 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_rule_', $old_idref)"/>
153 </xsl:when>
154
155 <xsl:when test="key('groups', $old_idref)">
156 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_group_', $old_idref)"/>
157 </xsl:when>
158
159 <xsl:when test="key('values', $old_idref)">
160 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_value_', $old_idref)"/>
161 </xsl:when>
162
163 <xsl:otherwise>
164 <xsl:value-of select="concat('dangling reference to ', $old_idref, '!')"/>
165 </xsl:otherwise>
166 </xsl:choose>
167 </xsl:attribute>
168 </xsl:template>
169
170 <xsl:template name="migrate-requires">
171 <!-- The string parameter represents text content of the requires attribute
172 It needs to have ' ' appended for this template to work! -->
173
174 <xsl:param name="string"/>
175 <xsl:choose>
176 <!-- requires contain space delimited references, we will strip them one by one -->
177 <xsl:when test="$string=''">
178 <!-- we are done -->
179 </xsl:when>
180 <xsl:otherwise>
181 <xsl:choose>
182 <xsl:when test="key('groups', substring-before($string, ' '))">
183 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_group_', substring-before($string, ' '))"/>
184 </xsl:when>
185 <xsl:when test="key('rules', substring-before($string, ' '))">
186 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_rule_', substring-before($string, ' '))"/>
187 </xsl:when>
188 <xsl:otherwise>
189 <xsl:value-of select="concat('dangling reference to ', substring-before($string, ' '), '!')"/>
190 </xsl:otherwise>
191 </xsl:choose>
192
193 <!-- we test whether the tail is empty to avoid inserting one extra space -->
194 <xsl:variable name="tail" select="substring-after($string, ' ')"/>
195 <xsl:if test="$tail!=''">
196 <xsl:text> </xsl:text>
197 <xsl:call-template name="migrate-requires">
198 <xsl:with-param name="string" select="$tail"/>
199 </xsl:call-template>
200 </xsl:if>
201 </xsl:otherwise>
202 </xsl:choose>
203 </xsl:template>
204
205 <xsl:template match="@idref[parent::xccdf_11:requires]">
206 <!-- We intentionally concat @idref and a space, this is required for the called template to work properly -->
207 <xsl:variable name="old_idref" select="."/>
208 <xsl:attribute name="idref"><xsl:call-template name="migrate-requires"><xsl:with-param name="string" select="concat($old_idref, ' ')"/></xsl:call-template></xsl:attribute>
209 </xsl:template>
210
211 <!-- value-ids -->
212 <xsl:template match="@value-id[parent::xccdf_11:*]">
213 <xsl:variable name="old_value_id" select="."/>
214
215 <xsl:attribute name="value-id">
216 <!-- Our goal here is to figure out which item the value-id references and
217 change its format accordingly -->
218
219 <xsl:choose>
220 <xsl:when test="key('values', $old_value_id)">
221 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_value_', $old_value_id)"/>
222 </xsl:when>
223
224 <xsl:otherwise>
225 <xsl:value-of select="concat('dangling reference to ', $old_value_id, '!')"/>
226 </xsl:otherwise>
227 </xsl:choose>
228 </xsl:attribute>
229 </xsl:template>
230
231 <!-- Group/@extends -->
232 <!-- If present, update extends attribute -->
233 <xsl:template match="@extends[parent::xccdf_11:Group]">
234 <xsl:variable name="old_extends" select="."/>
235
236 <xsl:attribute name="extends">
237 <xsl:choose>
238 <xsl:when test="key('groups', $old_extends)">
239 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_group_', $old_extends)"/>
240 </xsl:when>
241 <xsl:otherwise>
242 <xsl:value-of select="concat('dangling reference to ', $old_extends, '!')"/>
243 </xsl:otherwise>
244 </xsl:choose>
245 </xsl:attribute>
246 </xsl:template>
247
248 <!-- Rule/@extends -->
249 <!-- If present, update extends attribute -->
250 <xsl:template match="@extends[parent::xccdf_11:Rule]">
251 <xsl:variable name="old_extends" select="."/>
252
253 <xsl:attribute name="extends">
254 <xsl:choose>
255 <xsl:when test="key('rules', $old_extends)">
256 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_rule_', $old_extends)"/>
257 </xsl:when>
258 <xsl:otherwise>
259 <xsl:value-of select="concat('dangling reference to ', $old_extends, '!')"/>
260 </xsl:otherwise>
261 </xsl:choose>
262 </xsl:attribute>
263 </xsl:template>
264
265 <!-- Value/@extends -->
266 <!-- If present, update extends attribute -->
267 <xsl:template match="@extends[parent::xccdf_11:Value]">
268 <xsl:variable name="old_extends" select="."/>
269
270 <xsl:attribute name="extends">
271 <xsl:choose>
272 <xsl:when test="key('values', $old_extends)">
273 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_value_', $old_extends)"/>
274 </xsl:when>
275 <xsl:otherwise>
276 <xsl:value-of select="concat('dangling reference to ', $old_extends, '!')"/>
277 </xsl:otherwise>
278 </xsl:choose>
279 </xsl:attribute>
280 </xsl:template>
281
282 <!-- Profile/@extends -->
283 <!-- If present, update extends attribute -->
284 <xsl:template match="@extends[parent::xccdf_11:Profile]">
285 <xsl:variable name="old_extends" select="."/>
286
287 <xsl:attribute name="extends">
288 <xsl:choose>
289 <xsl:when test="key('profiles', $old_extends)">
290 <xsl:value-of select="concat('xccdf_', $reverse_DNS, '_profile_', $old_extends)"/>
291 </xsl:when>
292 <xsl:otherwise>
293 <xsl:value-of select="concat('dangling reference to ', $old_extends, '!')"/>
294 </xsl:otherwise>
295 </xsl:choose>
296 </xsl:attribute>
297 </xsl:template>
298
299 <!-- add the @legacy attribute to <sub> elements -->
300 <xsl:template match="xccdf_11:sub">
301 <xsl:element name="{local-name()}" namespace="http://checklists.nist.gov/xccdf/1.2">
302 <xsl:apply-templates select="@*"/>
303 <xsl:apply-templates/>
304 <xsl:attribute name="use">legacy</xsl:attribute>
305 </xsl:element>
306 </xsl:template>
307 </xsl:stylesheet>
308