1 <?xml version="1.0"?> 2 <!-- 3 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU Lesser General Public License as published by the Free 7 Software Foundation; either version 3 of the License, or (at your option) any 8 later version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 13 details. 14 15 You should have received a copy of the GNU Lesser General Public License along 16 with this program. If not, see <http://www.gnu.org/licenses/>. 17 --> 18 <xsl:stylesheet version="1.0" 19 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 20 xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" 21 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> 22 23 <xsl:output indent="yes"/> 24 <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> 25 26 27 28 <!-- Match the document itself. --> 29 30 <xsl:template match="/"> 31 <axsl:stylesheet version="1.0"> 32 33 <axsl:output indent="yes"/> 34 35 <axsl:template select="/"> 36 37 <!-- Process the elements. --> 38 <xsl:apply-templates select="*"/> 39 40 </axsl:template> 41 42 </axsl:stylesheet> 43 </xsl:template> 44 45 46 47 <!-- Match element references. --> 48 49 <xsl:template match="*[@template:element]"> 50 <xsl:call-template name="enter-element"> 51 <xsl:with-param name="other-elements" select="@template:element"/> 52 </xsl:call-template> 53 </xsl:template> 54 55 <xsl:template name="enter-element"> 56 <xsl:param name="other-elements"/> 57 <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> 58 <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> 59 <xsl:choose> 60 <xsl:when test="$first-element = ''"> 61 <xsl:call-template name="next-element"> 62 <xsl:with-param name="first-element" select="$other-elements"/> 63 </xsl:call-template> 64 </xsl:when> 65 <xsl:otherwise> 66 <xsl:call-template name="next-element"> 67 <xsl:with-param name="first-element" select="$first-element"/> 68 <xsl:with-param name="remaining-elements" select="$remaining-elements"/> 69 </xsl:call-template> 70 </xsl:otherwise> 71 </xsl:choose> 72 </xsl:template> 73 74 <xsl:template name="next-element"> 75 <xsl:param name="first-element"/> 76 <xsl:param name="remaining-elements"/> 77 <axsl:for-each select="{$first-element}"> 78 <axsl:element name="{$first-element}"> 79 <xsl:choose> 80 <xsl:when test="$remaining-elements = ''"> 81 <xsl:call-template name="enter-attribute"/> 82 </xsl:when> 83 <xsl:otherwise> 84 <xsl:call-template name="enter-element"> 85 <xsl:with-param name="other-elements" select="$remaining-elements"/> 86 </xsl:call-template> 87 </xsl:otherwise> 88 </xsl:choose> 89 </axsl:element> 90 </axsl:for-each> 91 </xsl:template> 92 93 94 95 <!-- Match special expression attributes. --> 96 97 <xsl:template match="*[not(@template:element) and @template:attribute]"> 98 <xsl:call-template name="enter-attribute"/> 99 </xsl:template> 100 101 <xsl:template name="enter-attribute"> 102 <xsl:choose> 103 <xsl:when test="@template:attribute"> 104 <axsl:choose> 105 <axsl:when test="@{@template:attribute}"> 106 <axsl:attribute name="{@template:attribute}"><axsl:value-of select="@{@template:attribute}"/></axsl:attribute> 107 <xsl:apply-templates select="*"/> 108 </axsl:when> 109 <axsl:otherwise> 110 <axsl:attribute name="{@template:attribute}"/> 111 <xsl:apply-templates select="*"/> 112 </axsl:otherwise> 113 </axsl:choose> 114 </xsl:when> 115 <xsl:otherwise> 116 <xsl:apply-templates select="*"/> 117 </xsl:otherwise> 118 </xsl:choose> 119 </xsl:template> 120 121 122 123 <!-- Traverse unknown elements. --> 124 125 <xsl:template match="*"> 126 <xsl:apply-templates select="*"/> 127 </xsl:template> 128 129 </xsl:stylesheet>