1 <?xml version="1.0"?> 2 <!-- 3 Copyright (C) 2005, 2006, 2007, 2008 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="@template:output"> 55 <!-- Output elements are omitted from the schema and are only used to produce output. --> 56 </xsl:when> 57 <xsl:when test="$first-element = ''"> 58 <xsl:call-template name="next-element"> 59 <xsl:with-param name="first-element" select="$other-elements"/> 60 <xsl:with-param name="first-init" select="$other-init"/> 61 </xsl:call-template> 62 </xsl:when> 63 <xsl:otherwise> 64 <xsl:call-template name="next-element"> 65 <xsl:with-param name="first-element" select="$first-element"/> 66 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 67 <xsl:with-param name="first-init" select="$first-init"/> 68 <xsl:with-param name="remaining-init" select="$remaining-init"/> 69 </xsl:call-template> 70 </xsl:otherwise> 71 </xsl:choose> 72 </xsl:template> 73 74 <xsl:template name="next-element"> 75 <xsl:param name="first-element"/> 76 <xsl:param name="remaining-elements"/> 77 <xsl:param name="first-init"/> 78 <xsl:param name="remaining-init"/> 79 <!-- Test for recursive references. --> 80 <xsl:variable name="recursive-element" select="ancestor::*[$first-element = str:split(@template:element, ',')[1]]"/> 81 <xsl:choose> 82 <!-- Generate a reference to the previous element definition. --> 83 <xsl:when test="$recursive-element"> 84 <element-ref> 85 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 86 </element-ref> 87 </xsl:when> 88 <!-- Generate a normal, nested element definition. --> 89 <xsl:otherwise> 90 <element> 91 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 92 <xsl:if test="$first-init != ''"> 93 <xsl:attribute name="init"><xsl:value-of select="$first-init"/></xsl:attribute> 94 </xsl:if> 95 <xsl:choose> 96 <xsl:when test="$remaining-elements = ''"> 97 <xsl:call-template name="enter-attribute"/> 98 </xsl:when> 99 <xsl:otherwise> 100 <xsl:call-template name="enter-element"> 101 <xsl:with-param name="other-elements" select="$remaining-elements"/> 102 <xsl:with-param name="other-init" select="$remaining-init"/> 103 </xsl:call-template> 104 </xsl:otherwise> 105 </xsl:choose> 106 </element> 107 </xsl:otherwise> 108 </xsl:choose> 109 </xsl:template> 110 111 112 113 <!-- Match attributes. --> 114 115 <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]"> 116 <xsl:call-template name="enter-attribute"/> 117 </xsl:template> 118 119 <xsl:template name="enter-attribute"> 120 <xsl:choose> 121 <xsl:when test="@template:attribute"> 122 <attribute> 123 <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> 124 </attribute> 125 </xsl:when> 126 <xsl:when test="@template:attribute-field"> 127 <attribute> 128 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 129 </attribute> 130 </xsl:when> 131 <xsl:when test="@template:attribute-area"> 132 <attribute> 133 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 134 <xsl:choose> 135 <xsl:when test="$field-attr != ''"> 136 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 137 </xsl:when> 138 <xsl:otherwise> 139 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 140 </xsl:otherwise> 141 </xsl:choose> 142 </attribute> 143 </xsl:when> 144 <xsl:when test="@template:attribute-button"> 145 <attribute> 146 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 147 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 148 </attribute> 149 </xsl:when> 150 <xsl:when test="@template:attribute-list-button"> 151 <attribute> 152 <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/> 153 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 154 </attribute> 155 </xsl:when> 156 <xsl:when test="@template:selector-field"> 157 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 158 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 159 <selector> 160 <xsl:choose> 161 <xsl:when test="$field-name != ''"> 162 <xsl:attribute name="name"><xsl:value-of select="$field-name"/></xsl:attribute> 163 <xsl:attribute name="element"><xsl:value-of select="$affected-element"/></xsl:attribute> 164 </xsl:when> 165 <xsl:otherwise> 166 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/></xsl:attribute> 167 </xsl:otherwise> 168 </xsl:choose> 169 </selector> 170 </xsl:when> 171 <xsl:when test="@template:multiple-choice-field"> 172 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 173 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 174 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 175 <xsl:variable name="field-attr-type-info" select="substring-after($field-attr-info, ',')"/> 176 <xsl:variable name="field-attr-type" select="substring-before($field-attr-type-info, ',')"/> 177 <xsl:variable name="field-source-type" select="substring-after($field-attr-type-info, ',')"/> 178 <xsl:choose> 179 <xsl:when test="$field-element != '-'"> 180 <element type="multiple-choice"> 181 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 182 <xsl:if test="$field-source-type = 'dynamic'"> 183 <xsl:attribute name="source">dynamic</xsl:attribute> 184 </xsl:if> 185 <attribute> 186 <xsl:choose> 187 <xsl:when test="$field-attr != ''"> 188 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 189 </xsl:when> 190 <xsl:otherwise> 191 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 192 </xsl:otherwise> 193 </xsl:choose> 194 </attribute> 195 <xsl:apply-templates select="*"/> 196 </element> 197 </xsl:when> 198 <xsl:otherwise> 199 <xsl:attribute name="type">multiple-choice</xsl:attribute> 200 <xsl:if test="$field-source-type = 'dynamic'"> 201 <xsl:attribute name="source">dynamic</xsl:attribute> 202 </xsl:if> 203 <attribute> 204 <xsl:choose> 205 <xsl:when test="$field-attr != ''"> 206 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 207 </xsl:when> 208 <xsl:otherwise> 209 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 210 </xsl:otherwise> 211 </xsl:choose> 212 </attribute> 213 <xsl:apply-templates select="*"/> 214 </xsl:otherwise> 215 </xsl:choose> 216 </xsl:when> 217 <xsl:when test="@template:multiple-choice-list-field"> 218 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 219 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 220 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 221 <xsl:variable name="field-list-attr-info" select="substring-after($field-list-info, ',')"/> 222 <xsl:variable name="field-list-attr" select="substring-before($field-list-attr-info, ',')"/> 223 <xsl:variable name="field-source-type" select="substring-after($field-list-attr-info, ',')"/> 224 <xsl:choose> 225 <xsl:when test="$field-element != '-'"> 226 <element type="multiple-choice-list"> 227 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 228 <xsl:if test="$field-source-type = 'dynamic'"> 229 <xsl:attribute name="source">dynamic</xsl:attribute> 230 </xsl:if> 231 <attribute> 232 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 233 </attribute> 234 <xsl:apply-templates select="*"/> 235 </element> 236 </xsl:when> 237 <xsl:otherwise> 238 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 239 <xsl:if test="$field-source-type = 'dynamic'"> 240 <xsl:attribute name="source">dynamic</xsl:attribute> 241 </xsl:if> 242 <attribute> 243 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 244 </attribute> 245 <xsl:apply-templates select="*"/> 246 </xsl:otherwise> 247 </xsl:choose> 248 </xsl:when> 249 <xsl:when test="@template:multiple-choice-list-element"> 250 <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/> 251 <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/> 252 <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/> 253 <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/> 254 <xsl:choose> 255 <xsl:when test="$element != '-'"> 256 <element type="multiple-choice-list"> 257 <xsl:attribute name="name"><xsl:value-of select="$element"/></xsl:attribute> 258 <element type="multiple-choice-list-value"> 259 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 260 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 261 <xsl:apply-templates select="*"/> 262 </element> 263 </element> 264 </xsl:when> 265 <xsl:otherwise> 266 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 267 <element type="multiple-choice-list-value"> 268 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 269 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 270 <xsl:apply-templates select="*"/> 271 </element> 272 </xsl:otherwise> 273 </xsl:choose> 274 </xsl:when> 275 <xsl:when test="@template:multiple-choice-value"> 276 <element type="multiple-choice-value"> 277 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 278 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 279 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 280 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 281 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 282 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 283 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 284 <xsl:attribute name="expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 285 <xsl:choose> 286 <xsl:when test="$field-set-attr != ''"> 287 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 288 </xsl:when> 289 <xsl:otherwise> 290 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 291 </xsl:otherwise> 292 </xsl:choose> 293 <attribute> 294 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 295 </attribute> 296 </element> 297 </xsl:when> 298 <xsl:when test="@template:multiple-choice-list-value"> 299 <element type="multiple-choice-list-value"> 300 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 301 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 302 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 303 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 304 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 305 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 306 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 307 <xsl:attribute name="expr">@value-is-set</xsl:attribute> 308 <!-- Special attribute corresponding with the expression. --> 309 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 310 <xsl:choose> 311 <xsl:when test="$field-set-attr != ''"> 312 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 313 </xsl:when> 314 <xsl:otherwise> 315 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 316 </xsl:otherwise> 317 </xsl:choose> 318 <attribute> 319 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 320 </attribute> 321 </element> 322 </xsl:when> 323 <xsl:otherwise> 324 <xsl:apply-templates select="*"/> 325 </xsl:otherwise> 326 </xsl:choose> 327 </xsl:template> 328 329 330 331 <!-- Traverse unknown elements. --> 332 333 <xsl:template match="*"> 334 <xsl:apply-templates select="*"/> 335 </xsl:template> 336 337 </xsl:stylesheet>