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