paulb@137 | 1 | <?xml version="1.0" encoding="utf-8"?> |
paulb@137 | 2 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
paulb@137 | 3 | xmlns:template="http://www.boddie.org.uk/ns/xmltools/template" |
paulb@137 | 4 | version="1.0"> |
paulb@137 | 5 | |
paulb@137 | 6 | <xsl:param name="types"/> |
paulb@137 | 7 | |
paulb@137 | 8 | <!-- Add or process type elements inside item elements. --> |
paulb@137 | 9 | |
paulb@137 | 10 | <xsl:template match="item"> |
paulb@137 | 11 | <!-- Copy the element and its contents. --> |
paulb@137 | 12 | <xsl:copy> |
paulb@137 | 13 | <xsl:apply-templates select="@*"/> |
paulb@137 | 14 | <!-- Look for a type element. --> |
paulb@137 | 15 | <xsl:choose> |
paulb@137 | 16 | <xsl:when test="types"> |
paulb@137 | 17 | <!-- Use the existing types element. --> |
paulb@137 | 18 | <xsl:apply-templates select="types"/> |
paulb@137 | 19 | </xsl:when> |
paulb@137 | 20 | <xsl:otherwise> |
paulb@137 | 21 | <!-- Add a types element if none was found. --> |
paulb@137 | 22 | <xsl:call-template name="types"/> |
paulb@137 | 23 | </xsl:otherwise> |
paulb@137 | 24 | </xsl:choose> |
paulb@137 | 25 | <xsl:apply-templates select="*[local-name() != 'types']"/> |
paulb@137 | 26 | </xsl:copy> |
paulb@137 | 27 | </xsl:template> |
paulb@137 | 28 | |
paulb@137 | 29 | <!-- Investigate types elements. --> |
paulb@137 | 30 | |
paulb@137 | 31 | <xsl:template name="types"> |
paulb@137 | 32 | <!-- Make the element. --> |
paulb@137 | 33 | <types> |
paulb@137 | 34 | <xsl:call-template name="type"/> |
paulb@137 | 35 | </types> |
paulb@137 | 36 | </xsl:template> |
paulb@137 | 37 | |
paulb@137 | 38 | <xsl:template match="types"> |
paulb@137 | 39 | <!-- Copy the element. --> |
paulb@137 | 40 | <xsl:copy> |
paulb@137 | 41 | <xsl:apply-templates select="@*"/> |
paulb@137 | 42 | <!-- Look for a type element. --> |
paulb@137 | 43 | <xsl:choose> |
paulb@137 | 44 | <xsl:when test="type"> |
paulb@137 | 45 | <!-- Use the existing type element. --> |
paulb@137 | 46 | <xsl:apply-templates select="type"/> |
paulb@137 | 47 | </xsl:when> |
paulb@137 | 48 | <xsl:otherwise> |
paulb@137 | 49 | <!-- Add a type element if none was found. --> |
paulb@137 | 50 | <xsl:call-template name="type"/> |
paulb@137 | 51 | </xsl:otherwise> |
paulb@137 | 52 | </xsl:choose> |
paulb@137 | 53 | <xsl:apply-templates select="*[local-name() != 'type']"/> |
paulb@137 | 54 | </xsl:copy> |
paulb@137 | 55 | </xsl:template> |
paulb@137 | 56 | |
paulb@137 | 57 | <!-- Investigate type elements. --> |
paulb@137 | 58 | |
paulb@137 | 59 | <xsl:template name="type"> |
paulb@137 | 60 | <!-- Make the element. --> |
paulb@137 | 61 | <type> |
paulb@137 | 62 | <xsl:call-template name="type-enum"/> |
paulb@137 | 63 | </type> |
paulb@137 | 64 | </xsl:template> |
paulb@137 | 65 | |
paulb@137 | 66 | <xsl:template match="type"> |
paulb@137 | 67 | <!-- Copy the element. --> |
paulb@137 | 68 | <xsl:copy> |
paulb@137 | 69 | <xsl:apply-templates select="@*"/> |
paulb@137 | 70 | <xsl:call-template name="type-enum"> |
paulb@137 | 71 | <!-- Use the value of the element. --> |
paulb@137 | 72 | <xsl:with-param name="value" select="@value"/> |
paulb@137 | 73 | </xsl:call-template> |
paulb@137 | 74 | </xsl:copy> |
paulb@137 | 75 | </xsl:template> |
paulb@137 | 76 | |
paulb@137 | 77 | <!-- Add to type elements the range of acceptable values. --> |
paulb@137 | 78 | |
paulb@137 | 79 | <xsl:template name="type-enum"> |
paulb@137 | 80 | <xsl:param name="value"/> |
paulb@137 | 81 | <!-- Find all values in the range and add them. --> |
paulb@137 | 82 | <xsl:for-each select="$types/types/type-enum"> |
paulb@137 | 83 | <xsl:copy> |
paulb@137 | 84 | <xsl:apply-templates select="@*"/> |
paulb@137 | 85 | <!-- Mark those whose value are the same as that found on the element. --> |
paulb@137 | 86 | <xsl:if test="$value = @value"> |
paulb@137 | 87 | <xsl:attribute name="value-is-set">true</xsl:attribute> |
paulb@137 | 88 | </xsl:if> |
paulb@137 | 89 | </xsl:copy> |
paulb@137 | 90 | </xsl:for-each> |
paulb@137 | 91 | </xsl:template> |
paulb@137 | 92 | |
paulb@137 | 93 | <!-- Replicate unknown elements. --> |
paulb@137 | 94 | |
paulb@137 | 95 | <xsl:template match="@*|*|node()"> |
paulb@137 | 96 | <xsl:copy> |
paulb@137 | 97 | <xsl:apply-templates select="@*|*|node()"/> |
paulb@137 | 98 | </xsl:copy> |
paulb@137 | 99 | </xsl:template> |
paulb@137 | 100 | |
paulb@137 | 101 | </xsl:stylesheet> |