1 <?xml version="1.0"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> 5 6 <xsl:output indent="yes"/> 7 8 9 10 <!-- Match the document itself. --> 11 12 <xsl:template match="/"> 13 <!-- Process the elements. --> 14 <xsl:apply-templates select="*"/> 15 </xsl:template> 16 17 18 19 <!-- Match element references. --> 20 21 <xsl:template match="*[@template:element]"> 22 <xsl:call-template name="enter-element"> 23 <xsl:with-param name="other-elements" select="@template:element"/> 24 </xsl:call-template> 25 </xsl:template> 26 27 <xsl:template name="enter-element"> 28 <xsl:param name="other-elements"/> 29 <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> 30 <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> 31 <xsl:choose> 32 <xsl:when test="$first-element = ''"> 33 <xsl:call-template name="next-element"> 34 <xsl:with-param name="first-element" select="$other-elements"/> 35 </xsl:call-template> 36 </xsl:when> 37 <xsl:otherwise> 38 <xsl:call-template name="next-element"> 39 <xsl:with-param name="first-element" select="$first-element"/> 40 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 41 </xsl:call-template> 42 </xsl:otherwise> 43 </xsl:choose> 44 </xsl:template> 45 46 <xsl:template name="next-element"> 47 <xsl:param name="first-element"/> 48 <xsl:param name="remaining-elements"/> 49 <element> 50 <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> 51 <xsl:choose> 52 <xsl:when test="$remaining-elements = ''"> 53 <xsl:call-template name="enter-attribute"/> 54 </xsl:when> 55 <xsl:otherwise> 56 <xsl:call-template name="enter-element"> 57 <xsl:with-param name="other-elements" select="$remaining-elements"/> 58 </xsl:call-template> 59 </xsl:otherwise> 60 </xsl:choose> 61 </element> 62 </xsl:template> 63 64 65 66 <!-- Match attributes. --> 67 68 <xsl:template match="*[not(@template:element) and (@template:attribute or @template:attribute-field or @template:attribute-area or @template:attribute-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)]"> 69 <xsl:call-template name="enter-attribute"/> 70 </xsl:template> 71 72 <xsl:template name="enter-attribute"> 73 <xsl:choose> 74 <xsl:when test="@template:attribute"> 75 <attribute> 76 <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> 77 </attribute> 78 </xsl:when> 79 <xsl:when test="@template:attribute-field"> 80 <attribute> 81 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 82 </attribute> 83 </xsl:when> 84 <xsl:when test="@template:attribute-area"> 85 <attribute> 86 <xsl:attribute name="name"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 87 </attribute> 88 </xsl:when> 89 <xsl:when test="@template:attribute-button"> 90 <attribute> 91 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 92 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 93 </attribute> 94 </xsl:when> 95 <xsl:when test="@template:selector-field"> 96 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 97 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 98 <selector> 99 <xsl:choose> 100 <xsl:when test="$field-name != ''"> 101 <xsl:attribute name="name"><xsl:value-of select="$field-name"/></xsl:attribute> 102 <xsl:attribute name="element"><xsl:value-of select="$affected-element"/></xsl:attribute> 103 </xsl:when> 104 <xsl:otherwise> 105 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/></xsl:attribute> 106 </xsl:otherwise> 107 </xsl:choose> 108 </selector> 109 </xsl:when> 110 <xsl:when test="@template:multiple-choice-field"> 111 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 112 <xsl:variable name="field-attr" select="substring-after(@template:multiple-choice-field, ',')"/> 113 <xsl:choose> 114 <xsl:when test="$field-element != '-'"> 115 <element type="multiple-choice"> 116 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 117 <attribute> 118 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 119 </attribute> 120 <xsl:apply-templates select="*"/> 121 </element> 122 </xsl:when> 123 <xsl:otherwise> 124 <xsl:attribute name="type">multiple-choice</xsl:attribute> 125 <attribute> 126 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 127 </attribute> 128 <xsl:apply-templates select="*"/> 129 </xsl:otherwise> 130 </xsl:choose> 131 </xsl:when> 132 <xsl:when test="@template:multiple-choice-list-field"> 133 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 134 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 135 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 136 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 137 <xsl:choose> 138 <xsl:when test="$field-element != '-'"> 139 <element type="multiple-choice-list"> 140 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 141 <attribute> 142 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 143 </attribute> 144 <xsl:apply-templates select="*"/> 145 </element> 146 </xsl:when> 147 <xsl:otherwise> 148 <xsl:attribute name="type">multiple-choice-list</xsl:attribute> 149 <attribute> 150 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 151 </attribute> 152 <xsl:apply-templates select="*"/> 153 </xsl:otherwise> 154 </xsl:choose> 155 </xsl:when> 156 <xsl:when test="@template:multiple-choice-value"> 157 <element type="multiple-choice-value"> 158 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 159 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 160 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 161 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 162 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 163 <xsl:attribute name="expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 164 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 165 <attribute> 166 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 167 </attribute> 168 </element> 169 </xsl:when> 170 <xsl:when test="@template:multiple-choice-list-value"> 171 <element type="multiple-choice-list-value"> 172 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 173 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-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:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 177 <xsl:attribute name="expr">@value-is-set</xsl:attribute> 178 <!-- Special attribute corresponding with the expression. --> 179 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 180 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 181 <attribute> 182 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 183 </attribute> 184 </element> 185 </xsl:when> 186 <xsl:otherwise> 187 <xsl:apply-templates select="*"/> 188 </xsl:otherwise> 189 </xsl:choose> 190 </xsl:template> 191 192 193 194 <!-- Traverse unknown elements. --> 195 196 <xsl:template match="*"> 197 <xsl:apply-templates select="*"/> 198 </xsl:template> 199 200 </xsl:stylesheet>