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 <element type="multiple-choice"> 112 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 113 <xsl:variable name="field-attr" select="substring-after(@template:multiple-choice-field, ',')"/> 114 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 115 <attribute> 116 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 117 </attribute> 118 <xsl:apply-templates select="*"/> 119 </element> 120 </xsl:when> 121 <xsl:when test="@template:multiple-choice-list-field"> 122 <element type="multiple-choice-list"> 123 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 124 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 125 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 126 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 127 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 128 <attribute> 129 <xsl:attribute name="name"><xsl:value-of select="$field-list-attr"/></xsl:attribute> 130 </attribute> 131 <xsl:apply-templates select="*"/> 132 </element> 133 </xsl:when> 134 <xsl:when test="@template:multiple-choice-value"> 135 <element type="multiple-choice-value"> 136 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 137 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 138 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 139 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 140 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 141 <xsl:attribute name="expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 142 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 143 <attribute> 144 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 145 </attribute> 146 </element> 147 </xsl:when> 148 <xsl:when test="@template:multiple-choice-list-value"> 149 <element type="multiple-choice-list-value"> 150 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 151 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 152 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 153 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 154 <xsl:attribute name="name"><xsl:value-of select="$field-element"/></xsl:attribute> 155 <xsl:attribute name="expr">@value-is-set</xsl:attribute> 156 <!-- Special attribute corresponding with the expression. --> 157 <xsl:attribute name="expr-name">value-is-set</xsl:attribute> 158 <xsl:attribute name="expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 159 <attribute> 160 <xsl:attribute name="name"><xsl:value-of select="$field-attr"/></xsl:attribute> 161 </attribute> 162 </element> 163 </xsl:when> 164 <xsl:otherwise> 165 <xsl:apply-templates select="*"/> 166 </xsl:otherwise> 167 </xsl:choose> 168 </xsl:template> 169 170 171 172 <!-- Traverse unknown elements. --> 173 174 <xsl:template match="*"> 175 <xsl:apply-templates select="*"/> 176 </xsl:template> 177 178 </xsl:stylesheet>