paulb@245 | 1 | <?xml version="1.0"?> |
paulb@274 | 2 | <!-- |
paulb@426 | 3 | A stylesheet which produces initialisation/input stylesheets for a particular |
paulb@426 | 4 | kind of document. |
paulb@426 | 5 | |
paulb@274 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@274 | 7 | |
paulb@620 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@620 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@620 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@620 | 11 | later version. |
paulb@274 | 12 | |
paulb@620 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@620 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@620 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@620 | 16 | details. |
paulb@274 | 17 | |
paulb@620 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@620 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@274 | 20 | --> |
paulb@245 | 21 | <xsl:stylesheet version="1.0" |
paulb@245 | 22 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@245 | 23 | xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" |
paulb@245 | 24 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> |
paulb@245 | 25 | |
paulb@245 | 26 | <xsl:output indent="yes"/> |
paulb@245 | 27 | <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> |
paulb@245 | 28 | |
paulb@245 | 29 | |
paulb@245 | 30 | |
paulb@365 | 31 | <xsl:param name="init-enumerations">yes</xsl:param> |
paulb@365 | 32 | |
paulb@365 | 33 | |
paulb@365 | 34 | |
paulb@245 | 35 | <!-- Match the document itself. --> |
paulb@245 | 36 | |
paulb@245 | 37 | <xsl:template match="/"> |
paulb@462 | 38 | <axsl:stylesheet version="1.0" xmlns:dyn="http://exslt.org/dynamic" |
paulb@462 | 39 | extension-element-prefixes="dyn"> |
paulb@245 | 40 | |
paulb@245 | 41 | <axsl:output indent="yes"/> |
paulb@245 | 42 | |
paulb@245 | 43 | <!-- Make document parameters for all elements appearing to use enumerations. --> |
paulb@365 | 44 | <xsl:if test="$init-enumerations = 'yes'"> |
paulb@365 | 45 | <xsl:for-each select="//element[@type='multiple-choice' or @type='multiple-choice-list']"> |
paulb@365 | 46 | <axsl:param name="{@name}"/> |
paulb@365 | 47 | </xsl:for-each> |
paulb@365 | 48 | </xsl:if> |
paulb@245 | 49 | |
paulb@245 | 50 | <!-- Make a document-level rule. --> |
paulb@245 | 51 | <axsl:template match="/"> |
paulb@489 | 52 | <axsl:apply-templates select="*" mode="{generate-id(*[1])}"/> |
paulb@245 | 53 | </axsl:template> |
paulb@245 | 54 | |
paulb@245 | 55 | <!-- Process the top-level element to make other rules. --> |
paulb@245 | 56 | <xsl:apply-templates select="*"/> |
paulb@245 | 57 | |
paulb@245 | 58 | <!-- Replicate unknown elements. --> |
paulb@273 | 59 | <axsl:template match="@*|placeholder|node()"> |
paulb@245 | 60 | <axsl:copy> |
paulb@245 | 61 | <axsl:apply-templates select="@*|node()"/> |
paulb@245 | 62 | </axsl:copy> |
paulb@245 | 63 | </axsl:template> |
paulb@245 | 64 | |
paulb@489 | 65 | <!-- Replicate placeholders for specific elements. --> |
paulb@489 | 66 | <xsl:for-each select="//element"> |
paulb@489 | 67 | <axsl:template match="placeholder" mode="{generate-id(.)}"> |
paulb@489 | 68 | <axsl:copy> |
paulb@489 | 69 | <axsl:apply-templates select="@*|node()"/> |
paulb@489 | 70 | </axsl:copy> |
paulb@489 | 71 | </axsl:template> |
paulb@489 | 72 | </xsl:for-each> |
paulb@489 | 73 | |
paulb@245 | 74 | </axsl:stylesheet> |
paulb@245 | 75 | </xsl:template> |
paulb@245 | 76 | |
paulb@245 | 77 | |
paulb@245 | 78 | |
paulb@245 | 79 | <!-- Match element references. --> |
paulb@245 | 80 | |
paulb@245 | 81 | <xsl:template match="element"> |
paulb@245 | 82 | |
paulb@245 | 83 | <!-- Make a rule for the element. --> |
paulb@489 | 84 | <axsl:template match="{@name}" mode="{generate-id(.)}"> |
paulb@245 | 85 | |
paulb@245 | 86 | <!-- Copy the element. --> |
paulb@245 | 87 | <xsl:element name="{@name}"> |
paulb@245 | 88 | |
paulb@245 | 89 | <!-- Process attributes. --> |
paulb@245 | 90 | <axsl:apply-templates select="@*"/> |
paulb@245 | 91 | |
paulb@245 | 92 | <!-- Find elements and determine how to process them. --> |
paulb@273 | 93 | <xsl:call-template name="process-elements"/> |
paulb@245 | 94 | </xsl:element> |
paulb@245 | 95 | </axsl:template> |
paulb@245 | 96 | |
paulb@245 | 97 | <!-- Make rules for nested elements. --> |
paulb@273 | 98 | <xsl:call-template name="process-rules"/> |
paulb@245 | 99 | |
paulb@245 | 100 | </xsl:template> |
paulb@245 | 101 | |
paulb@245 | 102 | |
paulb@245 | 103 | |
paulb@258 | 104 | <!-- Process elements. --> |
paulb@258 | 105 | |
paulb@273 | 106 | <xsl:template name="process-elements"> |
paulb@273 | 107 | <xsl:param name="path">.</xsl:param> |
paulb@245 | 108 | |
paulb@316 | 109 | <!-- To ensure "stable ordering" of elements, the initialised/static elements are |
paulb@316 | 110 | added first; the collection/dynamic elements are added afterwards. This may not |
paulb@316 | 111 | necessarily match the schema, however. --> |
paulb@316 | 112 | |
paulb@489 | 113 | <xsl:for-each select="element|element-ref"> |
paulb@273 | 114 | <!-- Define elements which do not have selectors. --> |
paulb@273 | 115 | <xsl:variable name="adding-selectors" select="count(//selector[@element=current()/@name])"/> |
paulb@258 | 116 | |
paulb@273 | 117 | <xsl:choose> |
paulb@489 | 118 | <!-- Recursive element references. --> |
paulb@489 | 119 | <xsl:when test="local-name(.) = 'element-ref'"> |
paulb@489 | 120 | <axsl:apply-templates select="{@name}" mode="{generate-id(ancestor::element[@name=current()/@name])}"/> |
paulb@489 | 121 | </xsl:when> |
paulb@273 | 122 | <!-- Enumerations. --> |
paulb@273 | 123 | <xsl:when test="@type='multiple-choice-value' or @type='multiple-choice-list-value'"> |
paulb@365 | 124 | <!-- Only generate enumerations if requested. --> |
paulb@365 | 125 | <xsl:if test="$init-enumerations = 'yes'"> |
paulb@365 | 126 | <xsl:call-template name="inside-enumeration"> |
paulb@365 | 127 | <xsl:with-param name="path" select="concat($path, '/', @name)"/> |
paulb@365 | 128 | </xsl:call-template> |
paulb@365 | 129 | </xsl:if> |
paulb@273 | 130 | </xsl:when> |
paulb@273 | 131 | <!-- Added elements. --> |
paulb@284 | 132 | <xsl:when test="(not(@init) or @init = 'auto') and $adding-selectors = 0 or @init = 'yes'"> |
paulb@273 | 133 | <xsl:element name="{@name}"> |
paulb@273 | 134 | <axsl:apply-templates select="{$path}/{@name}/@*"/> |
paulb@273 | 135 | <xsl:call-template name="process-elements"> |
paulb@258 | 136 | <xsl:with-param name="path" select="concat($path, '/', @name)"/> |
paulb@258 | 137 | </xsl:call-template> |
paulb@273 | 138 | </xsl:element> |
paulb@273 | 139 | </xsl:when> |
paulb@316 | 140 | <!-- Other elements are only added if found and must appear last - see below. --> |
paulb@316 | 141 | <xsl:otherwise/> |
paulb@316 | 142 | </xsl:choose> |
paulb@316 | 143 | </xsl:for-each> |
paulb@316 | 144 | |
paulb@316 | 145 | <!-- Add the collection/dynamic elements at the end. This includes placeholder |
paulb@316 | 146 | elements which may have represented the static elements. |
paulb@316 | 147 | NOTE: We may wish to exclude placeholder elements in any situation where static |
paulb@316 | 148 | NOTE: elements are employed, since the only place where keeping them around is |
paulb@316 | 149 | NOTE: necessary/meaningful is in dynamic element collections. --> |
paulb@316 | 150 | |
paulb@318 | 151 | <xsl:call-template name="produce-selection"> |
paulb@318 | 152 | <xsl:with-param name="path" select="$path"/> |
paulb@318 | 153 | <xsl:with-param name="element" select="element[1]"/> |
paulb@318 | 154 | </xsl:call-template> |
paulb@318 | 155 | |
paulb@318 | 156 | </xsl:template> |
paulb@318 | 157 | |
paulb@318 | 158 | |
paulb@318 | 159 | |
paulb@318 | 160 | <!-- Produce a selection of collection/dynamic elements. |
paulb@489 | 161 | This should produce apply-templates instructions selecting dynamic elements. --> |
paulb@318 | 162 | |
paulb@318 | 163 | <xsl:template name="produce-selection"> |
paulb@318 | 164 | <xsl:param name="path"/> |
paulb@318 | 165 | <xsl:param name="element"/> |
paulb@316 | 166 | |
paulb@318 | 167 | <xsl:choose> |
paulb@489 | 168 | <!-- While elements remain... --> |
paulb@489 | 169 | <xsl:when test="$element"> |
paulb@318 | 170 | |
paulb@318 | 171 | <!-- Define elements which do not have selectors. --> |
paulb@318 | 172 | <xsl:variable name="adding-selectors" select="count(//selector[@element=$element/@name])"/> |
paulb@318 | 173 | |
paulb@318 | 174 | <xsl:choose> |
paulb@318 | 175 | <!-- Do not select added elements or enumerations - see process-elements. --> |
paulb@318 | 176 | <xsl:when test="((not($element/@init) or $element/@init = 'auto') and $adding-selectors = 0 or $element/@init = 'yes') |
paulb@318 | 177 | or (@type='multiple-choice-value' or @type='multiple-choice-list-value')"> |
paulb@318 | 178 | <xsl:call-template name="produce-selection"> |
paulb@318 | 179 | <xsl:with-param name="path" select="$path"/> |
paulb@318 | 180 | <xsl:with-param name="element" select="$element/following-sibling::element[1]"/> |
paulb@318 | 181 | </xsl:call-template> |
paulb@318 | 182 | </xsl:when> |
paulb@318 | 183 | <!-- Other elements are only added if found. --> |
paulb@318 | 184 | <xsl:otherwise> |
paulb@489 | 185 | <axsl:apply-templates select="{$path}/placeholder|{$path}/{$element/@name}" mode="{generate-id($element)}"/> |
paulb@318 | 186 | <xsl:call-template name="produce-selection"> |
paulb@318 | 187 | <xsl:with-param name="path" select="$path"/> |
paulb@318 | 188 | <xsl:with-param name="element" select="$element/following-sibling::element[1]"/> |
paulb@318 | 189 | </xsl:call-template> |
paulb@318 | 190 | </xsl:otherwise> |
paulb@318 | 191 | </xsl:choose> |
paulb@489 | 192 | </xsl:when> |
paulb@489 | 193 | <!-- Do nothing when no elements remain. --> |
paulb@489 | 194 | <xsl:otherwise/> |
paulb@318 | 195 | </xsl:choose> |
paulb@258 | 196 | </xsl:template> |
paulb@258 | 197 | |
paulb@258 | 198 | |
paulb@258 | 199 | |
paulb@258 | 200 | <!-- Process rules. --> |
paulb@258 | 201 | |
paulb@258 | 202 | <xsl:template name="process-rules"> |
paulb@273 | 203 | <xsl:param name="path">.</xsl:param> |
paulb@258 | 204 | |
paulb@273 | 205 | <xsl:for-each select="element"> |
paulb@273 | 206 | <!-- Define elements which do not have selectors. --> |
paulb@273 | 207 | <!-- NOTE: Duplicating adding-selectors - see above. --> |
paulb@273 | 208 | <xsl:variable name="adding-selectors" select="count(//selector[@element=current()/@name])"/> |
paulb@273 | 209 | |
paulb@273 | 210 | <xsl:choose> |
paulb@273 | 211 | <xsl:when test="@type='multiple-choice-value' or @type='multiple-choice-list-value'"> |
paulb@273 | 212 | <!-- Do not match multiple-choice values. --> |
paulb@273 | 213 | </xsl:when> |
paulb@284 | 214 | <xsl:when test="(not(@init) or @init = 'auto') and $adding-selectors = 0 or @init = 'yes'"> |
paulb@263 | 215 | <xsl:call-template name="process-rules"> |
paulb@263 | 216 | <xsl:with-param name="path" select="concat($path, '/', @name)"/> |
paulb@263 | 217 | </xsl:call-template> |
paulb@273 | 218 | </xsl:when> |
paulb@273 | 219 | <xsl:otherwise> |
paulb@273 | 220 | <xsl:apply-templates select="."/> |
paulb@273 | 221 | </xsl:otherwise> |
paulb@273 | 222 | </xsl:choose> |
paulb@273 | 223 | </xsl:for-each> |
paulb@245 | 224 | </xsl:template> |
paulb@245 | 225 | |
paulb@245 | 226 | |
paulb@245 | 227 | |
paulb@245 | 228 | <!-- Fill in enumerations. --> |
paulb@245 | 229 | |
paulb@245 | 230 | <xsl:template name="inside-enumeration"> |
paulb@273 | 231 | <xsl:param name="path"/> |
paulb@263 | 232 | |
paulb@263 | 233 | <!-- Store multiple-choice selections, if appropriate. --> |
paulb@263 | 234 | <xsl:if test="../@type='multiple-choice-list'"> |
paulb@279 | 235 | <!-- NOTE: It is assumed here that ../attribute/@name (if it exists) == attribute/@name. --> |
paulb@279 | 236 | <axsl:variable name="values-{@name}" select="{$path}/@{attribute/@name}"/> |
paulb@263 | 237 | </xsl:if> |
paulb@263 | 238 | |
paulb@279 | 239 | <!-- Select inside the enumeration source, inside an element with the field's name, the enumeration elements. --> |
paulb@466 | 240 | <xsl:choose> |
paulb@466 | 241 | <xsl:when test="../@source='dynamic'"> |
paulb@466 | 242 | <axsl:for-each select="dyn:evaluate(${../@name})/{../@name}/{@name}"> |
paulb@466 | 243 | <xsl:call-template name="inside-enumeration-element"/> |
paulb@466 | 244 | </axsl:for-each> |
paulb@466 | 245 | </xsl:when> |
paulb@466 | 246 | <xsl:otherwise> |
paulb@466 | 247 | <axsl:for-each select="${../@name}/{../@name}/{@name}"> |
paulb@466 | 248 | <xsl:call-template name="inside-enumeration-element"/> |
paulb@466 | 249 | </axsl:for-each> |
paulb@466 | 250 | </xsl:otherwise> |
paulb@466 | 251 | </xsl:choose> |
paulb@466 | 252 | </xsl:template> |
paulb@466 | 253 | |
paulb@466 | 254 | <xsl:template name="inside-enumeration-element"> |
paulb@466 | 255 | <axsl:copy> |
paulb@466 | 256 | <axsl:apply-templates select="@*"/> |
paulb@466 | 257 | <xsl:if test="@type='multiple-choice-list-value'"> |
paulb@466 | 258 | <axsl:if test="$values-{@name}[string() = current()/@{attribute/@name}]"> |
paulb@466 | 259 | <axsl:attribute name="{@expr-name}">true</axsl:attribute> |
paulb@466 | 260 | </axsl:if> |
paulb@466 | 261 | </xsl:if> |
paulb@466 | 262 | <!-- Include child nodes, if provided. --> |
paulb@466 | 263 | <axsl:copy-of select="node()"/> |
paulb@466 | 264 | </axsl:copy> |
paulb@245 | 265 | </xsl:template> |
paulb@245 | 266 | |
paulb@245 | 267 | </xsl:stylesheet> |