XSLTools

XSLForms/XSL/Prepare.xsl

178:7e7d9dbcec62
2005-07-22 paulb [project @ 2005-07-22 18:26:38 by paulb] Tidied up the function names and added some API documentation. Added child-element and child-attribute functions for coherent references to potentially non-existent nodes in the form data.
     1 <?xml version="1.0"?>     2 <xsl:stylesheet version="1.0"     3   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     4   xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias"     5   xmlns:template="http://www.boddie.org.uk/ns/xmltools/template">     6      7   <xsl:output indent="yes"/>     8   <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>     9     10     11     12   <!-- Match the document itself. -->    13     14   <xsl:template match="/">    15     <axsl:stylesheet version="1.0">    16     17       <axsl:output indent="yes"/>    18       <!-- NOTE: Hard-coded doctypes to hopefully satisfy JavaScript code. -->    19       <!-- doctype-public="-//W3C//DTD XHTML 1.1//EN"    20         doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" -->    21     22       <axsl:template match="/">    23     24         <!-- Include the remaining attributes. -->    25         <xsl:apply-templates select="@*"/>    26     27         <!-- Process the elements. -->    28         <xsl:apply-templates select="*"/>    29     30       </axsl:template>    31     </axsl:stylesheet>    32   </xsl:template>    33     34     35     36   <!-- Match element references. -->    37     38   <xsl:template match="*[@template:element]">    39     <xsl:call-template name="enter-element">    40       <xsl:with-param name="other-elements" select="@template:element"/>    41     </xsl:call-template>    42   </xsl:template>    43     44   <xsl:template name="enter-element">    45     <xsl:param name="other-elements"/>    46     <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/>    47     <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/>    48     <xsl:choose>    49       <xsl:when test="$first-element = ''">    50         <xsl:call-template name="next-element">    51           <xsl:with-param name="first-element" select="$other-elements"/>    52         </xsl:call-template>    53       </xsl:when>    54       <xsl:otherwise>    55         <xsl:call-template name="next-element">    56           <xsl:with-param name="first-element" select="$first-element"/>    57           <xsl:with-param name="remaining-elements" select="$remaining-elements"/>    58         </xsl:call-template>    59       </xsl:otherwise>    60     </xsl:choose>    61   </xsl:template>    62     63   <xsl:template name="next-element">    64     <xsl:param name="first-element"/>    65     <xsl:param name="remaining-elements"/>    66     <axsl:for-each select="{$first-element}">    67       <xsl:choose>    68         <xsl:when test="$remaining-elements = ''">    69           <xsl:call-template name="enter-attribute"/>    70         </xsl:when>    71         <xsl:otherwise>    72           <xsl:call-template name="enter-element">    73             <xsl:with-param name="other-elements" select="$remaining-elements"/>    74           </xsl:call-template>    75         </xsl:otherwise>    76       </xsl:choose>    77     </axsl:for-each>    78   </xsl:template>    79     80     81     82   <!-- Match special expression attributes. -->    83     84   <xsl:template match="*[not(@template:element) and (@template:attribute or @template:value or @template:expr)]">    85     <xsl:call-template name="enter-attribute"/>    86   </xsl:template>    87     88   <xsl:template name="enter-attribute">    89     <xsl:choose>    90       <xsl:when test="@template:attribute">    91         <axsl:choose>    92           <axsl:when test="@{@template:attribute}">    93             <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable>    94             <axsl:variable name="this-value" select="@{@template:attribute}"/>    95             <xsl:call-template name="special-attributes"/>    96           </axsl:when>    97           <axsl:otherwise>    98             <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable>    99             <axsl:variable name="this-value"></axsl:variable>   100             <xsl:call-template name="special-attributes"/>   101           </axsl:otherwise>   102         </axsl:choose>   103       </xsl:when>   104       <xsl:otherwise>   105         <xsl:call-template name="special-attributes"/>   106       </xsl:otherwise>   107     </xsl:choose>   108   </xsl:template>   109    110   <xsl:template name="special-attributes">   111     <xsl:choose>   112       <xsl:when test="@template:effect = 'replace'">   113         <xsl:call-template name="special-value"/>   114       </xsl:when>   115       <xsl:otherwise>   116         <xsl:copy>   117           <xsl:apply-templates select="@*"/>   118           <xsl:if test="@template:expr and @template:expr-attr">   119             <axsl:if test="{@template:expr}">   120               <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute>   121             </axsl:if>   122           </xsl:if>   123           <xsl:call-template name="special-value"/>   124         </xsl:copy>   125       </xsl:otherwise>   126     </xsl:choose>   127   </xsl:template>   128    129   <xsl:template name="special-value">   130     <xsl:choose>   131       <xsl:when test="@template:value">   132         <axsl:value-of select="{@template:value}"/>   133       </xsl:when>   134       <xsl:otherwise>   135         <xsl:apply-templates select="node()"/>   136       </xsl:otherwise>   137     </xsl:choose>   138   </xsl:template>   139    140    141    142   <!-- Remove template elements. -->   143    144   <xsl:template match="@template:element|@template:attribute|@template:value|@template:expr|@template:expr-attr|@template:effect">   145   </xsl:template>   146    147    148    149   <!-- Replicate unknown elements. -->   150    151   <xsl:template match="@*|node()">   152     <xsl:copy>   153       <xsl:apply-templates select="@*|node()"/>   154     </xsl:copy>   155   </xsl:template>   156    157 </xsl:stylesheet>