1 <?xml version="1.0"?> 2 <!-- 3 A stylesheet which expands the higher-level macro attributes, producing 4 lower-level attributes that can be translated into the final output stylesheet 5 code. 6 7 Copyright (C) 2005, 2007, 2010 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU Lesser General Public License as published by the Free 11 Software Foundation; either version 3 of the License, or (at your option) any 12 later version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 17 details. 18 19 You should have received a copy of the GNU Lesser General Public License along 20 with this program. If not, see <http://www.gnu.org/licenses/>. 21 --> 22 <xsl:stylesheet version="1.0" 23 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 24 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> 25 26 <xsl:output indent="yes"/> 27 28 <!-- Input fields. --> 29 <!-- Format: attribute --> 30 31 <xsl:template match="*[@template:attribute-field]"> 32 <xsl:copy> 33 <!-- Remove attribute-field and replace name and value. --> 34 <xsl:apply-templates select="@*[local-name() != 'attribute-field' and local-name() != 'name' and local-name() != 'value']"/> 35 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 36 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 37 <xsl:attribute name="value">{$this-value}</xsl:attribute> 38 <xsl:apply-templates select="*|node()"/> 39 </xsl:copy> 40 </xsl:template> 41 42 <!-- Text areas and other elements without value attributes. --> 43 <!-- Format: attribute[,effect] --> 44 45 <xsl:template match="*[@template:attribute-area]"> 46 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 47 <xsl:variable name="field-effect" select="substring-after(@template:attribute-area, ',')"/> 48 <xsl:copy> 49 <!-- Remove attribute-area and replace name. --> 50 <xsl:apply-templates select="@*[local-name() != 'attribute-area' and local-name() != 'name']"/> 51 <xsl:choose> 52 <xsl:when test="$field-attr != ''"> 53 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 54 <xsl:if test="$field-effect != ''"> 55 <xsl:attribute name="template:effect"><xsl:value-of select="$field-effect"/></xsl:attribute> 56 </xsl:if> 57 <xsl:attribute name="template:value">$this-value</xsl:attribute> 58 </xsl:when> 59 <xsl:otherwise> 60 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 61 <xsl:attribute name="template:value">$this-value</xsl:attribute> 62 </xsl:otherwise> 63 </xsl:choose> 64 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 65 <xsl:apply-templates select="*|node()"/> 66 </xsl:copy> 67 </xsl:template> 68 69 <!-- Buttons whose state varies according to an attribute. --> 70 <!-- Format: attribute,value,attribute-to-set --> 71 72 <xsl:template match="*[@template:attribute-button]"> 73 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 74 <xsl:variable name="field-info" select="substring-after(@template:attribute-button, ',')"/> 75 <xsl:variable name="field-value" select="substring-before($field-info, ',')"/> 76 <xsl:variable name="field-set-attr" select="substring-after($field-info, ',')"/> 77 <xsl:copy> 78 <!-- Remove attribute-button and replace name. --> 79 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 80 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 81 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 82 <xsl:attribute name="template:expr">$this-value = '<xsl:value-of select="$field-value"/>'</xsl:attribute> 83 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 84 <xsl:attribute name="value"><xsl:value-of select="$field-value"/></xsl:attribute> 85 <xsl:apply-templates select="*|node()"/> 86 </xsl:copy> 87 </xsl:template> 88 89 <!-- Buttons whose state varies according to an attribute in a list of multiple choice elements. --> 90 <!-- Format: attribute,attribute-to-set --> 91 92 <xsl:template match="*[@template:attribute-list-button]"> 93 <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/> 94 <xsl:variable name="field-set-attr" select="substring-after(@template:attribute-list-button, ',')"/> 95 <xsl:copy> 96 <!-- Remove attribute-button and replace name. --> 97 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 98 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 99 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 100 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 101 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 102 <xsl:attribute name="value">{$this-value}</xsl:attribute> 103 <xsl:apply-templates select="*|node()"/> 104 </xsl:copy> 105 </xsl:template> 106 107 <!-- Selectors. --> 108 <!-- Format: name[,affected-element] --> 109 110 <xsl:template match="*[@template:selector-field]"> 111 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 112 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 113 <xsl:copy> 114 <!-- Remove selector-field and replace name. --> 115 <xsl:apply-templates select="@*[local-name() != 'selector-field' and local-name() != 'name']"/> 116 <xsl:choose> 117 <xsl:when test="$field-name != ''"> 118 <xsl:attribute name="name"><xsl:value-of select="$field-name"/>={template:this-element()}</xsl:attribute> 119 </xsl:when> 120 <xsl:otherwise> 121 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/>={template:this-element()}</xsl:attribute> 122 </xsl:otherwise> 123 </xsl:choose> 124 <xsl:apply-templates select="*|node()"/> 125 </xsl:copy> 126 </xsl:template> 127 128 <!-- Multiple choice fields, represented by menus and listboxes. --> 129 <!-- Format: element,attribute[,attribute-type|,attribute-type,source-type] --> 130 131 <xsl:template match="*[@template:multiple-choice-field]"> 132 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 133 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 134 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 135 <xsl:variable name="field-attr-type-info" select="substring-after($field-attr-info, ',')"/> 136 <xsl:variable name="field-attr-type" select="substring-before($field-attr-type-info, ',')"/> 137 <xsl:variable name="field-source-type" select="substring-after($field-attr-type-info, ',')"/> 138 <xsl:copy> 139 <!-- Remove multiple-choice-field and replace name. --> 140 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/> 141 <xsl:if test="$field-element != '-'"> 142 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 143 </xsl:if> 144 <xsl:choose> 145 <xsl:when test="$field-attr-type = 'new' or field-attr-type = '' and $field-attr-type-info = 'new'"> 146 <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute> 147 </xsl:when> 148 <xsl:when test="$field-attr = ''"> 149 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 150 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 151 </xsl:when> 152 <xsl:otherwise> 153 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 154 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 155 </xsl:otherwise> 156 </xsl:choose> 157 <xsl:apply-templates select="*|node()"/> 158 </xsl:copy> 159 </xsl:template> 160 161 <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. --> 162 <!-- Format: element,list-element,list-attribute[,source-type] --> 163 164 <xsl:template match="*[@template:multiple-choice-list-field]"> 165 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 166 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 167 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 168 <xsl:variable name="field-list-attr-info" select="substring-after($field-list-info, ',')"/> 169 <xsl:variable name="field-list-attr" select="substring-before($field-list-attr-info, ',')"/> 170 <xsl:variable name="field-source-type" select="substring-after($field-list-attr-info, ',')"/> 171 <xsl:copy> 172 <!-- Remove multiple-choice-list-field and replace name. --> 173 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/> 174 <xsl:if test="$field-element != '-'"> 175 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 176 </xsl:if> 177 <xsl:choose> 178 <xsl:when test="$field-list-attr != ''"> 179 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 180 '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute> 181 </xsl:when> 182 <xsl:otherwise> 183 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 184 '<xsl:value-of select="$field-list-attr-info"/>')}</xsl:attribute> 185 </xsl:otherwise> 186 </xsl:choose> 187 <xsl:apply-templates select="*|node()"/> 188 </xsl:copy> 189 </xsl:template> 190 191 <!-- Multiple choice values, represented by option elements. --> 192 <!-- Format: element,attribute,attribute-to-set[,contents-expr] --> 193 194 <xsl:template match="*[@template:multiple-choice-value]"> 195 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 196 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 197 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 198 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 199 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 200 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 201 <xsl:copy> 202 <!-- Remove multiple-choice-value and replace value. --> 203 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/> 204 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 205 <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 206 <!-- For the option text... --> 207 <xsl:choose> 208 <!-- Either provide the stated attribute as the eventual text of an option element. --> 209 <xsl:when test="$field-set-attr = ''"> 210 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 211 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 212 </xsl:when> 213 <!-- Or get the specific contents. --> 214 <xsl:otherwise> 215 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 216 <xsl:attribute name="template:value"><xsl:value-of select="$field-contents"/></xsl:attribute> 217 </xsl:otherwise> 218 </xsl:choose> 219 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 220 <xsl:apply-templates select="*|node()"/> 221 </xsl:copy> 222 </xsl:template> 223 224 <!-- Multiple choice list values, represented by option elements. --> 225 <!-- Format: list-element,list-attribute,attribute-to-set[,contents-expr] --> 226 227 <xsl:template match="*[@template:multiple-choice-list-value]"> 228 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 229 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 230 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 231 <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/> 232 <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/> 233 <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/> 234 <xsl:copy> 235 <!-- Remove multiple-choice-value and replace value. --> 236 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/> 237 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 238 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 239 <!-- For the option text... --> 240 <xsl:choose> 241 <!-- Either provide the stated attribute as the eventual text of an option element. --> 242 <xsl:when test="$field-set-attr = ''"> 243 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute> 244 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 245 </xsl:when> 246 <!-- Or get the specific contents. --> 247 <xsl:otherwise> 248 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 249 <xsl:attribute name="template:value"><xsl:value-of select="$field-contents"/></xsl:attribute> 250 </xsl:otherwise> 251 </xsl:choose> 252 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 253 <xsl:apply-templates select="*|node()"/> 254 </xsl:copy> 255 </xsl:template> 256 257 <!-- Multiple choice elements. --> 258 <!-- Format: element,list-element,list-attribute --> 259 260 <xsl:template match="*[@template:multiple-choice-list-element]"> 261 <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/> 262 <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/> 263 <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/> 264 <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/> 265 <xsl:copy> 266 <!-- Remove multiple-choice-list-element. --> 267 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value']"/> 268 <xsl:attribute name="template:element"><xsl:value-of select="$element"/>,<xsl:value-of select="$element-list-element"/></xsl:attribute> 269 <!-- The attribute is ignored - it is only useful in the schema and input processes. --> 270 <xsl:apply-templates select="*|node()"/> 271 </xsl:copy> 272 </xsl:template> 273 274 <!-- Replicate unknown elements. --> 275 276 <xsl:template match="@*|node()"> 277 <xsl:copy> 278 <xsl:apply-templates select="@*|node()"/> 279 </xsl:copy> 280 </xsl:template> 281 282 </xsl:stylesheet>