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 <!-- Input fields. --> 26 27 <xsl:template match="*[@template:attribute-field]"> 28 <xsl:copy> 29 <!-- Remove attribute-field and replace name and value. --> 30 <xsl:apply-templates select="@*[local-name() != 'attribute-field' and local-name() != 'name' and local-name() != 'value']"/> 31 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 32 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 33 <xsl:attribute name="value">{$this-value}</xsl:attribute> 34 <xsl:apply-templates select="*|node()"/> 35 </xsl:copy> 36 </xsl:template> 37 38 <!-- Text areas and other elements without value attributes. --> 39 40 <xsl:template match="*[@template:attribute-area]"> 41 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 42 <xsl:variable name="field-effect" select="substring-after(@template:attribute-area, ',')"/> 43 <xsl:copy> 44 <!-- Remove attribute-area and replace name. --> 45 <xsl:apply-templates select="@*[local-name() != 'attribute-area' and local-name() != 'name']"/> 46 <xsl:choose> 47 <xsl:when test="$field-attr != ''"> 48 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 49 <xsl:if test="$field-effect != ''"> 50 <xsl:attribute name="template:effect"><xsl:value-of select="$field-effect"/></xsl:attribute> 51 </xsl:if> 52 <xsl:attribute name="template:value">$this-value</xsl:attribute> 53 </xsl:when> 54 <xsl:otherwise> 55 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 56 <xsl:attribute name="template:value">$this-value</xsl:attribute> 57 </xsl:otherwise> 58 </xsl:choose> 59 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 60 <xsl:apply-templates select="*|node()"/> 61 </xsl:copy> 62 </xsl:template> 63 64 <!-- Buttons whose state varies according to an attribute. --> 65 66 <xsl:template match="*[@template:attribute-button]"> 67 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 68 <xsl:variable name="field-info" select="substring-after(@template:attribute-button, ',')"/> 69 <xsl:variable name="field-value" select="substring-before($field-info, ',')"/> 70 <xsl:variable name="field-set-attr" select="substring-after($field-info, ',')"/> 71 <xsl:copy> 72 <!-- Remove attribute-button and replace name. --> 73 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 74 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 75 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 76 <xsl:attribute name="template:expr">$this-value = '<xsl:value-of select="$field-value"/>'</xsl:attribute> 77 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 78 <xsl:attribute name="value"><xsl:value-of select="$field-value"/></xsl:attribute> 79 <xsl:apply-templates select="*|node()"/> 80 </xsl:copy> 81 </xsl:template> 82 83 <!-- Buttons whose state varies according to an attribute in a list of multiple choice elements. --> 84 85 <xsl:template match="*[@template:attribute-list-button]"> 86 <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/> 87 <xsl:variable name="field-set-attr" select="substring-after(@template:attribute-list-button, ',')"/> 88 <xsl:copy> 89 <!-- Remove attribute-button and replace name. --> 90 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 91 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 92 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 93 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 94 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 95 <xsl:attribute name="value">{$this-value}</xsl:attribute> 96 <xsl:apply-templates select="*|node()"/> 97 </xsl:copy> 98 </xsl:template> 99 100 <!-- Selectors. --> 101 102 <xsl:template match="*[@template:selector-field]"> 103 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 104 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 105 <xsl:copy> 106 <!-- Remove selector-field and replace name. --> 107 <xsl:apply-templates select="@*[local-name() != 'selector-field' and local-name() != 'name']"/> 108 <xsl:choose> 109 <xsl:when test="$field-name != ''"> 110 <xsl:attribute name="name"><xsl:value-of select="$field-name"/>={template:this-element()}</xsl:attribute> 111 </xsl:when> 112 <xsl:otherwise> 113 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/>={template:this-element()}</xsl:attribute> 114 </xsl:otherwise> 115 </xsl:choose> 116 <xsl:apply-templates select="*|node()"/> 117 </xsl:copy> 118 </xsl:template> 119 120 <!-- Multiple choice fields, represented by menus and listboxes. --> 121 122 <xsl:template match="*[@template:multiple-choice-field]"> 123 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 124 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 125 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 126 <xsl:variable name="field-attr-type" select="substring-after($field-attr-info, ',')"/> 127 <xsl:copy> 128 <!-- Remove multiple-choice-field and replace name. --> 129 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/> 130 <xsl:if test="$field-element != '-'"> 131 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 132 </xsl:if> 133 <xsl:choose> 134 <xsl:when test="$field-attr-type = 'new'"> 135 <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute> 136 </xsl:when> 137 <xsl:when test="$field-attr = ''"> 138 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 139 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 140 </xsl:when> 141 <xsl:otherwise> 142 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 143 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 144 </xsl:otherwise> 145 </xsl:choose> 146 <xsl:apply-templates select="*|node()"/> 147 </xsl:copy> 148 </xsl:template> 149 150 <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. --> 151 152 <xsl:template match="*[@template:multiple-choice-list-field]"> 153 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 154 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 155 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 156 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 157 <xsl:copy> 158 <!-- Remove multiple-choice-list-field and replace name. --> 159 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/> 160 <xsl:if test="$field-element != '-'"> 161 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 162 </xsl:if> 163 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 164 '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute> 165 <xsl:apply-templates select="*|node()"/> 166 </xsl:copy> 167 </xsl:template> 168 169 <!-- Multiple choice values, represented by option elements. --> 170 171 <xsl:template match="*[@template:multiple-choice-value]"> 172 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 173 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 174 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 175 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 176 <xsl:copy> 177 <!-- Remove multiple-choice-value and replace value. --> 178 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/> 179 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 180 <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 181 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 182 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 183 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 184 <xsl:apply-templates select="*|node()"/> 185 </xsl:copy> 186 </xsl:template> 187 188 <!-- Multiple choice list values, represented by option elements. --> 189 190 <xsl:template match="*[@template:multiple-choice-list-value]"> 191 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 192 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 193 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 194 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 195 <xsl:copy> 196 <!-- Remove multiple-choice-value and replace value. --> 197 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/> 198 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 199 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 200 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 201 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 202 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 203 <xsl:apply-templates select="*|node()"/> 204 </xsl:copy> 205 </xsl:template> 206 207 <!-- Multiple choice elements. --> 208 209 <xsl:template match="*[@template:multiple-choice-list-element]"> 210 <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/> 211 <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/> 212 <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/> 213 <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/> 214 <xsl:copy> 215 <!-- Remove multiple-choice-list-element. --> 216 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value']"/> 217 <xsl:attribute name="template:element"><xsl:value-of select="$element"/>,<xsl:value-of select="$element-list-element"/></xsl:attribute> 218 <!-- The attribute is ignored - it is only useful in the schema and input processes. --> 219 <xsl:apply-templates select="*|node()"/> 220 </xsl:copy> 221 </xsl:template> 222 223 <!-- Replicate unknown elements. --> 224 225 <xsl:template match="@*|node()"> 226 <xsl:copy> 227 <xsl:apply-templates select="@*|node()"/> 228 </xsl:copy> 229 </xsl:template> 230 231 </xsl:stylesheet>