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@40 | 43 | <axsl:output indent="yes"/> |
paulb@40 | 44 | |
paulb@432 | 45 | <!-- Include internationalisation (i18n) support if appropriate. --> |
paulb@435 | 46 | <axsl:param name="translations"/> |
paulb@435 | 47 | <axsl:param name="locale"/> |
paulb@432 | 48 | |
paulb@481 | 49 | <!-- Include fragment support if appropriate. --> |
paulb@481 | 50 | <axsl:param name="element-path"/> |
paulb@481 | 51 | |
paulb@1 | 52 | <axsl:template match="/"> |
paulb@481 | 53 | <xsl:choose> |
paulb@481 | 54 | <!-- Fragment production. --> |
paulb@481 | 55 | <xsl:when test="$element-id != ''"> |
paulb@481 | 56 | <axsl:for-each select="dyn:evaluate($element-path)"> |
paulb@481 | 57 | <xsl:apply-templates select="@*|node()"/> |
paulb@481 | 58 | </axsl:for-each> |
paulb@481 | 59 | </xsl:when> |
paulb@481 | 60 | <!-- Whole template production. --> |
paulb@481 | 61 | <xsl:otherwise> |
paulb@481 | 62 | <xsl:apply-templates select="@*|node()"/> |
paulb@481 | 63 | </xsl:otherwise> |
paulb@481 | 64 | </xsl:choose> |
paulb@481 | 65 | </axsl:template> |
paulb@1 | 66 | |
paulb@481 | 67 | <!-- Produce the rules for each element. --> |
paulb@481 | 68 | |
paulb@481 | 69 | <xsl:apply-templates select="//*[@template:element]"> |
paulb@481 | 70 | <xsl:with-param name="top-level">true</xsl:with-param> |
paulb@481 | 71 | </xsl:apply-templates> |
paulb@481 | 72 | |
paulb@481 | 73 | </axsl:stylesheet> |
paulb@481 | 74 | </xsl:template> |
paulb@481 | 75 | |
paulb@481 | 76 | |
paulb@481 | 77 | |
paulb@481 | 78 | <!-- Match elements referencing elements. --> |
paulb@481 | 79 | |
paulb@481 | 80 | <xsl:template match="*[@template:element]" priority="1"> |
paulb@481 | 81 | <xsl:param name="top-level">false</xsl:param> |
paulb@481 | 82 | <xsl:call-template name="enter-element"> |
paulb@481 | 83 | <xsl:with-param name="top-level" select="$top-level"/> |
paulb@481 | 84 | </xsl:call-template> |
paulb@481 | 85 | </xsl:template> |
paulb@1 | 86 | |
paulb@481 | 87 | <xsl:template name="enter-element"> |
paulb@481 | 88 | <xsl:param name="top-level"/> |
paulb@481 | 89 | <xsl:choose> |
paulb@481 | 90 | <!-- Produce templates where this is a top-level definition. --> |
paulb@481 | 91 | <xsl:when test="$top-level = 'true'"> |
paulb@481 | 92 | <xsl:call-template name="element-template"> |
paulb@481 | 93 | <xsl:with-param name="element-names" select="@template:element"/> |
paulb@481 | 94 | </xsl:call-template> |
paulb@481 | 95 | </xsl:when> |
paulb@481 | 96 | <!-- Produce references to elements where this is within a template. --> |
paulb@481 | 97 | <xsl:otherwise> |
paulb@481 | 98 | <xsl:variable name="first-name" select="str:split(@template:element, ',')[1]"/> |
paulb@481 | 99 | <!-- Check to see if this is a recursive reference. --> |
paulb@481 | 100 | <xsl:variable name="recursive-element" select="ancestor::*[$first-name = str:split(@template:element, ',')[1]]"/> |
paulb@481 | 101 | <xsl:choose> |
paulb@481 | 102 | <!-- Generate a reference to the previous element definition. --> |
paulb@481 | 103 | <xsl:when test="$recursive-element"> |
paulb@481 | 104 | <axsl:apply-templates select="{$first-name}" mode="{generate-id($recursive-element)}"/> |
paulb@481 | 105 | </xsl:when> |
paulb@481 | 106 | <!-- Generate a reference to this element definition. --> |
paulb@481 | 107 | <xsl:otherwise> |
paulb@481 | 108 | <axsl:apply-templates select="{$first-name}" mode="{generate-id(.)}"/> |
paulb@481 | 109 | </xsl:otherwise> |
paulb@481 | 110 | </xsl:choose> |
paulb@481 | 111 | </xsl:otherwise> |
paulb@481 | 112 | </xsl:choose> |
paulb@481 | 113 | </xsl:template> |
paulb@1 | 114 | |
paulb@481 | 115 | <xsl:template name="element-template"> |
paulb@481 | 116 | <xsl:param name="element-names"/> |
paulb@481 | 117 | <xsl:variable name="this-name" select="substring-before($element-names, ',')"/> |
paulb@481 | 118 | <xsl:variable name="next-names" select="substring-after($element-names, ',')"/> |
paulb@481 | 119 | <xsl:variable name="next-name" select="str:split($next-names, ',')[1]"/> |
paulb@481 | 120 | <xsl:choose> |
paulb@481 | 121 | <!-- Non-last part of a list of element names. --> |
paulb@481 | 122 | <!-- Produce a template referencing another template. --> |
paulb@481 | 123 | <xsl:when test="$this-name != ''"> |
paulb@481 | 124 | <!-- Produce a template with a mode. --> |
paulb@481 | 125 | <axsl:template match="{$this-name}" mode="{generate-id(.)}"> |
paulb@481 | 126 | <axsl:apply-templates select="{$next-name}" mode="{generate-id(.)}"/> |
paulb@481 | 127 | </axsl:template> |
paulb@481 | 128 | <!-- Produce the other elements' templates... --> |
paulb@481 | 129 | <xsl:call-template name="element-template"> |
paulb@481 | 130 | <xsl:with-param name="element-names" select="$next-names"/> |
paulb@481 | 131 | </xsl:call-template> |
paulb@481 | 132 | </xsl:when> |
paulb@481 | 133 | <!-- Last part of a list of element names. --> |
paulb@481 | 134 | <!-- Produce a template with content. --> |
paulb@481 | 135 | <xsl:otherwise> |
paulb@481 | 136 | <!-- Produce a template with a mode. --> |
paulb@481 | 137 | <axsl:template match="{$element-names}" mode="{generate-id(.)}"> |
paulb@481 | 138 | <xsl:call-template name="enter-attribute"/> |
paulb@481 | 139 | </axsl:template> |
paulb@481 | 140 | </xsl:otherwise> |
paulb@481 | 141 | </xsl:choose> |
paulb@1 | 142 | </xsl:template> |
paulb@1 | 143 | |
paulb@1 | 144 | |
paulb@1 | 145 | |
paulb@198 | 146 | <!-- Match special conditional expression attributes. --> |
paulb@198 | 147 | |
paulb@481 | 148 | <xsl:template match="*[@template:if]" priority="2"> |
paulb@481 | 149 | <xsl:param name="top-level">false</xsl:param> |
paulb@1 | 150 | <xsl:choose> |
paulb@481 | 151 | <!-- Since this rule may be invoked at the top level, ignore conditions. --> |
paulb@481 | 152 | <xsl:when test="$top-level = 'true'"> |
paulb@481 | 153 | <xsl:call-template name="enter-element"> |
paulb@481 | 154 | <xsl:with-param name="top-level" select="$top-level"/> |
paulb@1 | 155 | </xsl:call-template> |
paulb@1 | 156 | </xsl:when> |
paulb@481 | 157 | <!-- As part of a template, generate the condition. --> |
paulb@1 | 158 | <xsl:otherwise> |
paulb@481 | 159 | <axsl:if test="{@template:if}"> |
paulb@481 | 160 | <xsl:choose> |
paulb@481 | 161 | <xsl:when test="@template:element"> |
paulb@481 | 162 | <xsl:call-template name="enter-element"/> |
paulb@481 | 163 | </xsl:when> |
paulb@481 | 164 | <xsl:otherwise> |
paulb@481 | 165 | <xsl:call-template name="enter-attribute"/> |
paulb@481 | 166 | </xsl:otherwise> |
paulb@481 | 167 | </xsl:choose> |
paulb@481 | 168 | </axsl:if> |
paulb@1 | 169 | </xsl:otherwise> |
paulb@1 | 170 | </xsl:choose> |
paulb@1 | 171 | </xsl:template> |
paulb@1 | 172 | |
paulb@1 | 173 | |
paulb@1 | 174 | |
paulb@1 | 175 | <!-- Match special expression attributes. --> |
paulb@1 | 176 | |
paulb@481 | 177 | <xsl:template match="*[@template:attribute or @template:value or @template:expr or @template:copy]"> |
paulb@126 | 178 | <xsl:call-template name="enter-attribute"/> |
paulb@126 | 179 | </xsl:template> |
paulb@126 | 180 | |
paulb@126 | 181 | <xsl:template name="enter-attribute"> |
paulb@126 | 182 | <xsl:choose> |
paulb@126 | 183 | <xsl:when test="@template:attribute"> |
paulb@126 | 184 | <axsl:choose> |
paulb@126 | 185 | <axsl:when test="@{@template:attribute}"> |
paulb@126 | 186 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 187 | <axsl:variable name="this-value" select="@{@template:attribute}"/> |
paulb@126 | 188 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 189 | </axsl:when> |
paulb@126 | 190 | <axsl:otherwise> |
paulb@126 | 191 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 192 | <axsl:variable name="this-value"></axsl:variable> |
paulb@126 | 193 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 194 | </axsl:otherwise> |
paulb@126 | 195 | </axsl:choose> |
paulb@126 | 196 | </xsl:when> |
paulb@126 | 197 | <xsl:otherwise> |
paulb@126 | 198 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 199 | </xsl:otherwise> |
paulb@126 | 200 | </xsl:choose> |
paulb@1 | 201 | </xsl:template> |
paulb@1 | 202 | |
paulb@1 | 203 | <xsl:template name="special-attributes"> |
paulb@1 | 204 | <xsl:choose> |
paulb@1 | 205 | <xsl:when test="@template:effect = 'replace'"> |
paulb@126 | 206 | <xsl:call-template name="special-value"/> |
paulb@1 | 207 | </xsl:when> |
paulb@1 | 208 | <xsl:otherwise> |
paulb@1 | 209 | <xsl:copy> |
paulb@1 | 210 | <xsl:apply-templates select="@*"/> |
paulb@432 | 211 | <xsl:call-template name="expression-attributes"/> |
paulb@1 | 212 | </xsl:copy> |
paulb@1 | 213 | </xsl:otherwise> |
paulb@1 | 214 | </xsl:choose> |
paulb@1 | 215 | </xsl:template> |
paulb@1 | 216 | |
paulb@432 | 217 | <xsl:template name="expression-attributes"> |
paulb@432 | 218 | <xsl:if test="@template:expr and @template:expr-attr"> |
paulb@432 | 219 | <axsl:if test="{@template:expr}"> |
paulb@432 | 220 | <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute> |
paulb@432 | 221 | </axsl:if> |
paulb@432 | 222 | </xsl:if> |
paulb@432 | 223 | <xsl:call-template name="special-value"/> |
paulb@432 | 224 | </xsl:template> |
paulb@432 | 225 | |
paulb@1 | 226 | <xsl:template name="special-value"> |
paulb@1 | 227 | <xsl:choose> |
paulb@432 | 228 | <!-- Insert the stated value. --> |
paulb@1 | 229 | <xsl:when test="@template:value"> |
paulb@1 | 230 | <axsl:value-of select="{@template:value}"/> |
paulb@1 | 231 | </xsl:when> |
paulb@432 | 232 | <!-- Insert the translated value. --> |
paulb@435 | 233 | <xsl:when test="@template:i18n"> |
paulb@435 | 234 | <xsl:call-template name="translated-value"/> |
paulb@432 | 235 | </xsl:when> |
paulb@477 | 236 | <!-- Copy the stated expression. --> |
paulb@477 | 237 | <xsl:when test="@template:copy"> |
paulb@477 | 238 | <axsl:copy-of select="{@template:copy}"/> |
paulb@477 | 239 | </xsl:when> |
paulb@432 | 240 | <!-- Just process the descendants. --> |
paulb@1 | 241 | <xsl:otherwise> |
paulb@1 | 242 | <xsl:apply-templates select="node()"/> |
paulb@1 | 243 | </xsl:otherwise> |
paulb@1 | 244 | </xsl:choose> |
paulb@1 | 245 | </xsl:template> |
paulb@1 | 246 | |
paulb@1 | 247 | |
paulb@1 | 248 | |
paulb@432 | 249 | <!-- Match internationalisation attributes. --> |
paulb@432 | 250 | |
paulb@432 | 251 | <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 | 252 | <xsl:copy> |
paulb@435 | 253 | <xsl:apply-templates select="@*"/> |
paulb@435 | 254 | <xsl:call-template name="translated-value"/> |
paulb@432 | 255 | </xsl:copy> |
paulb@432 | 256 | </xsl:template> |
paulb@432 | 257 | |
paulb@435 | 258 | <xsl:template name="translated-value"> |
paulb@432 | 259 | <xsl:choose> |
paulb@435 | 260 | <!-- Look for a translation of the contents. --> |
paulb@435 | 261 | <xsl:when test="@template:i18n = '-'"> |
paulb@435 | 262 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 263 | <axsl:variable name="translation" |
paulb@435 | 264 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{text()}']/text()"/> |
paulb@435 | 265 | <xsl:call-template name="insert-translated-value"/> |
paulb@432 | 266 | </xsl:when> |
paulb@435 | 267 | <!-- Look for a named translation. --> |
paulb@435 | 268 | <xsl:otherwise> |
paulb@435 | 269 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 270 | <axsl:variable name="translation" |
paulb@435 | 271 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{@template:i18n}']/text()"/> |
paulb@435 | 272 | <xsl:call-template name="insert-translated-value"/> |
paulb@435 | 273 | </xsl:otherwise> |
paulb@432 | 274 | </xsl:choose> |
paulb@432 | 275 | </xsl:template> |
paulb@432 | 276 | |
paulb@435 | 277 | <xsl:template name="insert-translated-value"> |
paulb@435 | 278 | <axsl:choose> |
paulb@435 | 279 | <!-- Insert the translated value. --> |
paulb@435 | 280 | <axsl:when test="$translation"> |
paulb@435 | 281 | <axsl:value-of select="$translation"/> |
paulb@435 | 282 | </axsl:when> |
paulb@435 | 283 | <!-- Otherwise, just process the descendants. --> |
paulb@435 | 284 | <axsl:otherwise> |
paulb@435 | 285 | <xsl:apply-templates select="node()"/> |
paulb@435 | 286 | </axsl:otherwise> |
paulb@435 | 287 | </axsl:choose> |
paulb@432 | 288 | </xsl:template> |
paulb@432 | 289 | |
paulb@432 | 290 | |
paulb@432 | 291 | |
paulb@198 | 292 | <!-- Remove template attributes. --> |
paulb@1 | 293 | |
paulb@481 | 294 | <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 | 295 | </xsl:template> |
paulb@1 | 296 | |
paulb@1 | 297 | |
paulb@1 | 298 | |
paulb@1 | 299 | <!-- Replicate unknown elements. --> |
paulb@1 | 300 | |
paulb@1 | 301 | <xsl:template match="@*|node()"> |
paulb@1 | 302 | <xsl:copy> |
paulb@1 | 303 | <xsl:apply-templates select="@*|node()"/> |
paulb@1 | 304 | </xsl:copy> |
paulb@1 | 305 | </xsl:template> |
paulb@1 | 306 | |
paulb@1 | 307 | </xsl:stylesheet> |