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