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 <!-- Input fields. --> 9 10 <xsl:template match="*[@template:attribute-field]"> 11 <xsl:copy> 12 <!-- Remove attribute-field and replace name and value. --> 13 <xsl:apply-templates select="@*[local-name() != 'attribute-field' and local-name() != 'name' and local-name() != 'value']"/> 14 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-field"/></xsl:attribute> 15 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 16 <xsl:attribute name="value">{$this-value}</xsl:attribute> 17 <xsl:apply-templates select="*|node()"/> 18 </xsl:copy> 19 </xsl:template> 20 21 <!-- Text areas and other elements without value attributes. --> 22 23 <xsl:template match="*[@template:attribute-area]"> 24 <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/> 25 <xsl:variable name="field-effect" select="substring-after(@template:attribute-area, ',')"/> 26 <xsl:copy> 27 <!-- Remove attribute-area and replace name. --> 28 <xsl:apply-templates select="@*[local-name() != 'attribute-area' and local-name() != 'name']"/> 29 <xsl:choose> 30 <xsl:when test="$field-attr != ''"> 31 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 32 <xsl:if test="$field-effect != ''"> 33 <xsl:attribute name="template:effect"><xsl:value-of select="$field-effect"/></xsl:attribute> 34 <xsl:attribute name="template:value">$this-value</xsl:attribute> 35 </xsl:if> 36 </xsl:when> 37 <xsl:otherwise> 38 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 39 </xsl:otherwise> 40 </xsl:choose> 41 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 42 <xsl:apply-templates select="*|node()"/> 43 </xsl:copy> 44 </xsl:template> 45 46 <!-- Buttons whose state varies according to an attribute. --> 47 48 <xsl:template match="*[@template:attribute-button]"> 49 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 50 <xsl:variable name="field-info" select="substring-after(@template:attribute-button, ',')"/> 51 <xsl:variable name="field-value" select="substring-before($field-info, ',')"/> 52 <xsl:variable name="field-set-attr" select="substring-after($field-info, ',')"/> 53 <xsl:copy> 54 <!-- Remove attribute-button and replace name. --> 55 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 56 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 57 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 58 <xsl:attribute name="template:expr">$this-value = '<xsl:value-of select="$field-value"/>'</xsl:attribute> 59 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 60 <xsl:attribute name="value"><xsl:value-of select="$field-value"/></xsl:attribute> 61 <xsl:apply-templates select="*|node()"/> 62 </xsl:copy> 63 </xsl:template> 64 65 <!-- Selectors. --> 66 67 <xsl:template match="*[@template:selector-field]"> 68 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 69 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 70 <xsl:copy> 71 <!-- Remove selector-field and replace name. --> 72 <xsl:apply-templates select="@*[local-name() != 'selector-field' and local-name() != 'name']"/> 73 <xsl:choose> 74 <xsl:when test="$field-name != ''"> 75 <xsl:attribute name="name"><xsl:value-of select="$field-name"/>={template:this-element()}</xsl:attribute> 76 </xsl:when> 77 <xsl:otherwise> 78 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/>={template:this-element()}</xsl:attribute> 79 </xsl:otherwise> 80 </xsl:choose> 81 <xsl:apply-templates select="*|node()"/> 82 </xsl:copy> 83 </xsl:template> 84 85 <!-- Multiple choice fields, represented by menus and listboxes. --> 86 87 <xsl:template match="*[@template:multiple-choice-field]"> 88 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 89 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/> 90 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 91 <xsl:variable name="field-attr-type" select="substring-after($field-attr-info, ',')"/> 92 <xsl:copy> 93 <!-- Remove multiple-choice-field and replace name. --> 94 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/> 95 <xsl:if test="$field-element != '-'"> 96 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 97 </xsl:if> 98 <xsl:choose> 99 <xsl:when test="$field-attr-type = 'new'"> 100 <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute> 101 </xsl:when> 102 <xsl:when test="$field-attr = ''"> 103 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr-info"/></xsl:attribute> 104 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 105 </xsl:when> 106 <xsl:otherwise> 107 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 108 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 109 </xsl:otherwise> 110 </xsl:choose> 111 <xsl:apply-templates select="*|node()"/> 112 </xsl:copy> 113 </xsl:template> 114 115 <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. --> 116 117 <xsl:template match="*[@template:multiple-choice-list-field]"> 118 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 119 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 120 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 121 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 122 <xsl:copy> 123 <!-- Remove multiple-choice-list-field and replace name. --> 124 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/> 125 <xsl:if test="$field-element != '-'"> 126 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 127 </xsl:if> 128 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 129 '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute> 130 <xsl:apply-templates select="*|node()"/> 131 </xsl:copy> 132 </xsl:template> 133 134 <!-- Multiple choice values, represented by option elements. --> 135 136 <xsl:template match="*[@template:multiple-choice-value]"> 137 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 138 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 139 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 140 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 141 <xsl:copy> 142 <!-- Remove multiple-choice-value and replace value. --> 143 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/> 144 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 145 <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 146 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 147 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 148 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 149 <xsl:apply-templates select="*|node()"/> 150 </xsl:copy> 151 </xsl:template> 152 153 <!-- Multiple choice list values, represented by option elements. --> 154 155 <xsl:template match="*[@template:multiple-choice-list-value]"> 156 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 157 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 158 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 159 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 160 <xsl:copy> 161 <!-- Remove multiple-choice-value and replace value. --> 162 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/> 163 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 164 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 165 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 166 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 167 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 168 <xsl:apply-templates select="*|node()"/> 169 </xsl:copy> 170 </xsl:template> 171 172 <!-- Replicate unknown elements. --> 173 174 <xsl:template match="@*|node()"> 175 <xsl:copy> 176 <xsl:apply-templates select="@*|node()"/> 177 </xsl:copy> 178 </xsl:template> 179 180 </xsl:stylesheet>