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@233 | 51 | <xsl:element name="element"> |
paulb@233 | 52 | <xsl:attribute name="name"><xsl:value-of select="$first-element"/></xsl:attribute> |
paulb@233 | 53 | <xsl:choose> |
paulb@233 | 54 | <xsl:when test="$remaining-elements = ''"> |
paulb@233 | 55 | <xsl:call-template name="enter-attribute"/> |
paulb@233 | 56 | </xsl:when> |
paulb@233 | 57 | <xsl:otherwise> |
paulb@233 | 58 | <xsl:call-template name="enter-element"> |
paulb@233 | 59 | <xsl:with-param name="other-elements" select="$remaining-elements"/> |
paulb@233 | 60 | </xsl:call-template> |
paulb@233 | 61 | </xsl:otherwise> |
paulb@233 | 62 | </xsl:choose> |
paulb@233 | 63 | </xsl:element> |
paulb@233 | 64 | </xsl:template> |
paulb@233 | 65 | |
paulb@233 | 66 | |
paulb@233 | 67 | |
paulb@233 | 68 | <!-- Match attributes. --> |
paulb@233 | 69 | |
paulb@233 | 70 | <xsl:template match="*[not(@template:element) and (@template:attribute or @template:expr-attr)]"> |
paulb@233 | 71 | <xsl:call-template name="enter-attribute"/> |
paulb@233 | 72 | </xsl:template> |
paulb@233 | 73 | |
paulb@233 | 74 | <xsl:template name="enter-attribute"> |
paulb@233 | 75 | <xsl:choose> |
paulb@233 | 76 | <xsl:when test="@template:attribute"> |
paulb@233 | 77 | <xsl:element name="attribute"> |
paulb@233 | 78 | <xsl:attribute name="name"><xsl:value-of select="@template:attribute"/></xsl:attribute> |
paulb@233 | 79 | </xsl:element> |
paulb@233 | 80 | </xsl:when> |
paulb@233 | 81 | <xsl:when test="@template:expr-attr"> |
paulb@233 | 82 | <xsl:element name="attribute"> |
paulb@233 | 83 | <xsl:attribute name="name"><xsl:value-of select="@template:value"/></xsl:attribute> |
paulb@233 | 84 | </xsl:element> |
paulb@233 | 85 | </xsl:when> |
paulb@233 | 86 | <xsl:otherwise> |
paulb@233 | 87 | <xsl:apply-templates select="*"/> |
paulb@233 | 88 | </xsl:otherwise> |
paulb@233 | 89 | </xsl:choose> |
paulb@233 | 90 | </xsl:template> |
paulb@233 | 91 | |
paulb@233 | 92 | |
paulb@233 | 93 | |
paulb@233 | 94 | <!-- Traverse unknown elements. --> |
paulb@233 | 95 | |
paulb@233 | 96 | <xsl:template match="*"> |
paulb@233 | 97 | <xsl:apply-templates select="*"/> |
paulb@233 | 98 | </xsl:template> |
paulb@233 | 99 | |
paulb@233 | 100 | </xsl:stylesheet> |