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