1 <?xml version="1.0"?> 2 <!-- 3 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU Lesser General Public License as published by the Free 7 Software Foundation; either version 3 of the License, or (at your option) any 8 later version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 13 details. 14 15 You should have received a copy of the GNU Lesser General Public License along 16 with this program. If not, see <http://www.gnu.org/licenses/>. 17 --> 18 <xsl:stylesheet version="1.0" 19 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 20 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template" 21 xmlns:str="http://exslt.org/strings" 22 extension-element-prefixes="str"> 23 24 <xsl:output indent="yes"/> 25 26 27 28 <!-- Match the document itself. --> 29 30 <xsl:template match="/"> 31 <!-- Process the elements. --> 32 <xsl:apply-templates select="*"/> 33 </xsl:template> 34 35 36 37 <!-- Match element references. --> 38 39 <xsl:template match="*[@template:element]" priority="1"> 40 <xsl:call-template name="enter-element"> 41 <xsl:with-param name="other-elements" select="@template:element"/> 42 <xsl:with-param name="other-init" select="@template:init"/> 43 </xsl:call-template> 44 </xsl:template> 45 46 <xsl:template name="enter-element"> 47 <xsl:param name="other-elements"/> 48 <xsl:param name="other-init"/> 49 <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> 50 <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> 51 <xsl:variable name="first-init" select="substring-before($other-init, ',')"/> 52 <xsl:variable name="remaining-init" select="substring-after($other-init, ',')"/> 53 <xsl:choose> 54 <xsl:when test="$first-element = ''"> 55 <xsl:call-template name="next-element"> 56 <xsl:with-param name="first-element" select="$other-elements"/> 57 <xsl:with-param name="first-init" select="$other-init"/> 58 </xsl:call-template> 59 </xsl:when> 60 <xsl:otherwise> 61 <xsl:call-template name="next-element"> 62 <xsl:with-param name="first-element" select="$first-element"/> 63 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 64 <xsl:with-param name="first-init" select="$first-init"/> 65 <xsl:with-param name="remaining-init" select="$remaining-init"/> 66 </xsl:call-template> 67 </xsl:otherwise> 68 </xsl:choose> 69 </xsl:template> 70 71 <xsl:template name="next-element"> 72 <xsl:param name="first-element"/> 73 <xsl:param name="remaining-elements"/> 74 <xsl:param name="first-init"/> 75 <xsl:param name="remaining-init"/> 76 <!-- Test for recursive references. --> 77 <xsl:variable name="recursive-element" select="ancestor::*[$first-element = str:split(@template:element, ',')[1]]"/> 78 <xsl:choose> 79 <!-- Generate a reference to the previous element definition. --> 80 <xsl:when test="$recursive-element"> 81 <element-ref> 82 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 83 </element-ref> 84 </xsl:when> 85 <!-- Generate a normal, nested element definition. --> 86 <xsl:otherwise> 87 <element> 88 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 89 <xsl:if test="$first-init != ''"> 90 <xsl:attribute name="init"><xsl:value-of select="$first-init"/></xsl:attribute> 91 </xsl:if> 92 <xsl:choose> 93 <xsl:when test="$remaining-elements = ''"> 94 <xsl:call-template name="enter-attribute"/> 95 </xsl:when> 96 <xsl:otherwise> 97 <xsl:call-template name="enter-element"> 98 <xsl:with-param name="other-elements" select="$remaining-elements"/> 99 <xsl:with-param name="other-init" select="$remaining-init"/> 100 </xsl:call-template> 101 </xsl:otherwise> 102 </xsl:choose> 103 </element> 104 </xsl:otherwise> 105 </xsl:choose> 106 </xsl:template> 107 108 109 110 <!-- Match attributes. --> 111 112 <xsl:template match="*[@template:attribute or @template:attribute-field or @template:attribute-area or @template:attribute-button or @template:attribute-list-button or @template:selector-field or @template:multiple-choice-field or @template:multiple-choice-list-field or @template:multiple-choice-value or @template:multiple-choice-list-value or @template:multiple-choice-list-element]"> 113 <xsl:call-template name="enter-attribute"/> 114 </xsl:template> 115 116 <xsl:template name="enter-attribute"> 117 <xsl:choose> 118 <xsl:when test="@template:attribute"> 119 <attribute> 120 <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> 121 </attribute> 122 </xsl:when> 123 <xsl:when test="@template:attribute-field"> 124 <attribute> 125 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 126 </attribute> 127 </xsl:when> 128 <xsl:when test="@template:attribute-area"> 129 <attribute> 130 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 131 <xsl:choose> 132 <xsl:when test="$field-attr != ''"> 133 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 134 </xsl:when> 135 <xsl:otherwise> 136 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 137 </xsl:otherwise> 138 </xsl:choose> 139 </attribute> 140 </xsl:when> 141 <xsl:when test="@template:attribute-button"> 142 <attribute> 143 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 144 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 145 </attribute> 146 </xsl:when> 147 <xsl:when test="@template:attribute-list-button"> 148 <attribute> 149 <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/> 150 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 151 </attribute> 152 </xsl:when> 153 <xsl:when test="@template:selector-field"> 154 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 155 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 156 <selector> 157 <xsl:choose> 158 <xsl:when test="$field-name != ''"> 159 <xsl:attribute name="name"><xsl:value-of select="$field-name"/></xsl:attribute> 160 <xsl:attribute name="element"><xsl:value-of select="$affected-element"/></xsl:attribute> 161 </xsl:when> 162 <xsl:otherwise> 163 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/></xsl:attribute> 164 </xsl:otherwise> 165 </xsl:choose> 166 </selector> 167 </xsl:when> 168 <xsl:when test="@template:multiple-choice-field"> 169 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 170 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 171 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 172 <xsl:variable name="field-attr-type-info" select="substring-after($field-attr-info, ',')"/> 173 <xsl:variable name="field-attr-type" select="substring-before($field-attr-type-info, ',')"/> 174 <xsl:variable name="field-source-type" select="substring-after($field-attr-type-info, ',')"/> 175 <xsl:choose> 176 <xsl:when test="$field-element != '-'"> 177 <element type="multiple-choice"> 178 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 179 <xsl:if test="$field-source-type = 'dynamic'"> 180 <xsl:attribute name="source">dynamic</xsl:attribute> 181 </xsl:if> 182 <attribute> 183 <xsl:choose> 184 <xsl:when test="$field-attr != ''"> 185 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 186 </xsl:when> 187 <xsl:otherwise> 188 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 189 </xsl:otherwise> 190 </xsl:choose> 191 </attribute> 192 <xsl:apply-templates select="*"/> 193 </element> 194 </xsl:when> 195 <xsl:otherwise> 196 <xsl:attribute name="type">multiple-choice</xsl:attribute> 197 <xsl:if test="$field-source-type = 'dynamic'"> 198 <xsl:attribute name="source">dynamic</xsl:attribute> 199 </xsl:if> 200 <attribute> 201 <xsl:choose> 202 <xsl:when test="$field-attr != ''"> 203 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 204 </xsl:when> 205 <xsl:otherwise> 206 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 207 </xsl:otherwise> 208 </xsl:choose> 209 </attribute> 210 <xsl:apply-templates select="*"/> 211 </xsl:otherwise> 212 </xsl:choose> 213 </xsl:when> 214 <xsl:when test="@template:multiple-choice-list-field"> 215 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 216 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 217 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 218 <xsl:variable name="field-list-attr-info" select="substring-after($field-list-info, ',')"/> 219 <xsl:variable name="field-list-attr" select="substring-before($field-list-attr-info, ',')"/> 220 <xsl:variable name="field-source-type" select="substring-after($field-list-attr-info, ',')"/> 221 <xsl:choose> 222 <xsl:when test="$field-element != '-'"> 223 <element type="multiple-choice-list"> 224 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 225 <xsl:if test="$field-source-type = 'dynamic'"> 226 <xsl:attribute name="source">dynamic</xsl:attribute> 227 </xsl:if> 228 <attribute> 229 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 230 </attribute> 231 <xsl:apply-templates select="*"/> 232 </element> 233 </xsl:when> 234 <xsl:otherwise> 235 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 236 <xsl:if test="$field-source-type = 'dynamic'"> 237 <xsl:attribute name="source">dynamic</xsl:attribute> 238 </xsl:if> 239 <attribute> 240 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 241 </attribute> 242 <xsl:apply-templates select="*"/> 243 </xsl:otherwise> 244 </xsl:choose> 245 </xsl:when> 246 <xsl:when test="@template:multiple-choice-list-element"> 247 <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/> 248 <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/> 249 <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/> 250 <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/> 251 <xsl:choose> 252 <xsl:when test="$element != '-'"> 253 <element type="multiple-choice-list"> 254 <xsl:attribute name="name"><xsl:value-of select="$element"/></xsl:attribute> 255 <element type="multiple-choice-list-value"> 256 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 257 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 258 <xsl:apply-templates select="*"/> 259 </element> 260 </element> 261 </xsl:when> 262 <xsl:otherwise> 263 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 264 <element type="multiple-choice-list-value"> 265 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 266 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 267 <xsl:apply-templates select="*"/> 268 </element> 269 </xsl:otherwise> 270 </xsl:choose> 271 </xsl:when> 272 <xsl:when test="@template:multiple-choice-value"> 273 <element type="multiple-choice-value"> 274 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 275 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 276 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 277 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 278 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 279 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 280 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 281 <xsl:attribute name="expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 282 <xsl:choose> 283 <xsl:when test="$field-set-attr != ''"> 284 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 285 </xsl:when> 286 <xsl:otherwise> 287 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 288 </xsl:otherwise> 289 </xsl:choose> 290 <attribute> 291 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 292 </attribute> 293 </element> 294 </xsl:when> 295 <xsl:when test="@template:multiple-choice-list-value"> 296 <element type="multiple-choice-list-value"> 297 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 298 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 299 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 300 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 301 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 302 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 303 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 304 <xsl:attribute name="expr">@value-is-set</xsl:attribute> 305 <!-- Special attribute corresponding with the expression. --> 306 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 307 <xsl:choose> 308 <xsl:when test="$field-set-attr != ''"> 309 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 310 </xsl:when> 311 <xsl:otherwise> 312 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 313 </xsl:otherwise> 314 </xsl:choose> 315 <attribute> 316 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 317 </attribute> 318 </element> 319 </xsl:when> 320 <xsl:otherwise> 321 <xsl:apply-templates select="*"/> 322 </xsl:otherwise> 323 </xsl:choose> 324 </xsl:template> 325 326 327 328 <!-- Traverse unknown elements. --> 329 330 <xsl:template match="*"> 331 <xsl:apply-templates select="*"/> 332 </xsl:template> 333 334 </xsl:stylesheet>