paulb@233 | 1 | <?xml version="1.0"?> |
paulb@233 | 2 | <xsl:stylesheet version="1.0" |
paulb@233 | 3 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@233 | 4 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> |
paulb@233 | 5 | |
paulb@233 | 6 | <xsl:output indent="yes"/> |
paulb@233 | 7 | |
paulb@233 | 8 | |
paulb@233 | 9 | |
paulb@233 | 10 | <!-- Match the document itself. --> |
paulb@233 | 11 | |
paulb@233 | 12 | <xsl:template match="/"> |
paulb@233 | 13 | |
paulb@233 | 14 | <!-- Process the elements. --> |
paulb@233 | 15 | <xsl:apply-templates select="*"/> |
paulb@233 | 16 | |
paulb@233 | 17 | </xsl:template> |
paulb@233 | 18 | |
paulb@233 | 19 | |
paulb@233 | 20 | |
paulb@233 | 21 | <!-- Match element references. --> |
paulb@233 | 22 | |
paulb@233 | 23 | <xsl:template match="*[@template:element]"> |
paulb@233 | 24 | <xsl:call-template name="enter-element"> |
paulb@233 | 25 | <xsl:with-param name="other-elements" select="@template:element"/> |
paulb@233 | 26 | </xsl:call-template> |
paulb@233 | 27 | </xsl:template> |
paulb@233 | 28 | |
paulb@233 | 29 | <xsl:template name="enter-element"> |
paulb@233 | 30 | <xsl:param name="other-elements"/> |
paulb@233 | 31 | <xsl:variable name="first-element" select="substring-before($other-elements, ',')"/> |
paulb@233 | 32 | <xsl:variable name="remaining-elements" select="substring-after($other-elements, ',')"/> |
paulb@233 | 33 | <xsl:choose> |
paulb@233 | 34 | <xsl:when test="$first-element = ''"> |
paulb@233 | 35 | <xsl:call-template name="next-element"> |
paulb@233 | 36 | <xsl:with-param name="first-element" select="$other-elements"/> |
paulb@233 | 37 | </xsl:call-template> |
paulb@233 | 38 | </xsl:when> |
paulb@233 | 39 | <xsl:otherwise> |
paulb@233 | 40 | <xsl:call-template name="next-element"> |
paulb@233 | 41 | <xsl:with-param name="first-element" select="$first-element"/> |
paulb@233 | 42 | <xsl:with-param name="remaining-elements" select="$remaining-elements"/> |
paulb@233 | 43 | </xsl:call-template> |
paulb@233 | 44 | </xsl:otherwise> |
paulb@233 | 45 | </xsl:choose> |
paulb@233 | 46 | </xsl:template> |
paulb@233 | 47 | |
paulb@233 | 48 | <xsl:template name="next-element"> |
paulb@233 | 49 | <xsl:param name="first-element"/> |
paulb@233 | 50 | <xsl:param name="remaining-elements"/> |
paulb@245 | 51 | <element> |
paulb@233 | 52 | <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> |
paulb@245 | 53 | <xsl:if test="@template:expr"> |
paulb@245 | 54 | <xsl:attribute name="expr"><xsl:value-of select="@template:expr"/></xsl:attribute> |
paulb@245 | 55 | </xsl:if> |
paulb@233 | 56 | <xsl:choose> |
paulb@233 | 57 | <xsl:when test="$remaining-elements = ''"> |
paulb@233 | 58 | <xsl:call-template name="enter-attribute"/> |
paulb@233 | 59 | </xsl:when> |
paulb@233 | 60 | <xsl:otherwise> |
paulb@233 | 61 | <xsl:call-template name="enter-element"> |
paulb@233 | 62 | <xsl:with-param name="other-elements" select="$remaining-elements"/> |
paulb@233 | 63 | </xsl:call-template> |
paulb@233 | 64 | </xsl:otherwise> |
paulb@233 | 65 | </xsl:choose> |
paulb@245 | 66 | </element> |
paulb@233 | 67 | </xsl:template> |
paulb@233 | 68 | |
paulb@233 | 69 | |
paulb@233 | 70 | |
paulb@233 | 71 | <!-- Match attributes. --> |
paulb@233 | 72 | |
paulb@233 | 73 | <xsl:template match="*[not(@template:element) and (@template:attribute or @template:expr-attr)]"> |
paulb@233 | 74 | <xsl:call-template name="enter-attribute"/> |
paulb@233 | 75 | </xsl:template> |
paulb@233 | 76 | |
paulb@233 | 77 | <xsl:template name="enter-attribute"> |
paulb@233 | 78 | <xsl:choose> |
paulb@233 | 79 | <xsl:when test="@template:attribute"> |
paulb@245 | 80 | <attribute> |
paulb@233 | 81 | <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> |
paulb@245 | 82 | </attribute> |
paulb@233 | 83 | </xsl:when> |
paulb@233 | 84 | <xsl:otherwise> |
paulb@245 | 85 | <xsl:if test="@template:expr-attr"> |
paulb@245 | 86 | <xsl:attribute name="expr-attr"><xsl:value-of select="@template:expr-attr"/></xsl:attribute> |
paulb@245 | 87 | </xsl:if> |
paulb@245 | 88 | <xsl:if test="@template:expr"> |
paulb@245 | 89 | <xsl:attribute name="expr"><xsl:value-of select="@template:expr"/></xsl:attribute> |
paulb@245 | 90 | </xsl:if> |
paulb@245 | 91 | <xsl:if test="@template:value"> |
paulb@245 | 92 | <xsl:attribute name="value"><xsl:value-of select="@template:value"/></xsl:attribute> |
paulb@245 | 93 | </xsl:if> |
paulb@233 | 94 | <xsl:apply-templates select="*"/> |
paulb@233 | 95 | </xsl:otherwise> |
paulb@233 | 96 | </xsl:choose> |
paulb@233 | 97 | </xsl:template> |
paulb@233 | 98 | |
paulb@233 | 99 | |
paulb@233 | 100 | |
paulb@233 | 101 | <!-- Traverse unknown elements. --> |
paulb@233 | 102 | |
paulb@233 | 103 | <xsl:template match="*"> |
paulb@233 | 104 | <xsl:apply-templates select="*"/> |
paulb@233 | 105 | </xsl:template> |
paulb@233 | 106 | |
paulb@233 | 107 | </xsl:stylesheet> |