paulb@245 | 1 | <?xml version="1.0"?> |
paulb@245 | 2 | <xsl:stylesheet version="1.0" |
paulb@245 | 3 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@245 | 4 | xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" |
paulb@245 | 5 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"> |
paulb@245 | 6 | |
paulb@245 | 7 | <xsl:output indent="yes"/> |
paulb@245 | 8 | <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> |
paulb@245 | 9 | |
paulb@245 | 10 | |
paulb@245 | 11 | |
paulb@245 | 12 | <!-- Match the document itself. --> |
paulb@245 | 13 | |
paulb@245 | 14 | <xsl:template match="/"> |
paulb@245 | 15 | <axsl:stylesheet version="1.0"> |
paulb@245 | 16 | |
paulb@245 | 17 | <axsl:output indent="yes"/> |
paulb@245 | 18 | |
paulb@245 | 19 | <!-- Make document parameters for all elements appearing to use enumerations. --> |
paulb@245 | 20 | <xsl:for-each select="//element[*/@expr]"> |
paulb@245 | 21 | <axsl:param name="{@name}"/> |
paulb@245 | 22 | </xsl:for-each> |
paulb@245 | 23 | |
paulb@245 | 24 | <!-- Make a document-level rule. --> |
paulb@245 | 25 | <axsl:template match="/"> |
paulb@245 | 26 | <axsl:apply-templates select="*"/> |
paulb@245 | 27 | </axsl:template> |
paulb@245 | 28 | |
paulb@245 | 29 | <!-- Process the top-level element to make other rules. --> |
paulb@245 | 30 | <xsl:apply-templates select="*"/> |
paulb@245 | 31 | |
paulb@245 | 32 | <!-- Replicate unknown elements. --> |
paulb@245 | 33 | <axsl:template match="@*|node()"> |
paulb@245 | 34 | <axsl:copy> |
paulb@245 | 35 | <axsl:apply-templates select="@*|node()"/> |
paulb@245 | 36 | </axsl:copy> |
paulb@245 | 37 | </axsl:template> |
paulb@245 | 38 | |
paulb@245 | 39 | </axsl:stylesheet> |
paulb@245 | 40 | </xsl:template> |
paulb@245 | 41 | |
paulb@245 | 42 | |
paulb@245 | 43 | |
paulb@245 | 44 | <!-- Match element references. --> |
paulb@245 | 45 | |
paulb@245 | 46 | <xsl:template match="element"> |
paulb@245 | 47 | <xsl:param name="nested">false</xsl:param> |
paulb@245 | 48 | |
paulb@245 | 49 | <!-- Make a rule for the element. --> |
paulb@245 | 50 | <axsl:template match="{@name}"> |
paulb@245 | 51 | |
paulb@245 | 52 | <!-- Copy the element. --> |
paulb@245 | 53 | <xsl:element name="{@name}"> |
paulb@245 | 54 | |
paulb@245 | 55 | <!-- Process attributes. --> |
paulb@245 | 56 | <axsl:apply-templates select="@*"/> |
paulb@245 | 57 | |
paulb@245 | 58 | <!-- Find elements and determine how to process them. --> |
paulb@245 | 59 | <xsl:for-each select="element"> |
paulb@245 | 60 | <xsl:choose> |
paulb@245 | 61 | <!-- Enumerations. --> |
paulb@245 | 62 | <xsl:when test="@expr"> |
paulb@245 | 63 | <xsl:call-template name="inside-element"/> |
paulb@245 | 64 | </xsl:when> |
paulb@245 | 65 | <!-- Top-level or "section" elements are added. --> |
paulb@245 | 66 | <xsl:when test="count(attribute) = 0 or $nested = 'false'"> |
paulb@245 | 67 | <xsl:element name="{@name}"> |
paulb@245 | 68 | <xsl:call-template name="inside-element"/> |
paulb@245 | 69 | </xsl:element> |
paulb@245 | 70 | </xsl:when> |
paulb@245 | 71 | <!-- Other elements are only added if found. --> |
paulb@245 | 72 | <xsl:otherwise> |
paulb@245 | 73 | <axsl:apply-templates select="{@name}"/> |
paulb@245 | 74 | </xsl:otherwise> |
paulb@245 | 75 | </xsl:choose> |
paulb@245 | 76 | </xsl:for-each> |
paulb@245 | 77 | |
paulb@245 | 78 | </xsl:element> |
paulb@245 | 79 | |
paulb@245 | 80 | </axsl:template> |
paulb@245 | 81 | |
paulb@245 | 82 | <!-- Make rules for nested elements. --> |
paulb@245 | 83 | <xsl:choose> |
paulb@245 | 84 | <xsl:when test="$nested = 'true'"> |
paulb@245 | 85 | <xsl:apply-templates select="*[not(@expr)]"> |
paulb@245 | 86 | <xsl:with-param name="nested">true</xsl:with-param> |
paulb@245 | 87 | </xsl:apply-templates> |
paulb@245 | 88 | </xsl:when> |
paulb@245 | 89 | <xsl:otherwise> |
paulb@245 | 90 | <xsl:apply-templates select="*/*[not(@expr)]"> |
paulb@245 | 91 | <xsl:with-param name="nested">true</xsl:with-param> |
paulb@245 | 92 | </xsl:apply-templates> |
paulb@245 | 93 | </xsl:otherwise> |
paulb@245 | 94 | </xsl:choose> |
paulb@245 | 95 | |
paulb@245 | 96 | </xsl:template> |
paulb@245 | 97 | |
paulb@245 | 98 | |
paulb@245 | 99 | |
paulb@245 | 100 | <!-- Fill in elements. --> |
paulb@245 | 101 | |
paulb@245 | 102 | <xsl:template name="inside-element"> |
paulb@245 | 103 | <axsl:apply-templates select="{@name}/@*"/> |
paulb@245 | 104 | <xsl:choose> |
paulb@245 | 105 | <!-- Enumerations. --> |
paulb@245 | 106 | <xsl:when test="@expr"> |
paulb@245 | 107 | <xsl:call-template name="inside-enumeration"/> |
paulb@245 | 108 | </xsl:when> |
paulb@245 | 109 | <xsl:otherwise> |
paulb@245 | 110 | <!-- Enumeration containers. --> |
paulb@245 | 111 | <xsl:for-each select="*[@expr]"> |
paulb@245 | 112 | <xsl:call-template name="inside-element"/> |
paulb@245 | 113 | </xsl:for-each> |
paulb@245 | 114 | <!-- Normal elements. --> |
paulb@245 | 115 | <xsl:if test="count(element[not(@expr)]) != 0"> |
paulb@245 | 116 | <axsl:apply-templates select="{@name}/*"/> |
paulb@245 | 117 | </xsl:if> |
paulb@245 | 118 | </xsl:otherwise> |
paulb@245 | 119 | </xsl:choose> |
paulb@245 | 120 | </xsl:template> |
paulb@245 | 121 | |
paulb@245 | 122 | |
paulb@245 | 123 | |
paulb@245 | 124 | <!-- Fill in enumerations. --> |
paulb@245 | 125 | |
paulb@245 | 126 | <xsl:template name="inside-enumeration"> |
paulb@245 | 127 | <axsl:for-each select="${../@name}/{../@name}/{@name}"> |
paulb@245 | 128 | <axsl:copy> |
paulb@245 | 129 | <axsl:apply-templates select="@*"/> |
paulb@245 | 130 | </axsl:copy> |
paulb@245 | 131 | </axsl:for-each> |
paulb@245 | 132 | </xsl:template> |
paulb@245 | 133 | |
paulb@245 | 134 | </xsl:stylesheet> |