paulb@1 | 1 | <?xml version="1.0"?> |
paulb@274 | 2 | <!-- |
paulb@426 | 3 | A stylesheet which takes lower-level template annotations and produces an output |
paulb@426 | 4 | stylesheet - something which is capable of transforming XML documents into Web |
paulb@426 | 5 | pages or other kinds of XML documents. |
paulb@426 | 6 | |
paulb@564 | 7 | Copyright (C) 2005, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@274 | 8 | |
paulb@620 | 9 | This program is free software; you can redistribute it and/or modify it under |
paulb@620 | 10 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@620 | 11 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@620 | 12 | later version. |
paulb@274 | 13 | |
paulb@620 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@620 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@620 | 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@620 | 17 | details. |
paulb@274 | 18 | |
paulb@620 | 19 | You should have received a copy of the GNU Lesser General Public License along |
paulb@620 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@274 | 21 | --> |
paulb@1 | 22 | <xsl:stylesheet version="1.0" |
paulb@1 | 23 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@1 | 24 | xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" |
paulb@481 | 25 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template" |
paulb@481 | 26 | xmlns:str="http://exslt.org/strings" |
paulb@481 | 27 | extension-element-prefixes="str"> |
paulb@1 | 28 | |
paulb@1 | 29 | <xsl:output indent="yes"/> |
paulb@1 | 30 | <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> |
paulb@1 | 31 | |
paulb@481 | 32 | <!-- Fragment extraction support. --> |
paulb@481 | 33 | <xsl:param name="element-id"/> |
paulb@40 | 34 | |
paulb@1 | 35 | <!-- Match the document itself. --> |
paulb@1 | 36 | |
paulb@1 | 37 | <xsl:template match="/"> |
paulb@481 | 38 | <axsl:stylesheet version="1.0" |
paulb@481 | 39 | xmlns:dyn="http://exslt.org/dynamic" |
paulb@481 | 40 | extension-element-prefixes="dyn"> |
paulb@40 | 41 | |
paulb@505 | 42 | <!-- Add support for special namespace declarations. --> |
paulb@505 | 43 | <xsl:for-each select="//@*[local-name() = 'expr-prefix']"> |
paulb@505 | 44 | <xsl:attribute namespace="{namespace-uri()}" name="{name(.)}"></xsl:attribute> |
paulb@505 | 45 | </xsl:for-each> |
paulb@505 | 46 | |
paulb@40 | 47 | <axsl:output indent="yes"/> |
paulb@40 | 48 | |
paulb@432 | 49 | <!-- Include internationalisation (i18n) support if appropriate. --> |
paulb@435 | 50 | <axsl:param name="translations"/> |
paulb@435 | 51 | <axsl:param name="locale"/> |
paulb@432 | 52 | |
paulb@481 | 53 | <!-- Include fragment support if appropriate. --> |
paulb@481 | 54 | <axsl:param name="element-path"/> |
paulb@481 | 55 | |
paulb@1 | 56 | <axsl:template match="/"> |
paulb@481 | 57 | <xsl:choose> |
paulb@481 | 58 | <!-- Fragment production. --> |
paulb@481 | 59 | <xsl:when test="$element-id != ''"> |
paulb@481 | 60 | <axsl:for-each select="dyn:evaluate($element-path)"> |
paulb@481 | 61 | <xsl:apply-templates select="@*|node()"/> |
paulb@481 | 62 | </axsl:for-each> |
paulb@481 | 63 | </xsl:when> |
paulb@481 | 64 | <!-- Whole template production. --> |
paulb@481 | 65 | <xsl:otherwise> |
paulb@481 | 66 | <xsl:apply-templates select="@*|node()"/> |
paulb@481 | 67 | </xsl:otherwise> |
paulb@481 | 68 | </xsl:choose> |
paulb@481 | 69 | </axsl:template> |
paulb@1 | 70 | |
paulb@481 | 71 | <!-- Produce the rules for each element. --> |
paulb@481 | 72 | |
paulb@481 | 73 | <xsl:apply-templates select="//*[@template:element]"> |
paulb@481 | 74 | <xsl:with-param name="top-level">true</xsl:with-param> |
paulb@481 | 75 | </xsl:apply-templates> |
paulb@481 | 76 | |
paulb@481 | 77 | </axsl:stylesheet> |
paulb@481 | 78 | </xsl:template> |
paulb@481 | 79 | |
paulb@481 | 80 | |
paulb@481 | 81 | |
paulb@481 | 82 | <!-- Match elements referencing elements. --> |
paulb@481 | 83 | |
paulb@481 | 84 | <xsl:template match="*[@template:element]" priority="1"> |
paulb@481 | 85 | <xsl:param name="top-level">false</xsl:param> |
paulb@481 | 86 | <xsl:call-template name="enter-element"> |
paulb@481 | 87 | <xsl:with-param name="top-level" select="$top-level"/> |
paulb@481 | 88 | </xsl:call-template> |
paulb@481 | 89 | </xsl:template> |
paulb@1 | 90 | |
paulb@481 | 91 | <xsl:template name="enter-element"> |
paulb@481 | 92 | <xsl:param name="top-level"/> |
paulb@481 | 93 | <xsl:choose> |
paulb@481 | 94 | <!-- Produce templates where this is a top-level definition. --> |
paulb@481 | 95 | <xsl:when test="$top-level = 'true'"> |
paulb@481 | 96 | <xsl:call-template name="element-template"> |
paulb@481 | 97 | <xsl:with-param name="element-names" select="@template:element"/> |
paulb@481 | 98 | </xsl:call-template> |
paulb@481 | 99 | </xsl:when> |
paulb@481 | 100 | <!-- Produce references to elements where this is within a template. --> |
paulb@481 | 101 | <xsl:otherwise> |
paulb@481 | 102 | <xsl:variable name="first-name" select="str:split(@template:element, ',')[1]"/> |
paulb@481 | 103 | <!-- Check to see if this is a recursive reference. --> |
paulb@481 | 104 | <xsl:variable name="recursive-element" select="ancestor::*[$first-name = str:split(@template:element, ',')[1]]"/> |
paulb@481 | 105 | <xsl:choose> |
paulb@481 | 106 | <!-- Generate a reference to the previous element definition. --> |
paulb@481 | 107 | <xsl:when test="$recursive-element"> |
paulb@481 | 108 | <axsl:apply-templates select="{$first-name}" mode="{generate-id($recursive-element)}"/> |
paulb@481 | 109 | </xsl:when> |
paulb@481 | 110 | <!-- Generate a reference to this element definition. --> |
paulb@481 | 111 | <xsl:otherwise> |
paulb@481 | 112 | <axsl:apply-templates select="{$first-name}" mode="{generate-id(.)}"/> |
paulb@481 | 113 | </xsl:otherwise> |
paulb@481 | 114 | </xsl:choose> |
paulb@481 | 115 | </xsl:otherwise> |
paulb@481 | 116 | </xsl:choose> |
paulb@481 | 117 | </xsl:template> |
paulb@1 | 118 | |
paulb@481 | 119 | <xsl:template name="element-template"> |
paulb@481 | 120 | <xsl:param name="element-names"/> |
paulb@481 | 121 | <xsl:variable name="this-name" select="substring-before($element-names, ',')"/> |
paulb@481 | 122 | <xsl:variable name="next-names" select="substring-after($element-names, ',')"/> |
paulb@481 | 123 | <xsl:variable name="next-name" select="str:split($next-names, ',')[1]"/> |
paulb@481 | 124 | <xsl:choose> |
paulb@481 | 125 | <!-- Non-last part of a list of element names. --> |
paulb@481 | 126 | <!-- Produce a template referencing another template. --> |
paulb@481 | 127 | <xsl:when test="$this-name != ''"> |
paulb@481 | 128 | <!-- Produce a template with a mode. --> |
paulb@481 | 129 | <axsl:template match="{$this-name}" mode="{generate-id(.)}"> |
paulb@481 | 130 | <axsl:apply-templates select="{$next-name}" mode="{generate-id(.)}"/> |
paulb@481 | 131 | </axsl:template> |
paulb@481 | 132 | <!-- Produce the other elements' templates... --> |
paulb@481 | 133 | <xsl:call-template name="element-template"> |
paulb@481 | 134 | <xsl:with-param name="element-names" select="$next-names"/> |
paulb@481 | 135 | </xsl:call-template> |
paulb@481 | 136 | </xsl:when> |
paulb@481 | 137 | <!-- Last part of a list of element names. --> |
paulb@481 | 138 | <!-- Produce a template with content. --> |
paulb@481 | 139 | <xsl:otherwise> |
paulb@481 | 140 | <!-- Produce a template with a mode. --> |
paulb@481 | 141 | <axsl:template match="{$element-names}" mode="{generate-id(.)}"> |
paulb@481 | 142 | <xsl:call-template name="enter-attribute"/> |
paulb@481 | 143 | </axsl:template> |
paulb@481 | 144 | </xsl:otherwise> |
paulb@481 | 145 | </xsl:choose> |
paulb@1 | 146 | </xsl:template> |
paulb@1 | 147 | |
paulb@1 | 148 | |
paulb@1 | 149 | |
paulb@198 | 150 | <!-- Match special conditional expression attributes. --> |
paulb@198 | 151 | |
paulb@481 | 152 | <xsl:template match="*[@template:if]" priority="2"> |
paulb@481 | 153 | <xsl:param name="top-level">false</xsl:param> |
paulb@1 | 154 | <xsl:choose> |
paulb@481 | 155 | <!-- Since this rule may be invoked at the top level, ignore conditions. --> |
paulb@481 | 156 | <xsl:when test="$top-level = 'true'"> |
paulb@481 | 157 | <xsl:call-template name="enter-element"> |
paulb@481 | 158 | <xsl:with-param name="top-level" select="$top-level"/> |
paulb@1 | 159 | </xsl:call-template> |
paulb@1 | 160 | </xsl:when> |
paulb@481 | 161 | <!-- As part of a template, generate the condition. --> |
paulb@1 | 162 | <xsl:otherwise> |
paulb@481 | 163 | <axsl:if test="{@template:if}"> |
paulb@481 | 164 | <xsl:choose> |
paulb@481 | 165 | <xsl:when test="@template:element"> |
paulb@481 | 166 | <xsl:call-template name="enter-element"/> |
paulb@481 | 167 | </xsl:when> |
paulb@481 | 168 | <xsl:otherwise> |
paulb@481 | 169 | <xsl:call-template name="enter-attribute"/> |
paulb@481 | 170 | </xsl:otherwise> |
paulb@481 | 171 | </xsl:choose> |
paulb@481 | 172 | </axsl:if> |
paulb@1 | 173 | </xsl:otherwise> |
paulb@1 | 174 | </xsl:choose> |
paulb@1 | 175 | </xsl:template> |
paulb@1 | 176 | |
paulb@1 | 177 | |
paulb@1 | 178 | |
paulb@1 | 179 | <!-- Match special expression attributes. --> |
paulb@1 | 180 | |
paulb@481 | 181 | <xsl:template match="*[@template:attribute or @template:value or @template:expr or @template:copy]"> |
paulb@126 | 182 | <xsl:call-template name="enter-attribute"/> |
paulb@126 | 183 | </xsl:template> |
paulb@126 | 184 | |
paulb@126 | 185 | <xsl:template name="enter-attribute"> |
paulb@126 | 186 | <xsl:choose> |
paulb@126 | 187 | <xsl:when test="@template:attribute"> |
paulb@126 | 188 | <axsl:choose> |
paulb@126 | 189 | <axsl:when test="@{@template:attribute}"> |
paulb@126 | 190 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 191 | <axsl:variable name="this-value" select="@{@template:attribute}"/> |
paulb@126 | 192 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 193 | </axsl:when> |
paulb@126 | 194 | <axsl:otherwise> |
paulb@126 | 195 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 196 | <axsl:variable name="this-value"></axsl:variable> |
paulb@126 | 197 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 198 | </axsl:otherwise> |
paulb@126 | 199 | </axsl:choose> |
paulb@126 | 200 | </xsl:when> |
paulb@126 | 201 | <xsl:otherwise> |
paulb@126 | 202 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 203 | </xsl:otherwise> |
paulb@126 | 204 | </xsl:choose> |
paulb@1 | 205 | </xsl:template> |
paulb@1 | 206 | |
paulb@1 | 207 | <xsl:template name="special-attributes"> |
paulb@1 | 208 | <xsl:choose> |
paulb@1 | 209 | <xsl:when test="@template:effect = 'replace'"> |
paulb@126 | 210 | <xsl:call-template name="special-value"/> |
paulb@1 | 211 | </xsl:when> |
paulb@1 | 212 | <xsl:otherwise> |
paulb@1 | 213 | <xsl:copy> |
paulb@1 | 214 | <xsl:apply-templates select="@*"/> |
paulb@432 | 215 | <xsl:call-template name="expression-attributes"/> |
paulb@1 | 216 | </xsl:copy> |
paulb@1 | 217 | </xsl:otherwise> |
paulb@1 | 218 | </xsl:choose> |
paulb@1 | 219 | </xsl:template> |
paulb@1 | 220 | |
paulb@432 | 221 | <xsl:template name="expression-attributes"> |
paulb@432 | 222 | <xsl:if test="@template:expr and @template:expr-attr"> |
paulb@432 | 223 | <axsl:if test="{@template:expr}"> |
paulb@432 | 224 | <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute> |
paulb@432 | 225 | </axsl:if> |
paulb@432 | 226 | </xsl:if> |
paulb@432 | 227 | <xsl:call-template name="special-value"/> |
paulb@432 | 228 | </xsl:template> |
paulb@432 | 229 | |
paulb@1 | 230 | <xsl:template name="special-value"> |
paulb@1 | 231 | <xsl:choose> |
paulb@525 | 232 | <!-- Insert the translated value. --> |
paulb@525 | 233 | <xsl:when test="@template:i18n"> |
paulb@525 | 234 | <xsl:call-template name="translated-value"/> |
paulb@525 | 235 | </xsl:when> |
paulb@432 | 236 | <!-- Insert the stated value. --> |
paulb@1 | 237 | <xsl:when test="@template:value"> |
paulb@1 | 238 | <axsl:value-of select="{@template:value}"/> |
paulb@1 | 239 | </xsl:when> |
paulb@477 | 240 | <!-- Copy the stated expression. --> |
paulb@477 | 241 | <xsl:when test="@template:copy"> |
paulb@477 | 242 | <axsl:copy-of select="{@template:copy}"/> |
paulb@477 | 243 | </xsl:when> |
paulb@432 | 244 | <!-- Just process the descendants. --> |
paulb@1 | 245 | <xsl:otherwise> |
paulb@1 | 246 | <xsl:apply-templates select="node()"/> |
paulb@1 | 247 | </xsl:otherwise> |
paulb@1 | 248 | </xsl:choose> |
paulb@1 | 249 | </xsl:template> |
paulb@1 | 250 | |
paulb@1 | 251 | |
paulb@1 | 252 | |
paulb@432 | 253 | <!-- Match internationalisation attributes. --> |
paulb@432 | 254 | |
paulb@432 | 255 | <xsl:template match="*[not(@template:if) and not(@template:element) and not(@template:attribute) and not(@template:value) and not(@template:expr) and @template:i18n]"> |
paulb@432 | 256 | <xsl:copy> |
paulb@435 | 257 | <xsl:apply-templates select="@*"/> |
paulb@435 | 258 | <xsl:call-template name="translated-value"/> |
paulb@432 | 259 | </xsl:copy> |
paulb@432 | 260 | </xsl:template> |
paulb@432 | 261 | |
paulb@435 | 262 | <xsl:template name="translated-value"> |
paulb@432 | 263 | <xsl:choose> |
paulb@435 | 264 | <!-- Look for a translation of the contents. --> |
paulb@435 | 265 | <xsl:when test="@template:i18n = '-'"> |
paulb@435 | 266 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 267 | <axsl:variable name="translation" |
paulb@435 | 268 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{text()}']/text()"/> |
paulb@564 | 269 | <axsl:variable name="translation-default" |
paulb@564 | 270 | select="$translations/translations/locale[1]/translation[@value='{text()}']/text()"/> |
paulb@435 | 271 | <xsl:call-template name="insert-translated-value"/> |
paulb@432 | 272 | </xsl:when> |
paulb@525 | 273 | <!-- Look for a translation based on the expression. --> |
paulb@525 | 274 | <xsl:when test="starts-with(@template:i18n, '{') and substring(@template:i18n, string-length(@template:i18n), 1) = '}'"> |
paulb@525 | 275 | <!-- Select the expression. --> |
paulb@525 | 276 | <axsl:variable name="i18n-expr" select="{substring(@template:i18n, 2, string-length(@template:i18n) - 2)}"/> |
paulb@525 | 277 | <!-- Translate according to the expression. --> |
paulb@525 | 278 | <axsl:variable name="translation" |
paulb@525 | 279 | select="$translations/translations/locale[code/@value=$locale]/translation[@value=$i18n-expr]/text()"/> |
paulb@564 | 280 | <axsl:variable name="translation-default" |
paulb@564 | 281 | select="$translations/translations/locale[1]/translation[@value=$i18n-expr]/text()"/> |
paulb@525 | 282 | <xsl:call-template name="insert-translated-value"> |
paulb@525 | 283 | <xsl:with-param name="default">$i18n-expr</xsl:with-param> |
paulb@525 | 284 | </xsl:call-template> |
paulb@525 | 285 | </xsl:when> |
paulb@435 | 286 | <!-- Look for a named translation. --> |
paulb@435 | 287 | <xsl:otherwise> |
paulb@435 | 288 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 289 | <axsl:variable name="translation" |
paulb@435 | 290 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{@template:i18n}']/text()"/> |
paulb@564 | 291 | <axsl:variable name="translation-default" |
paulb@564 | 292 | select="$translations/translations/locale[1]/translation[@value='{@template:i18n}']/text()"/> |
paulb@435 | 293 | <xsl:call-template name="insert-translated-value"/> |
paulb@435 | 294 | </xsl:otherwise> |
paulb@432 | 295 | </xsl:choose> |
paulb@432 | 296 | </xsl:template> |
paulb@432 | 297 | |
paulb@435 | 298 | <xsl:template name="insert-translated-value"> |
paulb@525 | 299 | <xsl:param name="default"/> |
paulb@435 | 300 | <axsl:choose> |
paulb@435 | 301 | <!-- Insert the translated value. --> |
paulb@435 | 302 | <axsl:when test="$translation"> |
paulb@435 | 303 | <axsl:value-of select="$translation"/> |
paulb@435 | 304 | </axsl:when> |
paulb@564 | 305 | <!-- Or insert the default translated value. --> |
paulb@564 | 306 | <axsl:when test="$translation-default"> |
paulb@564 | 307 | <axsl:value-of select="$translation-default"/> |
paulb@564 | 308 | </axsl:when> |
paulb@435 | 309 | <!-- Otherwise, just process the descendants. --> |
paulb@435 | 310 | <axsl:otherwise> |
paulb@525 | 311 | <xsl:choose> |
paulb@525 | 312 | <xsl:when test="$default"> |
paulb@525 | 313 | <axsl:value-of select="{$default}"/> |
paulb@525 | 314 | </xsl:when> |
paulb@525 | 315 | <xsl:otherwise> |
paulb@525 | 316 | <xsl:apply-templates select="node()"/> |
paulb@525 | 317 | </xsl:otherwise> |
paulb@525 | 318 | </xsl:choose> |
paulb@435 | 319 | </axsl:otherwise> |
paulb@435 | 320 | </axsl:choose> |
paulb@432 | 321 | </xsl:template> |
paulb@432 | 322 | |
paulb@432 | 323 | |
paulb@432 | 324 | |
paulb@198 | 325 | <!-- Remove template attributes. --> |
paulb@1 | 326 | |
paulb@481 | 327 | <xsl:template match="@template:element|@template:init|@template:attribute|@template:value|@template:expr|@template:expr-attr|@template:effect|@template:if|@template:i18n|@template:copy"> |
paulb@1 | 328 | </xsl:template> |
paulb@1 | 329 | |
paulb@1 | 330 | |
paulb@1 | 331 | |
paulb@505 | 332 | <!-- Remove special attributes for preserving namespace prefixes used in expressions. --> |
paulb@505 | 333 | |
paulb@505 | 334 | <xsl:template match="@*[local-name() = 'expr-prefix']"> |
paulb@505 | 335 | </xsl:template> |
paulb@505 | 336 | |
paulb@505 | 337 | |
paulb@505 | 338 | |
paulb@1 | 339 | <!-- Replicate unknown elements. --> |
paulb@1 | 340 | |
paulb@1 | 341 | <xsl:template match="@*|node()"> |
paulb@1 | 342 | <xsl:copy> |
paulb@1 | 343 | <xsl:apply-templates select="@*|node()"/> |
paulb@1 | 344 | </xsl:copy> |
paulb@1 | 345 | </xsl:template> |
paulb@1 | 346 | |
paulb@1 | 347 | </xsl:stylesheet> |