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 23 <xsl:output indent="yes"/> 24 25 26 27 <!-- Match the document itself. --> 28 29 <xsl:template match="/"> 30 <!-- Process the elements. --> 31 <xsl:apply-templates select="*"/> 32 </xsl:template> 33 34 35 36 <!-- Match element references. --> 37 38 <xsl:template match="*[@template:element]"> 39 <xsl:call-template name="enter-element"> 40 <xsl:with-param name="other-elements" select="@template:element"/> 41 <xsl:with-param name="other-init" select="@template:init"/> 42 </xsl:call-template> 43 </xsl:template> 44 45 <xsl:template name="enter-element"> 46 <xsl:param name="other-elements"/> 47 <xsl:param name="other-init"/> 48 <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> 49 <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> 50 <xsl:variable name="first-init" select="substring-before($other-init, ',')"/> 51 <xsl:variable name="remaining-init" select="substring-after($other-init, ',')"/> 52 <xsl:choose> 53 <xsl:when test="$first-element = ''"> 54 <xsl:call-template name="next-element"> 55 <xsl:with-param name="first-element" select="$other-elements"/> 56 <xsl:with-param name="first-init" select="$other-init"/> 57 </xsl:call-template> 58 </xsl:when> 59 <xsl:otherwise> 60 <xsl:call-template name="next-element"> 61 <xsl:with-param name="first-element" select="$first-element"/> 62 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 63 <xsl:with-param name="first-init" select="$first-init"/> 64 <xsl:with-param name="remaining-init" select="$remaining-init"/> 65 </xsl:call-template> 66 </xsl:otherwise> 67 </xsl:choose> 68 </xsl:template> 69 70 <xsl:template name="next-element"> 71 <xsl:param name="first-element"/> 72 <xsl:param name="remaining-elements"/> 73 <xsl:param name="first-init"/> 74 <xsl:param name="remaining-init"/> 75 <element> 76 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 77 <xsl:if test="$first-init != ''"> 78 <xsl:attribute name="init"><xsl:value-of select="$first-init"/></xsl:attribute> 79 </xsl:if> 80 <xsl:choose> 81 <xsl:when test="$remaining-elements = ''"> 82 <xsl:call-template name="enter-attribute"/> 83 </xsl:when> 84 <xsl:otherwise> 85 <xsl:call-template name="enter-element"> 86 <xsl:with-param name="other-elements" select="$remaining-elements"/> 87 <xsl:with-param name="other-init" select="$remaining-init"/> 88 </xsl:call-template> 89 </xsl:otherwise> 90 </xsl:choose> 91 </element> 92 </xsl:template> 93 94 95 96 <!-- Match attributes. --> 97 98 <xsl:template match="*[not(@template:element) and (@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]"> 99 <xsl:call-template name="enter-attribute"/> 100 </xsl:template> 101 102 <xsl:template name="enter-attribute"> 103 <xsl:choose> 104 <xsl:when test="@template:attribute"> 105 <attribute> 106 <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> 107 </attribute> 108 </xsl:when> 109 <xsl:when test="@template:attribute-field"> 110 <attribute> 111 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 112 </attribute> 113 </xsl:when> 114 <xsl:when test="@template:attribute-area"> 115 <attribute> 116 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 117 <xsl:choose> 118 <xsl:when test="$field-attr != ''"> 119 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 120 </xsl:when> 121 <xsl:otherwise> 122 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 123 </xsl:otherwise> 124 </xsl:choose> 125 </attribute> 126 </xsl:when> 127 <xsl:when test="@template:attribute-button"> 128 <attribute> 129 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 130 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 131 </attribute> 132 </xsl:when> 133 <xsl:when test="@template:attribute-list-button"> 134 <attribute> 135 <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/> 136 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 137 </attribute> 138 </xsl:when> 139 <xsl:when test="@template:selector-field"> 140 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 141 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 142 <selector> 143 <xsl:choose> 144 <xsl:when test="$field-name != ''"> 145 <xsl:attribute name="name"><xsl:value-of select="$field-name"/></xsl:attribute> 146 <xsl:attribute name="element"><xsl:value-of select="$affected-element"/></xsl:attribute> 147 </xsl:when> 148 <xsl:otherwise> 149 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/></xsl:attribute> 150 </xsl:otherwise> 151 </xsl:choose> 152 </selector> 153 </xsl:when> 154 <xsl:when test="@template:multiple-choice-field"> 155 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 156 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 157 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 158 <xsl:variable name="field-attr-type" select="substring-after($field-attr-info, ',')"/> 159 <xsl:choose> 160 <xsl:when test="$field-element != '-'"> 161 <element type="multiple-choice"> 162 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 163 <attribute> 164 <xsl:choose> 165 <xsl:when test="$field-attr != ''"> 166 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 167 </xsl:when> 168 <xsl:otherwise> 169 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 170 </xsl:otherwise> 171 </xsl:choose> 172 </attribute> 173 <xsl:apply-templates select="*"/> 174 </element> 175 </xsl:when> 176 <xsl:otherwise> 177 <xsl:attribute name="type">multiple-choice</xsl:attribute> 178 <attribute> 179 <xsl:choose> 180 <xsl:when test="$field-attr != ''"> 181 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 182 </xsl:when> 183 <xsl:otherwise> 184 <xsl:attribute name="name"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 185 </xsl:otherwise> 186 </xsl:choose> 187 </attribute> 188 <xsl:apply-templates select="*"/> 189 </xsl:otherwise> 190 </xsl:choose> 191 </xsl:when> 192 <xsl:when test="@template:multiple-choice-list-field"> 193 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 194 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 195 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 196 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 197 <xsl:choose> 198 <xsl:when test="$field-element != '-'"> 199 <element type="multiple-choice-list"> 200 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 201 <attribute> 202 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 203 </attribute> 204 <xsl:apply-templates select="*"/> 205 </element> 206 </xsl:when> 207 <xsl:otherwise> 208 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 209 <attribute> 210 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 211 </attribute> 212 <xsl:apply-templates select="*"/> 213 </xsl:otherwise> 214 </xsl:choose> 215 </xsl:when> 216 <xsl:when test="@template:multiple-choice-list-element"> 217 <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/> 218 <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/> 219 <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/> 220 <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/> 221 <xsl:choose> 222 <xsl:when test="$element != '-'"> 223 <element type="multiple-choice-list"> 224 <xsl:attribute name="name"><xsl:value-of select="$element"/></xsl:attribute> 225 <element type="multiple-choice-list-value"> 226 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 227 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 228 <xsl:apply-templates select="*"/> 229 </element> 230 </element> 231 </xsl:when> 232 <xsl:otherwise> 233 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 234 <element type="multiple-choice-list-value"> 235 <xsl:attribute name="name"><xsl:value-of select="$element-list-element"/></xsl:attribute> 236 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 237 <xsl:apply-templates select="*"/> 238 </element> 239 </xsl:otherwise> 240 </xsl:choose> 241 </xsl:when> 242 <xsl:when test="@template:multiple-choice-value"> 243 <element type="multiple-choice-value"> 244 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 245 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 246 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 247 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 248 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 249 <xsl:attribute name="expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 250 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 251 <attribute> 252 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 253 </attribute> 254 </element> 255 </xsl:when> 256 <xsl:when test="@template:multiple-choice-list-value"> 257 <element type="multiple-choice-list-value"> 258 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 259 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 260 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 261 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 262 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 263 <xsl:attribute name="expr">@value-is-set</xsl:attribute> 264 <!-- Special attribute corresponding with the expression. --> 265 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 266 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 267 <attribute> 268 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 269 </attribute> 270 </element> 271 </xsl:when> 272 <xsl:otherwise> 273 <xsl:apply-templates select="*"/> 274 </xsl:otherwise> 275 </xsl:choose> 276 </xsl:template> 277 278 279 280 <!-- Traverse unknown elements. --> 281 282 <xsl:template match="*"> 283 <xsl:apply-templates select="*"/> 284 </xsl:template> 285 286 </xsl:stylesheet>