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 | |
paulb@245 | 48 | <!-- Make a rule for the element. --> |
paulb@245 | 49 | <axsl:template match="{@name}"> |
paulb@245 | 50 | |
paulb@245 | 51 | <!-- Copy the element. --> |
paulb@245 | 52 | <xsl:element name="{@name}"> |
paulb@245 | 53 | |
paulb@245 | 54 | <!-- Process attributes. --> |
paulb@245 | 55 | <axsl:apply-templates select="@*"/> |
paulb@245 | 56 | |
paulb@245 | 57 | <!-- Find elements and determine how to process them. --> |
paulb@245 | 58 | <xsl:for-each select="element"> |
paulb@258 | 59 | <xsl:call-template name="process-element"/> |
paulb@245 | 60 | </xsl:for-each> |
paulb@245 | 61 | </xsl:element> |
paulb@245 | 62 | </axsl:template> |
paulb@245 | 63 | |
paulb@245 | 64 | <!-- Make rules for nested elements. --> |
paulb@258 | 65 | <xsl:for-each select="element"> |
paulb@258 | 66 | <xsl:call-template name="process-rules"/> |
paulb@258 | 67 | </xsl:for-each> |
paulb@245 | 68 | |
paulb@245 | 69 | </xsl:template> |
paulb@245 | 70 | |
paulb@245 | 71 | |
paulb@245 | 72 | |
paulb@258 | 73 | <!-- Process elements. --> |
paulb@258 | 74 | |
paulb@258 | 75 | <xsl:template name="process-element"> |
paulb@258 | 76 | <xsl:param name="path" select="@name"/> |
paulb@245 | 77 | |
paulb@258 | 78 | <!-- Define elements which do not have selectors. --> |
paulb@258 | 79 | <xsl:variable name="adding-selectors" select="count(//selector[@element=current()/@name])"/> |
paulb@258 | 80 | |
paulb@245 | 81 | <xsl:choose> |
paulb@245 | 82 | <!-- Enumerations. --> |
paulb@258 | 83 | <xsl:when test="@type='multiple-choice-value' or @type='multiple-choice-list-value'"> |
paulb@263 | 84 | <xsl:call-template name="inside-enumeration"> |
paulb@263 | 85 | <xsl:with-param name="path" select="$path"/> |
paulb@263 | 86 | </xsl:call-template> |
paulb@245 | 87 | </xsl:when> |
paulb@258 | 88 | <!-- Added elements. --> |
paulb@258 | 89 | <xsl:when test="$adding-selectors = 0"> |
paulb@258 | 90 | <xsl:element name="{@name}"> |
paulb@258 | 91 | <axsl:apply-templates select="{$path}/@*"/> |
paulb@258 | 92 | <xsl:for-each select="element"> |
paulb@258 | 93 | <xsl:call-template name="process-element"> |
paulb@258 | 94 | <xsl:with-param name="path" select="concat($path, '/', @name)"/> |
paulb@258 | 95 | </xsl:call-template> |
paulb@258 | 96 | </xsl:for-each> |
paulb@258 | 97 | </xsl:element> |
paulb@258 | 98 | </xsl:when> |
paulb@258 | 99 | <!-- Other elements are only added if found. --> |
paulb@245 | 100 | <xsl:otherwise> |
paulb@258 | 101 | <axsl:apply-templates select="{$path}"/> |
paulb@258 | 102 | </xsl:otherwise> |
paulb@258 | 103 | </xsl:choose> |
paulb@258 | 104 | </xsl:template> |
paulb@258 | 105 | |
paulb@258 | 106 | |
paulb@258 | 107 | |
paulb@258 | 108 | <!-- Process rules. --> |
paulb@258 | 109 | |
paulb@258 | 110 | <xsl:template name="process-rules"> |
paulb@263 | 111 | <xsl:param name="path" select="@name"/> |
paulb@258 | 112 | |
paulb@258 | 113 | <!-- Define elements which do not have selectors. --> |
paulb@258 | 114 | <!-- NOTE: Duplicating adding-selectors - see above. --> |
paulb@258 | 115 | <xsl:variable name="adding-selectors" select="count(//selector[@element=current()/@name])"/> |
paulb@258 | 116 | |
paulb@258 | 117 | <xsl:choose> |
paulb@258 | 118 | <xsl:when test="@type='multiple-choice-value' or @type='multiple-choice-list-value'"> |
paulb@258 | 119 | <!-- Do not match multiple-choice values. --> |
paulb@258 | 120 | </xsl:when> |
paulb@258 | 121 | <xsl:when test="$adding-selectors = 0"> |
paulb@258 | 122 | <xsl:for-each select="element"> |
paulb@263 | 123 | <xsl:call-template name="process-rules"> |
paulb@263 | 124 | <xsl:with-param name="path" select="concat($path, '/', @name)"/> |
paulb@263 | 125 | </xsl:call-template> |
paulb@245 | 126 | </xsl:for-each> |
paulb@258 | 127 | </xsl:when> |
paulb@258 | 128 | <xsl:otherwise> |
paulb@258 | 129 | <xsl:apply-templates select="."/> |
paulb@245 | 130 | </xsl:otherwise> |
paulb@245 | 131 | </xsl:choose> |
paulb@245 | 132 | </xsl:template> |
paulb@245 | 133 | |
paulb@245 | 134 | |
paulb@245 | 135 | |
paulb@245 | 136 | <!-- Fill in enumerations. --> |
paulb@245 | 137 | |
paulb@245 | 138 | <xsl:template name="inside-enumeration"> |
paulb@263 | 139 | <xsl:param name="path" select="@name"/> |
paulb@263 | 140 | |
paulb@263 | 141 | <!-- Store multiple-choice selections, if appropriate. --> |
paulb@263 | 142 | <xsl:if test="../@type='multiple-choice-list'"> |
paulb@263 | 143 | <axsl:variable name="values-{@name}" select="{$path}/@{../attribute/@name}"/> |
paulb@263 | 144 | </xsl:if> |
paulb@263 | 145 | |
paulb@245 | 146 | <axsl:for-each select="${../@name}/{../@name}/{@name}"> |
paulb@245 | 147 | <axsl:copy> |
paulb@245 | 148 | <axsl:apply-templates select="@*"/> |
paulb@258 | 149 | <xsl:if test="@type='multiple-choice-list-value'"> |
paulb@263 | 150 | <axsl:if test="$values-{@name}[string() = current()/@{attribute/@name}]"> |
paulb@269 | 151 | <axsl:attribute name="{@expr-name}">true</axsl:attribute> |
paulb@258 | 152 | </axsl:if> |
paulb@258 | 153 | </xsl:if> |
paulb@245 | 154 | </axsl:copy> |
paulb@245 | 155 | </axsl:for-each> |
paulb@245 | 156 | </xsl:template> |
paulb@245 | 157 | |
paulb@245 | 158 | </xsl:stylesheet> |