paulb@30 | 1 | <?xml version="1.0" encoding="utf-8"?> |
paulb@30 | 2 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@30 | 3 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template" |
paulb@55 | 4 | xmlns:dyn="http://exslt.org/dynamic" |
paulb@55 | 5 | extension-element-prefixes="dyn" |
paulb@30 | 6 | version="1.0"> |
paulb@30 | 7 | |
paulb@30 | 8 | <xsl:param name="database"/> |
paulb@30 | 9 | |
paulb@30 | 10 | <xsl:template match="configuration"> |
paulb@30 | 11 | <xsl:copy> |
paulb@30 | 12 | <xsl:apply-templates select="@*"/> |
paulb@30 | 13 | <!-- Complete all multiple choice fields. --> |
paulb@30 | 14 | <details> |
paulb@30 | 15 | <xsl:call-template name="complete-element"> |
paulb@55 | 16 | <xsl:with-param name="found-element" select="details/base-system[1]"/> |
paulb@30 | 17 | <xsl:with-param name="element-name">base-system</xsl:with-param> |
paulb@30 | 18 | </xsl:call-template> |
paulb@30 | 19 | <xsl:call-template name="complete-element"> |
paulb@55 | 20 | <xsl:with-param name="found-element" select="details/cpu[1]"/> |
paulb@55 | 21 | <xsl:with-param name="control-element" select="details/base-system[1]"/> |
paulb@30 | 22 | <xsl:with-param name="element-name">cpu</xsl:with-param> |
paulb@55 | 23 | <xsl:with-param name="control-element-name">base-system</xsl:with-param> |
paulb@55 | 24 | <xsl:with-param name="control-attribute-name">cpu-socket</xsl:with-param> |
paulb@30 | 25 | </xsl:call-template> |
paulb@30 | 26 | </details> |
paulb@30 | 27 | <memory> |
paulb@30 | 28 | <xsl:for-each select="memory/memory-unit"> |
paulb@30 | 29 | <xsl:call-template name="complete-element"> |
paulb@55 | 30 | <xsl:with-param name="found-element" select="."/> |
paulb@30 | 31 | <xsl:with-param name="element-name">memory-unit</xsl:with-param> |
paulb@30 | 32 | </xsl:call-template> |
paulb@30 | 33 | </xsl:for-each> |
paulb@30 | 34 | </memory> |
paulb@30 | 35 | <hard-disks> |
paulb@30 | 36 | <xsl:for-each select="hard-disks/hard-disk"> |
paulb@30 | 37 | <xsl:call-template name="complete-element"> |
paulb@55 | 38 | <xsl:with-param name="found-element" select="."/> |
paulb@30 | 39 | <xsl:with-param name="element-name">hard-disk</xsl:with-param> |
paulb@30 | 40 | </xsl:call-template> |
paulb@30 | 41 | </xsl:for-each> |
paulb@30 | 42 | </hard-disks> |
paulb@30 | 43 | <storage> |
paulb@30 | 44 | <xsl:for-each select="storage/storage-unit"> |
paulb@30 | 45 | <xsl:call-template name="complete-element"> |
paulb@55 | 46 | <xsl:with-param name="found-element" select="."/> |
paulb@30 | 47 | <xsl:with-param name="element-name">storage-unit</xsl:with-param> |
paulb@30 | 48 | </xsl:call-template> |
paulb@30 | 49 | </xsl:for-each> |
paulb@30 | 50 | </storage> |
paulb@30 | 51 | <peripherals> |
paulb@30 | 52 | <xsl:call-template name="complete-element"> |
paulb@55 | 53 | <xsl:with-param name="found-element" select="peripherals/keyboard[1]"/> |
paulb@30 | 54 | <xsl:with-param name="element-name">keyboard</xsl:with-param> |
paulb@30 | 55 | </xsl:call-template> |
paulb@30 | 56 | <xsl:call-template name="complete-element"> |
paulb@55 | 57 | <xsl:with-param name="found-element" select="peripherals/mouse[1]"/> |
paulb@30 | 58 | <xsl:with-param name="element-name">mouse</xsl:with-param> |
paulb@30 | 59 | </xsl:call-template> |
paulb@30 | 60 | <xsl:call-template name="complete-element"> |
paulb@55 | 61 | <xsl:with-param name="found-element" select="peripherals/screen[1]"/> |
paulb@30 | 62 | <xsl:with-param name="element-name">screen</xsl:with-param> |
paulb@30 | 63 | </xsl:call-template> |
paulb@30 | 64 | </peripherals> |
paulb@30 | 65 | </xsl:copy> |
paulb@30 | 66 | </xsl:template> |
paulb@30 | 67 | |
paulb@30 | 68 | <xsl:template name="complete-element"> |
paulb@55 | 69 | <!-- Input parameters. --> |
paulb@55 | 70 | <xsl:param name="found-element"/> |
paulb@55 | 71 | <xsl:param name="control-element"/> |
paulb@55 | 72 | <!-- Database parameters. --> |
paulb@30 | 73 | <xsl:param name="element-name"/> |
paulb@55 | 74 | <xsl:param name="control-element-name"/> |
paulb@55 | 75 | <xsl:param name="control-attribute-name"/> |
paulb@55 | 76 | <!-- Find the database entries. --> |
paulb@30 | 77 | <xsl:variable name="element-enum-name"><xsl:value-of select="$element-name"/>-enum</xsl:variable> |
paulb@55 | 78 | <xsl:variable name="control-element-enum-name"><xsl:value-of select="$control-element-name"/>-enum</xsl:variable> |
paulb@55 | 79 | <!-- Find whether the input values are set. --> |
paulb@55 | 80 | <xsl:variable name="has-set-values" select="$found-element[@value]"/> |
paulb@55 | 81 | <xsl:variable name="has-control-values" select="$control-element and $control-element[@value]"/> |
paulb@55 | 82 | <!-- Get the database element for the element being prepared. --> |
paulb@55 | 83 | <xsl:variable name="database-element" select="$database/database/*[local-name() = $element-name]"/> |
paulb@55 | 84 | <!-- Get the database element for the control element. --> |
paulb@55 | 85 | <xsl:variable name="database-control-element" select="$database/database/*[local-name() = $control-element-name]"/> |
paulb@55 | 86 | <!-- Get the database entries for the control element. --> |
paulb@55 | 87 | <xsl:variable name="control-enum-elements" select="$database-control-element/*[local-name() = $control-element-enum-name]"/> |
paulb@55 | 88 | <!-- Get the database entry for the control element, then get the control attribute value in that entry. --> |
paulb@55 | 89 | <xsl:variable name="other-control-value" select="$control-enum-elements[@value = $control-element/@value]/@*[local-name() = $control-attribute-name]"/> |
paulb@30 | 90 | <!-- Make the element with the different values. --> |
paulb@30 | 91 | <xsl:element name="{$element-name}"> |
paulb@55 | 92 | <!-- Find the entries in the database for the element being prepared. --> |
paulb@55 | 93 | <xsl:for-each select="$database-element/*[local-name() = $element-enum-name]"> |
paulb@55 | 94 | <!-- Remember the current node for future comparisons. --> |
paulb@30 | 95 | <xsl:variable name="enum-element" select="."/> |
paulb@55 | 96 | <!-- Find whether the current entry matches the input value. --> |
paulb@55 | 97 | <xsl:variable name="matching-element" select="$found-element[@value = $enum-element/@value]"/> |
paulb@55 | 98 | <!-- Get the control attribute value for the current entry. --> |
paulb@55 | 99 | <xsl:variable name="this-control-value" select="@*[local-name() = $control-attribute-name]"/> |
paulb@55 | 100 | <!-- Filter the entry out unless no control element was given, this entry has no control attribute value or... |
paulb@55 | 101 | if the control attribute value matches the one for the control element. --> |
paulb@55 | 102 | <xsl:if test="not($has-control-values) or not($this-control-value) or $this-control-value = $other-control-value"> |
paulb@55 | 103 | <!-- Copy the entry. --> |
paulb@55 | 104 | <xsl:copy> |
paulb@55 | 105 | <!-- Include all attributes except the price, adding the price only if the item is selected. --> |
paulb@55 | 106 | <xsl:apply-templates select="@*[local-name() != 'price']"/> |
paulb@55 | 107 | <!-- Find whether this item is selected. --> |
paulb@55 | 108 | <xsl:choose> |
paulb@55 | 109 | <!-- In the current field, the value attribute matches this value. --> |
paulb@55 | 110 | <xsl:when test="$matching-element"> |
paulb@55 | 111 | <xsl:attribute name="value-is-set">true</xsl:attribute> |
paulb@55 | 112 | <xsl:apply-templates select="@price"/> |
paulb@55 | 113 | </xsl:when> |
paulb@55 | 114 | <!-- This value is the first and no value attribute is specified. --> |
paulb@55 | 115 | <xsl:when test="not($has-set-values) and count($enum-element/preceding-sibling::*) = 0"> |
paulb@55 | 116 | <xsl:attribute name="value-is-set">true</xsl:attribute> |
paulb@55 | 117 | <xsl:apply-templates select="@price"/> |
paulb@55 | 118 | </xsl:when> |
paulb@55 | 119 | <xsl:otherwise/> |
paulb@55 | 120 | </xsl:choose> |
paulb@55 | 121 | </xsl:copy> |
paulb@55 | 122 | </xsl:if> |
paulb@30 | 123 | </xsl:for-each> |
paulb@30 | 124 | </xsl:element> |
paulb@30 | 125 | </xsl:template> |
paulb@30 | 126 | |
paulb@30 | 127 | <!-- Replicate unknown elements. --> |
paulb@30 | 128 | |
paulb@30 | 129 | <xsl:template match="@*|node()"> |
paulb@30 | 130 | <xsl:copy> |
paulb@30 | 131 | <xsl:apply-templates select="@*|node()"/> |
paulb@30 | 132 | </xsl:copy> |
paulb@30 | 133 | </xsl:template> |
paulb@30 | 134 | |
paulb@30 | 135 | </xsl:stylesheet> |