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@435 | 44 | <axsl:param name="translations"/> |
paulb@435 | 45 | <axsl:param name="locale"/> |
paulb@432 | 46 | |
paulb@1 | 47 | <axsl:template match="/"> |
paulb@1 | 48 | |
paulb@1 | 49 | <!-- Include the remaining attributes. --> |
paulb@1 | 50 | <xsl:apply-templates select="@*"/> |
paulb@1 | 51 | |
paulb@1 | 52 | <!-- Process the elements. --> |
paulb@1 | 53 | <xsl:apply-templates select="*"/> |
paulb@1 | 54 | |
paulb@1 | 55 | </axsl:template> |
paulb@1 | 56 | </axsl:stylesheet> |
paulb@1 | 57 | </xsl:template> |
paulb@1 | 58 | |
paulb@1 | 59 | |
paulb@1 | 60 | |
paulb@198 | 61 | <!-- Match special conditional expression attributes. --> |
paulb@198 | 62 | |
paulb@198 | 63 | <xsl:template match="*[@template:if]"> |
paulb@198 | 64 | <axsl:if test="{@template:if}"> |
paulb@198 | 65 | <xsl:choose> |
paulb@198 | 66 | <xsl:when test="@template:element"> |
paulb@198 | 67 | <xsl:call-template name="enter-element"> |
paulb@198 | 68 | <xsl:with-param name="other-elements" select="@template:element"/> |
paulb@198 | 69 | </xsl:call-template> |
paulb@198 | 70 | </xsl:when> |
paulb@198 | 71 | <xsl:otherwise> |
paulb@198 | 72 | <xsl:call-template name="enter-attribute"/> |
paulb@198 | 73 | </xsl:otherwise> |
paulb@198 | 74 | </xsl:choose> |
paulb@198 | 75 | </axsl:if> |
paulb@198 | 76 | </xsl:template> |
paulb@198 | 77 | |
paulb@198 | 78 | |
paulb@198 | 79 | |
paulb@1 | 80 | <!-- Match element references. --> |
paulb@1 | 81 | |
paulb@198 | 82 | <xsl:template match="*[not(@template:if) and @template:element]"> |
paulb@1 | 83 | <xsl:call-template name="enter-element"> |
paulb@1 | 84 | <xsl:with-param name="other-elements" select="@template:element"/> |
paulb@1 | 85 | </xsl:call-template> |
paulb@1 | 86 | </xsl:template> |
paulb@1 | 87 | |
paulb@1 | 88 | <xsl:template name="enter-element"> |
paulb@1 | 89 | <xsl:param name="other-elements"/> |
paulb@1 | 90 | <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> |
paulb@1 | 91 | <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> |
paulb@1 | 92 | <xsl:choose> |
paulb@1 | 93 | <xsl:when test="$first-element = ''"> |
paulb@1 | 94 | <xsl:call-template name="next-element"> |
paulb@1 | 95 | <xsl:with-param name="first-element" select="$other-elements"/> |
paulb@1 | 96 | </xsl:call-template> |
paulb@1 | 97 | </xsl:when> |
paulb@1 | 98 | <xsl:otherwise> |
paulb@1 | 99 | <xsl:call-template name="next-element"> |
paulb@1 | 100 | <xsl:with-param name="first-element" select="$first-element"/> |
paulb@1 | 101 | <xsl:with-param name="remaining-elements" select="$remaining-elements"/> |
paulb@1 | 102 | </xsl:call-template> |
paulb@1 | 103 | </xsl:otherwise> |
paulb@1 | 104 | </xsl:choose> |
paulb@1 | 105 | </xsl:template> |
paulb@1 | 106 | |
paulb@1 | 107 | <xsl:template name="next-element"> |
paulb@1 | 108 | <xsl:param name="first-element"/> |
paulb@1 | 109 | <xsl:param name="remaining-elements"/> |
paulb@1 | 110 | <axsl:for-each select="{$first-element}"> |
paulb@1 | 111 | <xsl:choose> |
paulb@1 | 112 | <xsl:when test="$remaining-elements = ''"> |
paulb@126 | 113 | <xsl:call-template name="enter-attribute"/> |
paulb@1 | 114 | </xsl:when> |
paulb@1 | 115 | <xsl:otherwise> |
paulb@1 | 116 | <xsl:call-template name="enter-element"> |
paulb@1 | 117 | <xsl:with-param name="other-elements" select="$remaining-elements"/> |
paulb@1 | 118 | </xsl:call-template> |
paulb@1 | 119 | </xsl:otherwise> |
paulb@1 | 120 | </xsl:choose> |
paulb@1 | 121 | </axsl:for-each> |
paulb@1 | 122 | </xsl:template> |
paulb@1 | 123 | |
paulb@1 | 124 | |
paulb@1 | 125 | |
paulb@1 | 126 | <!-- Match special expression attributes. --> |
paulb@1 | 127 | |
paulb@477 | 128 | <xsl:template match="*[not(@template:if) and not(@template:element) and (@template:attribute or @template:value or @template:expr or @template:copy)]"> |
paulb@126 | 129 | <xsl:call-template name="enter-attribute"/> |
paulb@126 | 130 | </xsl:template> |
paulb@126 | 131 | |
paulb@126 | 132 | <xsl:template name="enter-attribute"> |
paulb@126 | 133 | <xsl:choose> |
paulb@126 | 134 | <xsl:when test="@template:attribute"> |
paulb@126 | 135 | <axsl:choose> |
paulb@126 | 136 | <axsl:when test="@{@template:attribute}"> |
paulb@126 | 137 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 138 | <axsl:variable name="this-value" select="@{@template:attribute}"/> |
paulb@126 | 139 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 140 | </axsl:when> |
paulb@126 | 141 | <axsl:otherwise> |
paulb@126 | 142 | <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> |
paulb@126 | 143 | <axsl:variable name="this-value"></axsl:variable> |
paulb@126 | 144 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 145 | </axsl:otherwise> |
paulb@126 | 146 | </axsl:choose> |
paulb@126 | 147 | </xsl:when> |
paulb@126 | 148 | <xsl:otherwise> |
paulb@126 | 149 | <xsl:call-template name="special-attributes"/> |
paulb@126 | 150 | </xsl:otherwise> |
paulb@126 | 151 | </xsl:choose> |
paulb@1 | 152 | </xsl:template> |
paulb@1 | 153 | |
paulb@1 | 154 | <xsl:template name="special-attributes"> |
paulb@1 | 155 | <xsl:choose> |
paulb@1 | 156 | <xsl:when test="@template:effect = 'replace'"> |
paulb@126 | 157 | <xsl:call-template name="special-value"/> |
paulb@1 | 158 | </xsl:when> |
paulb@1 | 159 | <xsl:otherwise> |
paulb@1 | 160 | <xsl:copy> |
paulb@1 | 161 | <xsl:apply-templates select="@*"/> |
paulb@432 | 162 | <xsl:call-template name="expression-attributes"/> |
paulb@1 | 163 | </xsl:copy> |
paulb@1 | 164 | </xsl:otherwise> |
paulb@1 | 165 | </xsl:choose> |
paulb@1 | 166 | </xsl:template> |
paulb@1 | 167 | |
paulb@432 | 168 | <xsl:template name="expression-attributes"> |
paulb@432 | 169 | <xsl:if test="@template:expr and @template:expr-attr"> |
paulb@432 | 170 | <axsl:if test="{@template:expr}"> |
paulb@432 | 171 | <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute> |
paulb@432 | 172 | </axsl:if> |
paulb@432 | 173 | </xsl:if> |
paulb@432 | 174 | <xsl:call-template name="special-value"/> |
paulb@432 | 175 | </xsl:template> |
paulb@432 | 176 | |
paulb@1 | 177 | <xsl:template name="special-value"> |
paulb@1 | 178 | <xsl:choose> |
paulb@432 | 179 | <!-- Insert the stated value. --> |
paulb@1 | 180 | <xsl:when test="@template:value"> |
paulb@1 | 181 | <axsl:value-of select="{@template:value}"/> |
paulb@1 | 182 | </xsl:when> |
paulb@432 | 183 | <!-- Insert the translated value. --> |
paulb@435 | 184 | <xsl:when test="@template:i18n"> |
paulb@435 | 185 | <xsl:call-template name="translated-value"/> |
paulb@432 | 186 | </xsl:when> |
paulb@477 | 187 | <!-- Copy the stated expression. --> |
paulb@477 | 188 | <xsl:when test="@template:copy"> |
paulb@477 | 189 | <axsl:copy-of select="{@template:copy}"/> |
paulb@477 | 190 | </xsl:when> |
paulb@432 | 191 | <!-- Just process the descendants. --> |
paulb@1 | 192 | <xsl:otherwise> |
paulb@1 | 193 | <xsl:apply-templates select="node()"/> |
paulb@1 | 194 | </xsl:otherwise> |
paulb@1 | 195 | </xsl:choose> |
paulb@1 | 196 | </xsl:template> |
paulb@1 | 197 | |
paulb@1 | 198 | |
paulb@1 | 199 | |
paulb@432 | 200 | <!-- Match internationalisation attributes. --> |
paulb@432 | 201 | |
paulb@432 | 202 | <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 | 203 | <xsl:copy> |
paulb@435 | 204 | <xsl:apply-templates select="@*"/> |
paulb@435 | 205 | <xsl:call-template name="translated-value"/> |
paulb@432 | 206 | </xsl:copy> |
paulb@432 | 207 | </xsl:template> |
paulb@432 | 208 | |
paulb@435 | 209 | <xsl:template name="translated-value"> |
paulb@432 | 210 | <xsl:choose> |
paulb@435 | 211 | <!-- Look for a translation of the contents. --> |
paulb@435 | 212 | <xsl:when test="@template:i18n = '-'"> |
paulb@435 | 213 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 214 | <axsl:variable name="translation" |
paulb@435 | 215 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{text()}']/text()"/> |
paulb@435 | 216 | <xsl:call-template name="insert-translated-value"/> |
paulb@432 | 217 | </xsl:when> |
paulb@435 | 218 | <!-- Look for a named translation. --> |
paulb@435 | 219 | <xsl:otherwise> |
paulb@435 | 220 | <!-- NOTE: Quoting not done completely. --> |
paulb@435 | 221 | <axsl:variable name="translation" |
paulb@435 | 222 | select="$translations/translations/locale[code/@value=$locale]/translation[@value='{@template:i18n}']/text()"/> |
paulb@435 | 223 | <xsl:call-template name="insert-translated-value"/> |
paulb@435 | 224 | </xsl:otherwise> |
paulb@432 | 225 | </xsl:choose> |
paulb@432 | 226 | </xsl:template> |
paulb@432 | 227 | |
paulb@435 | 228 | <xsl:template name="insert-translated-value"> |
paulb@435 | 229 | <axsl:choose> |
paulb@435 | 230 | <!-- Insert the translated value. --> |
paulb@435 | 231 | <axsl:when test="$translation"> |
paulb@435 | 232 | <axsl:value-of select="$translation"/> |
paulb@435 | 233 | </axsl:when> |
paulb@435 | 234 | <!-- Otherwise, just process the descendants. --> |
paulb@435 | 235 | <axsl:otherwise> |
paulb@435 | 236 | <xsl:apply-templates select="node()"/> |
paulb@435 | 237 | </axsl:otherwise> |
paulb@435 | 238 | </axsl:choose> |
paulb@432 | 239 | </xsl:template> |
paulb@432 | 240 | |
paulb@432 | 241 | |
paulb@432 | 242 | |
paulb@198 | 243 | <!-- Remove template attributes. --> |
paulb@1 | 244 | |
paulb@432 | 245 | <xsl:template match="@template:element|@template:init|@template:attribute|@template:value|@template:expr|@template:expr-attr|@template:effect|@template:if|@template:i18n"> |
paulb@1 | 246 | </xsl:template> |
paulb@1 | 247 | |
paulb@1 | 248 | |
paulb@1 | 249 | |
paulb@1 | 250 | <!-- Replicate unknown elements. --> |
paulb@1 | 251 | |
paulb@1 | 252 | <xsl:template match="@*|node()"> |
paulb@1 | 253 | <xsl:copy> |
paulb@1 | 254 | <xsl:apply-templates select="@*|node()"/> |
paulb@1 | 255 | </xsl:copy> |
paulb@1 | 256 | </xsl:template> |
paulb@1 | 257 | |
paulb@1 | 258 | </xsl:stylesheet> |