XSLTools

Changeset

489:0014798ee7d8
2006-01-27 paulb raw files shortlog changelog graph [project @ 2006-01-27 17:00:08 by paulb] Added recursive element reference support to the schema generation and to the input stylesheet preparation, simplifying the template rules in the former slightly.
XSLForms/XSL/Input.xsl (file) XSLForms/XSL/Schema.xsl (file)
     1.1 --- a/XSLForms/XSL/Input.xsl	Fri Jan 27 00:44:13 2006 +0000
     1.2 +++ b/XSLForms/XSL/Input.xsl	Fri Jan 27 17:00:08 2006 +0000
     1.3 @@ -50,7 +50,7 @@
     1.4  
     1.5        <!-- Make a document-level rule. -->
     1.6        <axsl:template match="/">
     1.7 -        <axsl:apply-templates select="*"/>
     1.8 +        <axsl:apply-templates select="*" mode="{generate-id(*[1])}"/>
     1.9        </axsl:template>
    1.10  
    1.11        <!-- Process the top-level element to make other rules. -->
    1.12 @@ -63,6 +63,15 @@
    1.13          </axsl:copy>
    1.14        </axsl:template>
    1.15  
    1.16 +      <!-- Replicate placeholders for specific elements. -->
    1.17 +      <xsl:for-each select="//element">
    1.18 +        <axsl:template match="placeholder" mode="{generate-id(.)}">
    1.19 +          <axsl:copy>
    1.20 +            <axsl:apply-templates select="@*|node()"/>
    1.21 +          </axsl:copy>
    1.22 +        </axsl:template>
    1.23 +      </xsl:for-each>
    1.24 +
    1.25      </axsl:stylesheet>
    1.26    </xsl:template>
    1.27  
    1.28 @@ -73,7 +82,7 @@
    1.29    <xsl:template match="element">
    1.30  
    1.31      <!-- Make a rule for the element. -->
    1.32 -    <axsl:template match="{@name}">
    1.33 +    <axsl:template match="{@name}" mode="{generate-id(.)}">
    1.34  
    1.35        <!-- Copy the element. -->
    1.36        <xsl:element name="{@name}">
    1.37 @@ -102,11 +111,15 @@
    1.38           added first; the collection/dynamic elements are added afterwards. This may not
    1.39           necessarily match the schema, however. -->
    1.40  
    1.41 -    <xsl:for-each select="element">
    1.42 +    <xsl:for-each select="element|element-ref">
    1.43        <!-- Define elements which do not have selectors. -->
    1.44        <xsl:variable name="adding-selectors" select="count(//selector[@element=current()/@name])"/>
    1.45  
    1.46        <xsl:choose>
    1.47 +        <!-- Recursive element references. -->
    1.48 +        <xsl:when test="local-name(.) = 'element-ref'">
    1.49 +          <axsl:apply-templates select="{@name}" mode="{generate-id(ancestor::element[@name=current()/@name])}"/>
    1.50 +        </xsl:when>
    1.51          <!-- Enumerations. -->
    1.52          <xsl:when test="@type='multiple-choice-value' or @type='multiple-choice-list-value'">
    1.53            <!-- Only generate enumerations if requested. -->
    1.54 @@ -138,7 +151,6 @@
    1.55  
    1.56      <xsl:call-template name="produce-selection">
    1.57        <xsl:with-param name="path" select="$path"/>
    1.58 -      <xsl:with-param name="selection" select="concat($path, '/placeholder')"/>
    1.59        <xsl:with-param name="element" select="element[1]"/>
    1.60      </xsl:call-template>
    1.61  
    1.62 @@ -147,22 +159,15 @@
    1.63  
    1.64  
    1.65    <!-- Produce a selection of collection/dynamic elements.
    1.66 -       This should produce an apply-templates instruction selecting dynamic elements.
    1.67 -       For example:
    1.68 -       ./placeholder|./some-element|./some-other-element -->
    1.69 +       This should produce apply-templates instructions selecting dynamic elements. -->
    1.70  
    1.71    <xsl:template name="produce-selection">
    1.72      <xsl:param name="path"/>
    1.73 -    <xsl:param name="selection"/>
    1.74      <xsl:param name="element"/>
    1.75  
    1.76      <xsl:choose>
    1.77 -      <!-- After processing all elements, produce the selection instruction. -->
    1.78 -      <xsl:when test="not($element)">
    1.79 -        <axsl:apply-templates select="{$selection}"/>
    1.80 -      </xsl:when>
    1.81 -      <!-- Otherwise, process the remaining elements. -->
    1.82 -      <xsl:otherwise>
    1.83 +      <!-- While elements remain... -->
    1.84 +      <xsl:when test="$element">
    1.85  
    1.86          <!-- Define elements which do not have selectors. -->
    1.87          <xsl:variable name="adding-selectors" select="count(//selector[@element=$element/@name])"/>
    1.88 @@ -173,21 +178,21 @@
    1.89                            or (@type='multiple-choice-value' or @type='multiple-choice-list-value')">
    1.90              <xsl:call-template name="produce-selection">
    1.91                <xsl:with-param name="path" select="$path"/>
    1.92 -              <xsl:with-param name="selection" select="$selection"/>
    1.93                <xsl:with-param name="element" select="$element/following-sibling::element[1]"/>
    1.94              </xsl:call-template>
    1.95            </xsl:when>
    1.96            <!-- Other elements are only added if found. -->
    1.97            <xsl:otherwise>
    1.98 +            <axsl:apply-templates select="{$path}/placeholder|{$path}/{$element/@name}" mode="{generate-id($element)}"/>
    1.99              <xsl:call-template name="produce-selection">
   1.100                <xsl:with-param name="path" select="$path"/>
   1.101 -              <xsl:with-param name="selection" select="concat($selection, '|', $path, '/', $element/@name)"/>
   1.102                <xsl:with-param name="element" select="$element/following-sibling::element[1]"/>
   1.103              </xsl:call-template>
   1.104            </xsl:otherwise>
   1.105          </xsl:choose>
   1.106 -
   1.107 -      </xsl:otherwise>
   1.108 +      </xsl:when>
   1.109 +      <!-- Do nothing when no elements remain. -->
   1.110 +      <xsl:otherwise/>
   1.111      </xsl:choose>
   1.112    </xsl:template>
   1.113  
     2.1 --- a/XSLForms/XSL/Schema.xsl	Fri Jan 27 00:44:13 2006 +0000
     2.2 +++ b/XSLForms/XSL/Schema.xsl	Fri Jan 27 17:00:08 2006 +0000
     2.3 @@ -18,7 +18,9 @@
     2.4  -->
     2.5  <xsl:stylesheet version="1.0"
     2.6    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     2.7 -  xmlns:template="http://www.boddie.org.uk/ns/xmltools/template">
     2.8 +  xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"
     2.9 +  xmlns:str="http://exslt.org/strings"
    2.10 +  extension-element-prefixes="str">
    2.11  
    2.12    <xsl:output indent="yes"/>
    2.13  
    2.14 @@ -35,7 +37,7 @@
    2.15  
    2.16    <!-- Match element references. -->
    2.17  
    2.18 -  <xsl:template match="*[@template:element]">
    2.19 +  <xsl:template match="*[@template:element]" priority="1">
    2.20      <xsl:call-template name="enter-element">
    2.21        <xsl:with-param name="other-elements" select="@template:element"/>
    2.22        <xsl:with-param name="other-init" select="@template:init"/>
    2.23 @@ -72,30 +74,43 @@
    2.24      <xsl:param name="remaining-elements"/>
    2.25      <xsl:param name="first-init"/>
    2.26      <xsl:param name="remaining-init"/>
    2.27 -    <element>
    2.28 -      <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute>
    2.29 -      <xsl:if test="$first-init != ''">
    2.30 -        <xsl:attribute name="init"><xsl:value-of select="$first-init"/></xsl:attribute>
    2.31 -      </xsl:if>
    2.32 -      <xsl:choose>
    2.33 -        <xsl:when test="$remaining-elements = ''">
    2.34 -          <xsl:call-template name="enter-attribute"/>
    2.35 -        </xsl:when>
    2.36 -        <xsl:otherwise>
    2.37 -          <xsl:call-template name="enter-element">
    2.38 -            <xsl:with-param name="other-elements" select="$remaining-elements"/>
    2.39 -            <xsl:with-param name="other-init" select="$remaining-init"/>
    2.40 -          </xsl:call-template>
    2.41 -        </xsl:otherwise>
    2.42 -      </xsl:choose>
    2.43 -    </element>
    2.44 +    <!-- Test for recursive references. -->
    2.45 +    <xsl:variable name="recursive-element" select="ancestor::*[$first-element = str:split(@template:element, ',')[1]]"/>
    2.46 +    <xsl:choose>
    2.47 +      <!-- Generate a reference to the previous element definition. -->
    2.48 +      <xsl:when test="$recursive-element">
    2.49 +        <element-ref>
    2.50 +          <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute>
    2.51 +        </element-ref>
    2.52 +      </xsl:when>
    2.53 +      <!-- Generate a normal, nested element definition. -->
    2.54 +      <xsl:otherwise>
    2.55 +        <element>
    2.56 +          <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute>
    2.57 +          <xsl:if test="$first-init != ''">
    2.58 +            <xsl:attribute name="init"><xsl:value-of select="$first-init"/></xsl:attribute>
    2.59 +          </xsl:if>
    2.60 +          <xsl:choose>
    2.61 +            <xsl:when test="$remaining-elements = ''">
    2.62 +              <xsl:call-template name="enter-attribute"/>
    2.63 +            </xsl:when>
    2.64 +            <xsl:otherwise>
    2.65 +              <xsl:call-template name="enter-element">
    2.66 +                <xsl:with-param name="other-elements" select="$remaining-elements"/>
    2.67 +                <xsl:with-param name="other-init" select="$remaining-init"/>
    2.68 +              </xsl:call-template>
    2.69 +            </xsl:otherwise>
    2.70 +          </xsl:choose>
    2.71 +        </element>
    2.72 +      </xsl:otherwise>
    2.73 +    </xsl:choose>
    2.74    </xsl:template>
    2.75  
    2.76  
    2.77  
    2.78    <!-- Match attributes. -->
    2.79  
    2.80 -  <xsl:template match="*[not(@template:element) and (@template:attribute or @template:attribute-field or @template:attribute-area or @template:attribute-button or @template:attribute-list-button or @template:selector-field or @template:multiple-choice-field or @template:multiple-choice-list-field or @template:multiple-choice-value or @template:multiple-choice-list-value) or @template:multiple-choice-list-element]">
    2.81 +  <xsl:template match="*[@template:attribute or @template:attribute-field or @template:attribute-area or @template:attribute-button or @template:attribute-list-button or @template:selector-field or @template:multiple-choice-field or @template:multiple-choice-list-field or @template:multiple-choice-value or @template:multiple-choice-list-value or @template:multiple-choice-list-element]">
    2.82      <xsl:call-template name="enter-attribute"/>
    2.83    </xsl:template>
    2.84