XSLTools

XSLForms/XSL/PrepareMacro.xsl

522:44f29f525fc7
2006-03-12 paulb [project @ 2006-03-12 22:37:41 by paulb] Made the non-installed package and examples override any installed software.
     1 <?xml version="1.0"?>     2 <!--     3 A stylesheet which expands the higher-level macro attributes, producing     4 lower-level attributes that can be translated into the final output stylesheet     5 code.     6      7 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>     8      9 This library is free software; you can redistribute it and/or    10 modify it under the terms of the GNU Lesser General Public    11 License as published by the Free Software Foundation; either    12 version 2.1 of the License, or (at your option) any later version.    13     14 This library is distributed in the hope that it will be useful,    15 but WITHOUT ANY WARRANTY; without even the implied warranty of    16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    17 Lesser General Public License for more details.    18     19 You should have received a copy of the GNU Lesser General Public    20 License along with this library; if not, write to the Free Software    21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA    22 -->    23 <xsl:stylesheet version="1.0"    24   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    25   xmlns:template="http://www.boddie.org.uk/ns/xmltools/template">    26     27   <xsl:output indent="yes"/>    28     29   <!-- Input fields. -->    30   <!-- Format: attribute -->    31     32   <xsl:template match="*[@template:attribute-field]">    33     <xsl:copy>    34       <!-- Remove attribute-field and replace name and value. -->    35       <xsl:apply-templates select="@*[local-name() != 'attribute-field' and local-name() != 'name' and local-name() != 'value']"/>    36       <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-field"/></xsl:attribute>    37       <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>    38       <xsl:attribute name="value">{$this-value}</xsl:attribute>    39       <xsl:apply-templates select="*|node()"/>    40     </xsl:copy>    41   </xsl:template>    42     43   <!-- Text areas and other elements without value attributes. -->    44   <!-- Format: attribute[,effect] -->    45     46   <xsl:template match="*[@template:attribute-area]">    47     <xsl:variable name="field-attr" select="substring-before(@template:attribute-area, ',')"/>    48     <xsl:variable name="field-effect" select="substring-after(@template:attribute-area, ',')"/>    49     <xsl:copy>    50       <!-- Remove attribute-area and replace name. -->    51       <xsl:apply-templates select="@*[local-name() != 'attribute-area' and local-name() != 'name']"/>    52       <xsl:choose>    53         <xsl:when test="$field-attr != ''">    54           <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute>    55           <xsl:if test="$field-effect != ''">    56             <xsl:attribute name="template:effect"><xsl:value-of select="$field-effect"/></xsl:attribute>    57           </xsl:if>    58           <xsl:attribute name="template:value">$this-value</xsl:attribute>    59         </xsl:when>    60         <xsl:otherwise>    61           <xsl:attribute name="template:attribute"><xsl:value-of select="@template:attribute-area"/></xsl:attribute>    62           <xsl:attribute name="template:value">$this-value</xsl:attribute>    63         </xsl:otherwise>    64       </xsl:choose>    65       <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>    66       <xsl:apply-templates select="*|node()"/>    67     </xsl:copy>    68   </xsl:template>    69     70   <!-- Buttons whose state varies according to an attribute. -->    71   <!-- Format: attribute,value,attribute-to-set -->    72     73   <xsl:template match="*[@template:attribute-button]">    74     <xsl:variable name="field-attr" select="substring-before(@template:attribute-button, ',')"/>    75     <xsl:variable name="field-info" select="substring-after(@template:attribute-button, ',')"/>    76     <xsl:variable name="field-value" select="substring-before($field-info, ',')"/>    77     <xsl:variable name="field-set-attr" select="substring-after($field-info, ',')"/>    78     <xsl:copy>    79       <!-- Remove attribute-button and replace name. -->    80       <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/>    81       <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute>    82       <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute>    83       <xsl:attribute name="template:expr">$this-value = '<xsl:value-of select="$field-value"/>'</xsl:attribute>    84       <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>    85       <xsl:attribute name="value"><xsl:value-of select="$field-value"/></xsl:attribute>    86       <xsl:apply-templates select="*|node()"/>    87     </xsl:copy>    88   </xsl:template>    89     90   <!-- Buttons whose state varies according to an attribute in a list of multiple choice elements. -->    91   <!-- Format: attribute,attribute-to-set -->    92     93   <xsl:template match="*[@template:attribute-list-button]">    94     <xsl:variable name="field-attr" select="substring-before(@template:attribute-list-button, ',')"/>    95     <xsl:variable name="field-set-attr" select="substring-after(@template:attribute-list-button, ',')"/>    96     <xsl:copy>    97       <!-- Remove attribute-button and replace name. -->    98       <xsl:apply-templates select="@*[local-name() != 'attribute-button' and local-name() != 'name']"/>    99       <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute>   100       <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute>   101       <xsl:attribute name="template:expr">@value-is-set</xsl:attribute>   102       <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>   103       <xsl:attribute name="value">{$this-value}</xsl:attribute>   104       <xsl:apply-templates select="*|node()"/>   105     </xsl:copy>   106   </xsl:template>   107    108   <!-- Selectors. -->   109   <!-- Format: name[,affected-element] -->   110    111   <xsl:template match="*[@template:selector-field]">   112     <xsl:variable name="field-name" select="substring-before(@template:selector-field, ',')"/>   113     <xsl:variable name="affected-element" select="substring-after(@template:selector-field, ',')"/>   114     <xsl:copy>   115       <!-- Remove selector-field and replace name. -->   116       <xsl:apply-templates select="@*[local-name() != 'selector-field' and local-name() != 'name']"/>   117       <xsl:choose>   118         <xsl:when test="$field-name != ''">   119           <xsl:attribute name="name"><xsl:value-of select="$field-name"/>={template:this-element()}</xsl:attribute>   120         </xsl:when>   121         <xsl:otherwise>   122           <xsl:attribute name="name"><xsl:value-of select="@template:selector-field"/>={template:this-element()}</xsl:attribute>   123         </xsl:otherwise>   124       </xsl:choose>   125       <xsl:apply-templates select="*|node()"/>   126     </xsl:copy>   127   </xsl:template>   128    129   <!-- Multiple choice fields, represented by menus and listboxes. -->   130   <!-- Format: element,attribute[,attribute-type|,attribute-type,source-type] -->   131    132   <xsl:template match="*[@template:multiple-choice-field]">   133     <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-field, ',')"/>   134     <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-field, ',')"/>   135     <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/>   136     <xsl:variable name="field-attr-type-info" select="substring-after($field-attr-info, ',')"/>   137     <xsl:variable name="field-attr-type" select="substring-before($field-attr-type-info, ',')"/>   138     <xsl:variable name="field-source-type" select="substring-after($field-attr-type-info, ',')"/>   139     <xsl:copy>   140       <!-- Remove multiple-choice-field and replace name. -->   141       <xsl:apply-templates select="@*[local-name() != 'multiple-choice-field' and local-name() != 'name']"/>   142       <xsl:if test="$field-element != '-'">   143         <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute>   144       </xsl:if>   145       <xsl:choose>   146         <xsl:when test="$field-attr-type = 'new' or field-attr-type = '' and $field-attr-type-info = 'new'">   147           <xsl:attribute name="name">{template:new-attribute('<xsl:value-of select="$field-attr"/>')}</xsl:attribute>   148         </xsl:when>   149         <xsl:when test="$field-attr = ''">   150           <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr-info"/></xsl:attribute>   151           <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>   152         </xsl:when>   153         <xsl:otherwise>   154           <xsl:attribute name="template:attribute"><xsl:value-of select="$field-attr"/></xsl:attribute>   155           <xsl:attribute name="name">{template:this-attribute()}</xsl:attribute>   156         </xsl:otherwise>   157       </xsl:choose>   158       <xsl:apply-templates select="*|node()"/>   159     </xsl:copy>   160   </xsl:template>   161    162   <!-- Multiple choice list fields, represented by menus and listboxes with multiple values. -->   163   <!-- Format: element,list-element,list-attribute[,source-type] -->   164    165   <xsl:template match="*[@template:multiple-choice-list-field]">   166     <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-field, ',')"/>   167     <xsl:variable name="field-list-info" select="substring-after(@template:multiple-choice-list-field, ',')"/>   168     <xsl:variable name="field-list-element" select="substring-before($field-list-info, ',')"/>   169     <xsl:variable name="field-list-attr-info" select="substring-after($field-list-info, ',')"/>   170     <xsl:variable name="field-list-attr" select="substring-before($field-list-attr-info, ',')"/>   171     <xsl:variable name="field-source-type" select="substring-after($field-list-attr-info, ',')"/>   172     <xsl:copy>   173       <!-- Remove multiple-choice-list-field and replace name. -->   174       <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-field' and local-name() != 'name']"/>   175       <xsl:if test="$field-element != '-'">   176         <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute>   177       </xsl:if>   178       <xsl:choose>   179         <xsl:when test="$field-list-attr != ''">   180           <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>',   181             '<xsl:value-of select="$field-list-attr"/>')}</xsl:attribute>   182         </xsl:when>   183         <xsl:otherwise>   184           <xsl:attribute name="name">{template:list-attribute('<xsl:value-of select="$field-list-element"/>',   185             '<xsl:value-of select="$field-list-attr-info"/>')}</xsl:attribute>   186         </xsl:otherwise>   187       </xsl:choose>   188       <xsl:apply-templates select="*|node()"/>   189     </xsl:copy>   190   </xsl:template>   191    192   <!-- Multiple choice values, represented by option elements. -->   193   <!-- Format: element,attribute,attribute-to-set[,contents-expr] -->   194    195   <xsl:template match="*[@template:multiple-choice-value]">   196     <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-value, ',')"/>   197     <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-value, ',')"/>   198     <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/>   199     <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/>   200     <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/>   201     <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/>   202     <xsl:copy>   203       <!-- Remove multiple-choice-value and replace value. -->   204       <xsl:apply-templates select="@*[local-name() != 'multiple-choice-value' and local-name() != 'value']"/>   205       <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute>   206       <xsl:attribute name="template:expr">@<xsl:value-of select="$field-attr"/> = ../@<xsl:value-of select="$field-attr"/></xsl:attribute>   207       <!-- For the option text... -->   208       <xsl:choose>   209         <!-- Either provide the stated attribute as the eventual text of an option element. -->   210         <xsl:when test="$field-set-attr = ''">   211           <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute>   212           <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute>   213         </xsl:when>   214         <!-- Or get the specific contents. -->   215         <xsl:otherwise>   216           <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute>   217           <xsl:attribute name="template:value"><xsl:value-of select="$field-contents"/></xsl:attribute>   218         </xsl:otherwise>   219       </xsl:choose>   220       <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute>   221       <xsl:apply-templates select="*|node()"/>   222     </xsl:copy>   223   </xsl:template>   224    225   <!-- Multiple choice list values, represented by option elements. -->   226   <!-- Format: list-element,list-attribute,attribute-to-set[,contents-expr] -->   227    228   <xsl:template match="*[@template:multiple-choice-list-value]">   229     <xsl:variable name="field-element" select="substring-before(@template:multiple-choice-list-value, ',')"/>   230     <xsl:variable name="field-attr-info" select="substring-after(@template:multiple-choice-list-value, ',')"/>   231     <xsl:variable name="field-attr" select="substring-before($field-attr-info, ',')"/>   232     <xsl:variable name="field-set-attr-info" select="substring-after($field-attr-info, ',')"/>   233     <xsl:variable name="field-set-attr" select="substring-before($field-set-attr-info, ',')"/>   234     <xsl:variable name="field-contents" select="substring-after($field-set-attr-info, ',')"/>   235     <xsl:copy>   236       <!-- Remove multiple-choice-value and replace value. -->   237       <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value' and local-name() != 'value']"/>   238       <xsl:attribute name="template:element"><xsl:value-of select="$field-element"/></xsl:attribute>   239       <xsl:attribute name="template:expr">@value-is-set</xsl:attribute>   240       <!-- For the option text... -->   241       <xsl:choose>   242         <!-- Either provide the stated attribute as the eventual text of an option element. -->   243         <xsl:when test="$field-set-attr = ''">   244           <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr-info"/></xsl:attribute>   245           <xsl:attribute name="template:value">@<xsl:value-of select="$field-attr"/></xsl:attribute>   246         </xsl:when>   247         <!-- Or get the specific contents. -->   248         <xsl:otherwise>   249           <xsl:attribute name="template:expr-attr"><xsl:value-of select="$field-set-attr"/></xsl:attribute>   250           <xsl:attribute name="template:value"><xsl:value-of select="$field-contents"/></xsl:attribute>   251         </xsl:otherwise>   252       </xsl:choose>   253       <xsl:attribute name="value">{@<xsl:value-of select="$field-attr"/>}</xsl:attribute>   254       <xsl:apply-templates select="*|node()"/>   255     </xsl:copy>   256   </xsl:template>   257    258   <!-- Multiple choice elements. -->   259   <!-- Format: element,list-element,list-attribute -->   260    261   <xsl:template match="*[@template:multiple-choice-list-element]">   262     <xsl:variable name="element" select="substring-before(@template:multiple-choice-list-element, ',')"/>   263     <xsl:variable name="element-list-info" select="substring-after(@template:multiple-choice-list-element, ',')"/>   264     <xsl:variable name="element-list-element" select="substring-before($element-list-info, ',')"/>   265     <xsl:variable name="element-list-attr" select="substring-after($element-list-info, ',')"/>   266     <xsl:copy>   267       <!-- Remove multiple-choice-list-element. -->   268       <xsl:apply-templates select="@*[local-name() != 'multiple-choice-list-value']"/>   269       <xsl:attribute name="template:element"><xsl:value-of select="$element"/>,<xsl:value-of select="$element-list-element"/></xsl:attribute>   270       <!-- The attribute is ignored - it is only useful in the schema and input processes. -->   271       <xsl:apply-templates select="*|node()"/>   272     </xsl:copy>   273   </xsl:template>   274    275   <!-- Replicate unknown elements. -->   276    277   <xsl:template match="@*|node()">   278     <xsl:copy>   279       <xsl:apply-templates select="@*|node()"/>   280     </xsl:copy>   281   </xsl:template>   282    283 </xsl:stylesheet>