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@1 | 26 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> |
paulb@1 | 27 | |
paulb@1 | 28 | <xsl:output indent="yes"/> |
paulb@1 | 29 | <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> |
paulb@1 | 30 | |
paulb@40 | 31 | |
paulb@40 | 32 | |
paulb@1 | 33 | <!-- Match the document itself. --> |
paulb@1 | 34 | |
paulb@1 | 35 | <xsl:template match="/"> |
paulb@1 | 36 | <axsl:stylesheet version="1.0"> |
paulb@40 | 37 | |
paulb@40 | 38 | <axsl:output indent="yes"/> |
paulb@40 | 39 | <!-- NOTE: Hard-coded doctypes to hopefully satisfy JavaScript code. --> |
paulb@40 | 40 | <!-- doctype-public="-//W3C//DTD XHTML 1.1//EN" |
paulb@40 | 41 | doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" --> |
paulb@40 | 42 | |
paulb@432 | 43 | <!-- Include internationalisation (i18n) support if appropriate. --> |
paulb@432 | 44 | <xsl:if test="//*[@template:i18n]"> |
paulb@432 | 45 | <axsl:param name="translations"/> |
paulb@432 | 46 | <axsl:param name="locale"/> |
paulb@432 | 47 | </xsl:if> |
paulb@432 | 48 | |
paulb@1 | 49 | <axsl:template match="/"> |
paulb@1 | 50 | |
paulb@1 | 51 | <!-- Include the remaining attributes. --> |
paulb@1 | 52 | <xsl:apply-templates select="@*"/> |
paulb@1 | 53 | |
paulb@1 | 54 | <!-- Process the elements. --> |
paulb@1 | 55 | <xsl:apply-templates select="*"/> |
paulb@1 | 56 | |
paulb@1 | 57 | </axsl:template> |
paulb@1 | 58 | </axsl:stylesheet> |
paulb@1 | 59 | </xsl:template> |
paulb@1 | 60 | |
paulb@1 | 61 | |
paulb@1 | 62 | |
paulb@198 | 63 | <!-- Match special conditional expression attributes. --> |
paulb@198 | 64 | |
paulb@198 | 65 | <xsl:template match="*[@template:if]"> |
paulb@198 | 66 | <axsl:if test="{@template:if}"> |
paulb@198 | 67 | <xsl:choose> |
paulb@198 | 68 | <xsl:when test="@template:element"> |
paulb@198 | 69 | <xsl:call-template name="enter-element"> |
paulb@198 | 70 | <xsl:with-param name="other-elements" select="@template:element"/> |
paulb@198 | 71 | </xsl:call-template> |
paulb@198 | 72 | </xsl:when> |
paulb@198 | 73 | <xsl:otherwise> |
paulb@198 | 74 | <xsl:call-template name="enter-attribute"/> |
paulb@198 | 75 | </xsl:otherwise> |
paulb@198 | 76 | </xsl:choose> |
paulb@198 | 77 | </axsl:if> |
paulb@198 | 78 | </xsl:template> |
paulb@198 | 79 | |
paulb@198 | 80 | |
paulb@198 | 81 | |
paulb@1 | 82 | <!-- Match element references. --> |
paulb@1 | 83 | |
paulb@198 | 84 | <xsl:template match="*[not(@template:if) and @template:element]"> |
paulb@1 | 85 | <xsl:call-template name="enter-element"> |
paulb@1 | 86 | <xsl:with-param name="other-elements" select="@template:element"/> |
paulb@1 | 87 | </xsl:call-template> |
paulb@1 | 88 | </xsl:template> |
paulb@1 | 89 | |
paulb@1 | 90 | <xsl:template name="enter-element"> |
paulb@1 | 91 | <xsl:param name="other-elements"/> |
paulb@1 | 92 | <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> |
paulb@1 | 93 | <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> |
paulb@1 | 94 | <xsl:choose> |
paulb@1 | 95 | <xsl:when test="$first-element = ''"> |
paulb@1 | 96 | <xsl:call-template name="next-element"> |
paulb@1 | 97 | <xsl:with-param name="first-element" select="$other-elements"/> |
paulb@1 | 98 | </xsl:call-template> |
paulb@1 | 99 | </xsl:when> |
paulb@1 | 100 | <xsl:otherwise> |
paulb@1 | 101 | <xsl:call-template name="next-element"> |
paulb@1 | 102 | <xsl:with-param name="first-element" select="$first-element"/> |
paulb@1 | 103 | <xsl:with-param name="remaining-elements" select="$remaining-elements"/> |
paulb@1 | 104 | </xsl:call-template> |
paulb@1 | 105 | </xsl:otherwise> |
paulb@1 | 106 | </xsl:choose> |
paulb@1 | 107 | </xsl:template> |
paulb@1 | 108 | |
paulb@1 | 109 | <xsl:template name="next-element"> |
paulb@1 | 110 | <xsl:param name="first-element"/> |
paulb@1 | 111 | <xsl:param name="remaining-elements"/> |
paulb@1 | 112 | <axsl:for-each select="{$first-element}"> |
paulb@1 | 113 | <xsl:choose> |
paulb@1 | 114 | <xsl:when test="$remaining-elements = ''"> |
paulb@126 | 115 | <xsl:call-template name="enter-attribute"/> |
paulb@1 | 116 | </xsl:when> |
paulb@1 | 117 | <xsl:otherwise> |
paulb@1 | 118 | <xsl:call-template name="enter-element"> |
paulb@1 | 119 | <xsl:with-param name="other-elements" select="$remaining-elements"/> |
paulb@1 | 120 | </xsl:call-template> |
paulb@1 | 121 | </xsl:otherwise> |
paulb@1 | 122 | </xsl:choose> |
paulb@1 | 123 | </axsl:for-each> |
paulb@1 | 124 | </xsl:template> |
paulb@1 | 125 | |
paulb@1 | 126 | |
paulb@1 | 127 | |
paulb@1 | 128 | <!-- Match special expression attributes. --> |
paulb@1 | 129 | |
paulb@198 | 130 | <xsl:template match="*[not(@template:if) and not(@template:element) and (@template:attribute or @template:value or @template:expr)]"> |
paulb@126 | 131 | <xsl:call-template name="enter-attribute"/> |
paulb@126 | 132 | </xsl:template> |
paulb@126 | 133 | |
paulb@126 | 134 | <xsl:template name="enter-attribute"> |
paulb@126 | 135 | <xsl:choose> |
paulb@126 | 136 | <xsl:when test="@template:attribute"> |
paulb@126 | 137 | <axsl:choose> |
paulb@126 | 138 | <axsl:when test="@{@template:attribute}"> |
paulb@126 | 139 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 140 | <axsl:variable name="this-value" select="@{@template:attribute}"/> |
paulb@126 | 141 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 142 | </axsl:when> |
paulb@126 | 143 | <axsl:otherwise> |
paulb@126 | 144 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 145 | <axsl:variable name="this-value"></axsl:variable> |
paulb@126 | 146 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 147 | </axsl:otherwise> |
paulb@126 | 148 | </axsl:choose> |
paulb@126 | 149 | </xsl:when> |
paulb@126 | 150 | <xsl:otherwise> |
paulb@126 | 151 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 152 | </xsl:otherwise> |
paulb@126 | 153 | </xsl:choose> |
paulb@1 | 154 | </xsl:template> |
paulb@1 | 155 | |
paulb@1 | 156 | <xsl:template name="special-attributes"> |
paulb@1 | 157 | <xsl:choose> |
paulb@1 | 158 | <xsl:when test="@template:effect = 'replace'"> |
paulb@126 | 159 | <xsl:call-template name="special-value"/> |
paulb@1 | 160 | </xsl:when> |
paulb@432 | 161 | <xsl:when test="@template:i18n != '' and @template:i18n != '-'"> |
paulb@432 | 162 | <xsl:copy> |
paulb@432 | 163 | <xsl:apply-templates select="@*"/> |
paulb@432 | 164 | <xsl:call-template name="translated-values"> |
paulb@432 | 165 | <xsl:with-param name="values" select="@template:i18n"/> |
paulb@432 | 166 | </xsl:call-template> |
paulb@432 | 167 | <xsl:call-template name="expression-attributes"/> |
paulb@432 | 168 | </xsl:copy> |
paulb@432 | 169 | </xsl:when> |
paulb@1 | 170 | <xsl:otherwise> |
paulb@1 | 171 | <xsl:copy> |
paulb@1 | 172 | <xsl:apply-templates select="@*"/> |
paulb@432 | 173 | <xsl:call-template name="expression-attributes"/> |
paulb@1 | 174 | </xsl:copy> |
paulb@1 | 175 | </xsl:otherwise> |
paulb@1 | 176 | </xsl:choose> |
paulb@1 | 177 | </xsl:template> |
paulb@1 | 178 | |
paulb@432 | 179 | <xsl:template name="expression-attributes"> |
paulb@432 | 180 | <xsl:if test="@template:expr and @template:expr-attr"> |
paulb@432 | 181 | <axsl:if test="{@template:expr}"> |
paulb@432 | 182 | <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute> |
paulb@432 | 183 | </axsl:if> |
paulb@432 | 184 | </xsl:if> |
paulb@432 | 185 | <xsl:call-template name="special-value"/> |
paulb@432 | 186 | </xsl:template> |
paulb@432 | 187 | |
paulb@1 | 188 | <xsl:template name="special-value"> |
paulb@1 | 189 | <xsl:choose> |
paulb@432 | 190 | <!-- Insert the stated value. --> |
paulb@1 | 191 | <xsl:when test="@template:value"> |
paulb@1 | 192 | <axsl:value-of select="{@template:value}"/> |
paulb@1 | 193 | </xsl:when> |
paulb@432 | 194 | <!-- Insert the translated value. --> |
paulb@432 | 195 | <xsl:when test="@template:i18n = '-'"> |
paulb@432 | 196 | <xsl:call-template name="translated-value"> |
paulb@432 | 197 | <xsl:with-param name="mode">contents</xsl:with-param> |
paulb@432 | 198 | </xsl:call-template> |
paulb@432 | 199 | </xsl:when> |
paulb@432 | 200 | <!-- Just process the descendants. --> |
paulb@1 | 201 | <xsl:otherwise> |
paulb@1 | 202 | <xsl:apply-templates select="node()"/> |
paulb@1 | 203 | </xsl:otherwise> |
paulb@1 | 204 | </xsl:choose> |
paulb@1 | 205 | </xsl:template> |
paulb@1 | 206 | |
paulb@1 | 207 | |
paulb@1 | 208 | |
paulb@432 | 209 | <!-- Match internationalisation attributes. --> |
paulb@432 | 210 | |
paulb@432 | 211 | <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 | 212 | <xsl:copy> |
paulb@432 | 213 | <xsl:choose> |
paulb@432 | 214 | <xsl:when test="@template:i18n = '-'"> |
paulb@432 | 215 | <xsl:apply-templates select="@*"/> |
paulb@432 | 216 | <xsl:call-template name="translated-value"> |
paulb@432 | 217 | <xsl:with-param name="mode">contents</xsl:with-param> |
paulb@432 | 218 | </xsl:call-template> |
paulb@432 | 219 | </xsl:when> |
paulb@432 | 220 | <xsl:otherwise> |
paulb@432 | 221 | <xsl:apply-templates select="@*"/> |
paulb@432 | 222 | <xsl:call-template name="translated-values"> |
paulb@432 | 223 | <xsl:with-param name="values" select="@template:i18n"/> |
paulb@432 | 224 | </xsl:call-template> |
paulb@432 | 225 | </xsl:otherwise> |
paulb@432 | 226 | </xsl:choose> |
paulb@432 | 227 | </xsl:copy> |
paulb@432 | 228 | </xsl:template> |
paulb@432 | 229 | |
paulb@432 | 230 | <xsl:template name="translated-values"> |
paulb@432 | 231 | <xsl:param name="values"/> |
paulb@432 | 232 | <xsl:param name="this-value" select="substring-before($values, ',')"/> |
paulb@432 | 233 | <xsl:param name="next-values" select="substring-after($values, ',')"/> |
paulb@432 | 234 | <xsl:choose> |
paulb@432 | 235 | <xsl:when test="$this-value != ''"> |
paulb@432 | 236 | <xsl:call-template name="translated-value"> |
paulb@432 | 237 | <xsl:with-param name="mode">attribute</xsl:with-param> |
paulb@432 | 238 | <xsl:with-param name="attribute-name" select="$this-value"/> |
paulb@432 | 239 | </xsl:call-template> |
paulb@432 | 240 | <xsl:call-template name="translated-values"> |
paulb@432 | 241 | <xsl:with-param name="values" select="$next-values"/> |
paulb@432 | 242 | </xsl:call-template> |
paulb@432 | 243 | </xsl:when> |
paulb@432 | 244 | <xsl:when test="$values != ''"> |
paulb@432 | 245 | <xsl:call-template name="translated-value"> |
paulb@432 | 246 | <xsl:with-param name="mode">attribute</xsl:with-param> |
paulb@432 | 247 | <xsl:with-param name="attribute-name" select="$values"/> |
paulb@432 | 248 | </xsl:call-template> |
paulb@432 | 249 | </xsl:when> |
paulb@432 | 250 | <xsl:otherwise/> |
paulb@432 | 251 | </xsl:choose> |
paulb@432 | 252 | </xsl:template> |
paulb@432 | 253 | |
paulb@432 | 254 | <xsl:template name="translated-value"> |
paulb@432 | 255 | <xsl:param name="mode"/> |
paulb@432 | 256 | <xsl:param name="attribute-name"/> |
paulb@432 | 257 | <xsl:choose> |
paulb@432 | 258 | <!-- Look for a translation for the contents. --> |
paulb@432 | 259 | <xsl:when test="$mode = 'contents'"> |
paulb@432 | 260 | <!-- NOTE: Quoting not done completely. --> |
paulb@432 | 261 | <!-- NOTE: Guard against scoping issues by generating a special name. --> |
paulb@432 | 262 | <axsl:variable name="translation-{$attribute-name}" |
paulb@432 | 263 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{text()}']/text()"/> |
paulb@432 | 264 | <axsl:choose> |
paulb@432 | 265 | <!-- Insert the translated value. --> |
paulb@432 | 266 | <axsl:when test="$translation-{$attribute-name}"> |
paulb@432 | 267 | <axsl:value-of select="$translation-{$attribute-name}"/> |
paulb@432 | 268 | </axsl:when> |
paulb@432 | 269 | <!-- Otherwise, just process the descendants. --> |
paulb@432 | 270 | <axsl:otherwise> |
paulb@432 | 271 | <xsl:apply-templates select="node()"/> |
paulb@432 | 272 | </axsl:otherwise> |
paulb@432 | 273 | </axsl:choose> |
paulb@432 | 274 | </xsl:when> |
paulb@432 | 275 | <!-- Look for a translation for an attribute. --> |
paulb@432 | 276 | <xsl:otherwise> |
paulb@432 | 277 | <!-- NOTE: Quoting not done completely. --> |
paulb@432 | 278 | <!-- NOTE: Guard against scoping issues by generating a special name. --> |
paulb@432 | 279 | <xsl:variable name="attribute-value" select="@*[local-name() = $attribute-name]"/> |
paulb@432 | 280 | <axsl:variable name="translation-{$attribute-name}" |
paulb@432 | 281 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{$attribute-value}']/text()"/> |
paulb@432 | 282 | <axsl:choose> |
paulb@432 | 283 | <!-- Insert the translated value. --> |
paulb@432 | 284 | <axsl:when test="$translation-{$attribute-name}"> |
paulb@432 | 285 | <axsl:attribute name="{$attribute-name}"><axsl:value-of select="$translation-{$attribute-name}"/></axsl:attribute> |
paulb@432 | 286 | </axsl:when> |
paulb@432 | 287 | <!-- Otherwise, just process the attribute. --> |
paulb@432 | 288 | <axsl:otherwise> |
paulb@432 | 289 | <axsl:attribute name="{$attribute-name}"><xsl:value-of select="$attribute-value"/></axsl:attribute> |
paulb@432 | 290 | </axsl:otherwise> |
paulb@432 | 291 | </axsl:choose> |
paulb@432 | 292 | </xsl:otherwise> |
paulb@432 | 293 | </xsl:choose> |
paulb@432 | 294 | </xsl:template> |
paulb@432 | 295 | |
paulb@432 | 296 | |
paulb@432 | 297 | |
paulb@198 | 298 | <!-- Remove template attributes. --> |
paulb@1 | 299 | |
paulb@432 | 300 | <xsl:template match="@template:element|@template:init|@template:attribute|@template:value|@template:expr|@template:expr-attr|@template:effect|@template:if|@template:i18n"> |
paulb@1 | 301 | </xsl:template> |
paulb@1 | 302 | |
paulb@1 | 303 | |
paulb@1 | 304 | |
paulb@1 | 305 | <!-- Replicate unknown elements. --> |
paulb@1 | 306 | |
paulb@1 | 307 | <xsl:template match="@*|node()"> |
paulb@1 | 308 | <xsl:copy> |
paulb@1 | 309 | <xsl:apply-templates select="@*|node()"/> |
paulb@1 | 310 | </xsl:copy> |
paulb@1 | 311 | </xsl:template> |
paulb@1 | 312 | |
paulb@1 | 313 | </xsl:stylesheet> |