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" select="substring-after(@template:multiple-choice-field, ',')"/> 90 <xsl:copy> 91 <!-- Remove multiple-choice-field and replace name. --> 92 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/> 93 <xsl:if test="$field-element != '-'"> 94 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 95 </xsl:if> 96 <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute> 97 <xsl:apply-templates select="*|node()"/> 98 </xsl:copy> 99 </xsl:template> 100 101 <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. --> 102 103 <xsl:template match="*[@template:multiple-choice-list-field]"> 104 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 105 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 106 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 107 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 108 <xsl:copy> 109 <!-- Remove multiple-choice-list-field and replace name. --> 110 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/> 111 <xsl:if test="$field-element != '-'"> 112 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 113 </xsl:if> 114 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 115 '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute> 116 <xsl:apply-templates select="*|node()"/> 117 </xsl:copy> 118 </xsl:template> 119 120 <!-- Multiple choice values, represented by option elements. --> 121 122 <xsl:template match="*[@template:multiple-choice-value]"> 123 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 124 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 125 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 126 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 127 <xsl:copy> 128 <!-- Remove multiple-choice-value and replace value. --> 129 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/> 130 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 131 <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 132 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 133 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 134 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 135 <xsl:apply-templates select="*|node()"/> 136 </xsl:copy> 137 </xsl:template> 138 139 <!-- Multiple choice list values, represented by option elements. --> 140 141 <xsl:template match="*[@template:multiple-choice-list-value]"> 142 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 143 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 144 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 145 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 146 <xsl:copy> 147 <!-- Remove multiple-choice-value and replace value. --> 148 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/> 149 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 150 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 151 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 152 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 153 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 154 <xsl:apply-templates select="*|node()"/> 155 </xsl:copy> 156 </xsl:template> 157 158 <!-- Replicate unknown elements. --> 159 160 <xsl:template match="@*|node()"> 161 <xsl:copy> 162 <xsl:apply-templates select="@*|node()"/> 163 </xsl:copy> 164 </xsl:template> 165 166 </xsl:stylesheet>