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