1 <?xml version="1.0"?> 2 <!-- 3 Copyright (C) 2006, 2007 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:template="http://www.boddie.org.uk/ns/xmltools/template" 21 xmlns:str="http://exslt.org/strings" 22 extension-element-prefixes="str"> 23 24 <!-- NOTE: Add various top-level definitions specific to XHTML. --> 25 26 <xsl:output indent="yes" 27 doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 28 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 29 method="xml"/> 30 31 <!-- Get all declared expr-prefix attributes. --> 32 33 <xsl:variable name="prefixes" select="//@expr-prefix"/> 34 35 36 37 <!-- Process the root element. --> 38 39 <xsl:template match="/"> 40 <xsl:for-each select="*"> 41 <!-- NOTE: Stating the namespace using an explicit xmlns attribute. --> 42 <xsl:element name="{name()}"> 43 <xsl:attribute name="xmlns"><xsl:value-of select="namespace-uri()"/></xsl:attribute> 44 <xsl:for-each select="$prefixes"> 45 <xsl:attribute namespace="{substring-after(string(), ' ')}" name="{substring-before(string(), ' ')}:{name()}"><xsl:value-of select="string()"/></xsl:attribute> 46 </xsl:for-each> 47 <xsl:apply-templates select="@*|node()"/> 48 </xsl:element> 49 </xsl:for-each> 50 </xsl:template> 51 52 53 54 <!-- Remove the mangled template namespace declaration and other declarations. --> 55 56 <xsl:template match="@template|@expr-prefix"> 57 </xsl:template> 58 59 60 61 <!-- Match specific template attributes. --> 62 63 <xsl:template match="@if|@element|@attribute|@attribute-field|@attribute-area|@attribute-button|@attribute-list-button|@selector-field|@multiple-choice-field|@multiple-choice-list-field|@multiple-choice-value|@multiple-choice-list-value|@multiple-choice-list-element|@effect|@value|@expr|@expr-attr|@i18n|@copy"> 64 <!-- Add the namespace. --> 65 <xsl:attribute name="template:{local-name(.)}"> 66 <xsl:copy-of select="string(.)"/> 67 </xsl:attribute> 68 </xsl:template> 69 70 71 72 <!-- Fix strings in attributes. --> 73 74 <xsl:template name="fix-string"> 75 <xsl:copy-of select="str:decode-uri(string(.))"/> 76 </xsl:template> 77 78 79 80 <!-- Handle special attributes. --> 81 82 <xsl:template match="@href|@src"> 83 <xsl:attribute name="{name(.)}"> 84 <xsl:call-template name="fix-string"/> 85 </xsl:attribute> 86 </xsl:template> 87 88 89 90 <!-- Traverse unknown nodes. --> 91 92 <xsl:template match="@*|node()"> 93 <xsl:variable name="this-name" select="name()"/> 94 <xsl:if test="not($prefixes[substring-before(string(), ' ') = $this-name])"> 95 <xsl:copy> 96 <xsl:apply-templates select="@*|node()"/> 97 </xsl:copy> 98 </xsl:if> 99 </xsl:template> 100 101 </xsl:stylesheet>