1 <?xml version="1.0"?> 2 <!-- 3 A stylesheet which takes lower-level template annotations and produces an output 4 stylesheet - something which is capable of transforming XML documents into Web 5 pages or other kinds of XML documents. 6 7 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 8 9 This library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Lesser General Public 11 License as published by the Free Software Foundation; either 12 version 2.1 of the License, or (at your option) any later version. 13 14 This library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Lesser General Public License for more details. 18 19 You should have received a copy of the GNU Lesser General Public 20 License along with this library; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 22 --> 23 <xsl:stylesheet version="1.0" 24 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 25 xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" 26 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> 27 28 <xsl:output indent="yes"/> 29 <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> 30 31 32 33 <!-- Match the document itself. --> 34 35 <xsl:template match="/"> 36 <axsl:stylesheet version="1.0"> 37 38 <axsl:output indent="yes"/> 39 <!-- NOTE: Hard-coded doctypes to hopefully satisfy JavaScript code. --> 40 <!-- doctype-public="-//W3C//DTD XHTML 1.1//EN" 41 doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" --> 42 43 <axsl:template match="/"> 44 45 <!-- Include the remaining attributes. --> 46 <xsl:apply-templates select="@*"/> 47 48 <!-- Process the elements. --> 49 <xsl:apply-templates select="*"/> 50 51 </axsl:template> 52 </axsl:stylesheet> 53 </xsl:template> 54 55 56 57 <!-- Match special conditional expression attributes. --> 58 59 <xsl:template match="*[@template:if]"> 60 <axsl:if test="{@template:if}"> 61 <xsl:choose> 62 <xsl:when test="@template:element"> 63 <xsl:call-template name="enter-element"> 64 <xsl:with-param name="other-elements" select="@template:element"/> 65 </xsl:call-template> 66 </xsl:when> 67 <xsl:otherwise> 68 <xsl:call-template name="enter-attribute"/> 69 </xsl:otherwise> 70 </xsl:choose> 71 </axsl:if> 72 </xsl:template> 73 74 75 76 <!-- Match element references. --> 77 78 <xsl:template match="*[not(@template:if) and @template:element]"> 79 <xsl:call-template name="enter-element"> 80 <xsl:with-param name="other-elements" select="@template:element"/> 81 </xsl:call-template> 82 </xsl:template> 83 84 <xsl:template name="enter-element"> 85 <xsl:param name="other-elements"/> 86 <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> 87 <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> 88 <xsl:choose> 89 <xsl:when test="$first-element = ''"> 90 <xsl:call-template name="next-element"> 91 <xsl:with-param name="first-element" select="$other-elements"/> 92 </xsl:call-template> 93 </xsl:when> 94 <xsl:otherwise> 95 <xsl:call-template name="next-element"> 96 <xsl:with-param name="first-element" select="$first-element"/> 97 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 98 </xsl:call-template> 99 </xsl:otherwise> 100 </xsl:choose> 101 </xsl:template> 102 103 <xsl:template name="next-element"> 104 <xsl:param name="first-element"/> 105 <xsl:param name="remaining-elements"/> 106 <axsl:for-each select="{$first-element}"> 107 <xsl:choose> 108 <xsl:when test="$remaining-elements = ''"> 109 <xsl:call-template name="enter-attribute"/> 110 </xsl:when> 111 <xsl:otherwise> 112 <xsl:call-template name="enter-element"> 113 <xsl:with-param name="other-elements" select="$remaining-elements"/> 114 </xsl:call-template> 115 </xsl:otherwise> 116 </xsl:choose> 117 </axsl:for-each> 118 </xsl:template> 119 120 121 122 <!-- Match special expression attributes. --> 123 124 <xsl:template match="*[not(@template:if) and not(@template:element) and (@template:attribute or @template:value or @template:expr)]"> 125 <xsl:call-template name="enter-attribute"/> 126 </xsl:template> 127 128 <xsl:template name="enter-attribute"> 129 <xsl:choose> 130 <xsl:when test="@template:attribute"> 131 <axsl:choose> 132 <axsl:when test="@{@template:attribute}"> 133 <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> 134 <axsl:variable name="this-value" select="@{@template:attribute}"/> 135 <xsl:call-template name="special-attributes"/> 136 </axsl:when> 137 <axsl:otherwise> 138 <axsl:variable name="this-name"><xsl:value-of select="@template:attribute"/></axsl:variable> 139 <axsl:variable name="this-value"></axsl:variable> 140 <xsl:call-template name="special-attributes"/> 141 </axsl:otherwise> 142 </axsl:choose> 143 </xsl:when> 144 <xsl:otherwise> 145 <xsl:call-template name="special-attributes"/> 146 </xsl:otherwise> 147 </xsl:choose> 148 </xsl:template> 149 150 <xsl:template name="special-attributes"> 151 <xsl:choose> 152 <xsl:when test="@template:effect = 'replace'"> 153 <xsl:call-template name="special-value"/> 154 </xsl:when> 155 <xsl:otherwise> 156 <xsl:copy> 157 <xsl:apply-templates select="@*"/> 158 <xsl:if test="@template:expr and @template:expr-attr"> 159 <axsl:if test="{@template:expr}"> 160 <axsl:attribute name="{@template:expr-attr}"><xsl:value-of select="@template:expr-attr"/></axsl:attribute> 161 </axsl:if> 162 </xsl:if> 163 <xsl:call-template name="special-value"/> 164 </xsl:copy> 165 </xsl:otherwise> 166 </xsl:choose> 167 </xsl:template> 168 169 <xsl:template name="special-value"> 170 <xsl:choose> 171 <xsl:when test="@template:value"> 172 <axsl:value-of select="{@template:value}"/> 173 </xsl:when> 174 <xsl:otherwise> 175 <xsl:apply-templates select="node()"/> 176 </xsl:otherwise> 177 </xsl:choose> 178 </xsl:template> 179 180 181 182 <!-- Remove template attributes. --> 183 184 <xsl:template match="@template:element|@template:init|@template:attribute|@template:value|@template:expr|@template:expr-attr|@template:effect|@template:if"> 185 </xsl:template> 186 187 188 189 <!-- Replicate unknown elements. --> 190 191 <xsl:template match="@*|node()"> 192 <xsl:copy> 193 <xsl:apply-templates select="@*|node()"/> 194 </xsl:copy> 195 </xsl:template> 196 197 </xsl:stylesheet>