1 <?xml version="1.0"?> 2 <!-- 3 Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 --> 19 <xsl:stylesheet version="1.0" 20 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 21 xmlns:template="http://www.boddie.org.uk/ns/xmltools/template" 22 xmlns:str="http://exslt.org/strings" 23 extension-element-prefixes="str"> 24 25 <!-- NOTE: Add various top-level definitions specific to XHTML. --> 26 27 <xsl:output indent="yes" 28 doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 29 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 30 method="xml"/> 31 32 <!-- Get all declared expr-prefix attributes. --> 33 34 <xsl:variable name="prefixes" select="//@expr-prefix"/> 35 36 37 38 <!-- Process the root element. --> 39 40 <xsl:template match="/"> 41 <xsl:for-each select="*"> 42 <!-- NOTE: Stating the namespace using an explicit xmlns attribute. --> 43 <xsl:element name="{name()}"> 44 <xsl:attribute name="xmlns"><xsl:value-of select="namespace-uri()"/></xsl:attribute> 45 <xsl:for-each select="$prefixes"> 46 <xsl:attribute namespace="{substring-after(string(), ' ')}" name="{substring-before(string(), ' ')}:{name()}"><xsl:value-of select="string()"/></xsl:attribute> 47 </xsl:for-each> 48 <xsl:apply-templates select="@*|node()"/> 49 </xsl:element> 50 </xsl:for-each> 51 </xsl:template> 52 53 54 55 <!-- Remove the mangled template namespace declaration and other declarations. --> 56 57 <xsl:template match="@template|@expr-prefix"> 58 </xsl:template> 59 60 61 62 <!-- Match specific template attributes. --> 63 64 <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"> 65 <!-- Add the namespace. --> 66 <xsl:attribute name="template:{local-name(.)}"> 67 <xsl:copy-of select="string(.)"/> 68 </xsl:attribute> 69 </xsl:template> 70 71 72 73 <!-- Fix strings in attributes. --> 74 75 <xsl:template name="fix-string"> 76 <xsl:copy-of select="str:decode-uri(string(.))"/> 77 </xsl:template> 78 79 80 81 <!-- Handle special attributes. --> 82 83 <xsl:template match="@href|@src"> 84 <xsl:attribute name="{name(.)}"> 85 <xsl:call-template name="fix-string"/> 86 </xsl:attribute> 87 </xsl:template> 88 89 90 91 <!-- Traverse unknown nodes. --> 92 93 <xsl:template match="@*|node()"> 94 <xsl:variable name="this-name" select="name()"/> 95 <xsl:if test="not($prefixes[substring-before(string(), ' ') = $this-name])"> 96 <xsl:copy> 97 <xsl:apply-templates select="@*|node()"/> 98 </xsl:copy> 99 </xsl:if> 100 </xsl:template> 101 102 </xsl:stylesheet>