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="type"> 17 <!-- Use the existing type element. --> 18 <xsl:apply-templates select="type"/> 19 </xsl:when> 20 <xsl:otherwise> 21 <!-- Add a type element if none was found. --> 22 <xsl:call-template name="type"/> 23 </xsl:otherwise> 24 </xsl:choose> 25 <xsl:apply-templates select="*[local-name() != 'type']"/> 26 </xsl:copy> 27 </xsl:template> 28 29 <!-- Investigate type elements. --> 30 31 <xsl:template name="type"> 32 <!-- Make the element. --> 33 <type> 34 <xsl:call-template name="type-enum"/> 35 </type> 36 </xsl:template> 37 38 <xsl:template match="type"> 39 <!-- Copy the element. --> 40 <xsl:copy> 41 <xsl:apply-templates select="@*"/> 42 <xsl:call-template name="type-enum"> 43 <!-- Use the value of the element. --> 44 <xsl:with-param name="value" select="@value"/> 45 </xsl:call-template> 46 </xsl:copy> 47 </xsl:template> 48 49 <!-- Add to type elements the range of acceptable values. --> 50 51 <xsl:template name="type-enum"> 52 <xsl:param name="value"/> 53 <!-- Find all values in the range and add them. --> 54 <xsl:for-each select="$types/types/type-enum"> 55 <xsl:copy> 56 <xsl:apply-templates select="@*"/> 57 </xsl:copy> 58 </xsl:for-each> 59 </xsl:template> 60 61 <!-- Replicate unknown elements. --> 62 63 <xsl:template match="@*|*|node()"> 64 <xsl:copy> 65 <xsl:apply-templates select="@*|*|node()"/> 66 </xsl:copy> 67 </xsl:template> 68 69 </xsl:stylesheet>