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:copy> 25 <!-- Remove attribute-field and replace name and value. --> 26 <xsl:apply-templates select="@*[local-name() != 'attribute-area' and local-name() != 'name' and local-name() != 'value']"/> 27 <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-area"/></xsl:attribute> 28 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 29 <xsl:apply-templates select="*|node()"/> 30 </xsl:copy> 31 </xsl:template> 32 33 <!-- Buttons whose state varies according to an attribute. --> 34 35 <xsl:template match="*[@template:attribute-button]"> 36 <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/> 37 <xsl:variable name="field-set-attr" select="substring-after(@template:attribute-button, ',')"/> 38 <xsl:copy> 39 <!-- Remove attribute-button and replace name. --> 40 <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/> 41 <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute> 42 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 43 <xsl:attribute name="template:expr">$this-value != ''</xsl:attribute> 44 <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute> 45 <xsl:apply-templates select="*|node()"/> 46 </xsl:copy> 47 </xsl:template> 48 49 <!-- Selectors. --> 50 51 <xsl:template match="*[@template:selector-field]"> 52 <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/> 53 <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/> 54 <xsl:copy> 55 <!-- Remove selector-field and replace name. --> 56 <xsl:apply-templates select="@*[local-name() != 'selector-field' and local-name() != 'name']"/> 57 <xsl:choose> 58 <xsl:when test="$field-name != ''"> 59 <xsl:attribute name="name"><xsl:value-of select="$field-name"/>={template:this-element()}</xsl:attribute> 60 </xsl:when> 61 <xsl:otherwise> 62 <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/>={template:this-element()}</xsl:attribute> 63 </xsl:otherwise> 64 </xsl:choose> 65 <xsl:apply-templates select="*|node()"/> 66 </xsl:copy> 67 </xsl:template> 68 69 <!-- Multiple choice fields, represented by menus and listboxes. --> 70 71 <xsl:template match="*[@template:multiple-choice-field]"> 72 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/> 73 <xsl:variable name="field-attr" select="substring-after(@template:multiple-choice-field, ',')"/> 74 <xsl:copy> 75 <!-- Remove multiple-choice-field and replace name. --> 76 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/> 77 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 78 <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute> 79 <xsl:apply-templates select="*|node()"/> 80 </xsl:copy> 81 </xsl:template> 82 83 <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. --> 84 85 <xsl:template match="*[@template:multiple-choice-list-field]"> 86 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/> 87 <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/> 88 <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/> 89 <xsl:variable name="field-list-attr" select="substring-after($field-list-info, ',')"/> 90 <xsl:copy> 91 <!-- Remove multiple-choice-list-field and replace name. --> 92 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/> 93 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 94 <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>', 95 '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute> 96 <xsl:apply-templates select="*|node()"/> 97 </xsl:copy> 98 </xsl:template> 99 100 <!-- Multiple choice values, represented by option elements. --> 101 102 <xsl:template match="*[@template:multiple-choice-value]"> 103 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/> 104 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/> 105 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 106 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 107 <xsl:copy> 108 <!-- Remove multiple-choice-value and replace value. --> 109 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/> 110 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 111 <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute> 112 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 113 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 114 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 115 <xsl:apply-templates select="*|node()"/> 116 </xsl:copy> 117 </xsl:template> 118 119 <!-- Multiple choice list values, represented by option elements. --> 120 121 <xsl:template match="*[@template:multiple-choice-list-value]"> 122 <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/> 123 <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/> 124 <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/> 125 <xsl:variable name="field-set-attr" select="substring-after($field-attr-info, ',')"/> 126 <xsl:copy> 127 <!-- Remove multiple-choice-value and replace value. --> 128 <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/> 129 <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute> 130 <xsl:attribute name="template:expr">@value-is-set</xsl:attribute> 131 <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute> 132 <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute> 133 <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute> 134 <xsl:apply-templates select="*|node()"/> 135 </xsl:copy> 136 </xsl:template> 137 138 <!-- Replicate unknown elements. --> 139 140 <xsl:template match="@*|node()"> 141 <xsl:copy> 142 <xsl:apply-templates select="@*|node()"/> 143 </xsl:copy> 144 </xsl:template> 145 146 </xsl:stylesheet>