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