Re: How to Reorder Items & Number Them Sequentially?
Bill, >What I'd like to know is: how I can create this ordering of the >output and retain the ability to number the items sequentially using >XSLT? I have figured out a way to reorder the elements by using >nested for-each loops, but there appears to be no way to get >sequential numbering when I do it that way. I'm sure there must be >a functional approach that makes this easy. Here are a couple of templates that give you the ordering, grouping and numbering that you want. They are not pretty. It works by identifying, from the current node, which node should be next in the output list, and then operating on that. Let me know if you need it explained further. <xsl:key name="animalTypes" match="animal" use="@type" /> <xsl:template match="animals"> <doc> Here are my pets: <xsl:apply-templates select="animal"> <xsl:with-param name="number">1</xsl:with-param> </xsl:apply-templates> </doc> </xsl:template> <xsl:template match="animal"> <xsl:param name="number" /> <xsl:variable name="type" select="@type" /> <xsl:value-of select="$number" />. <xsl:value-of select="@type" /> (<xsl:value-of select="@name" />) <xsl:choose> <!-- this isn't the last of this type in the document --> <xsl:when test="generate-id(.) != generate-id(key('animalTypes', $type)[last()])"> <xsl:apply-templates select="following-sibling::animal[@type = $type]"> <xsl:with-param name="number" select="$number + 1" /> </xsl:apply-templates> </xsl:when> <!-- move onto the next in order --> <xsl:otherwise> <xsl:apply-templates select="key('animalTypes', $type)/following-sibling::animal[generate-id(.) = generate-id(key('animalTypes', @type))]"> <xsl:with-param name="number" select="$number + 1" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> I hope that helps. Cheers, Jeni Dr Jeni Tennison Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE Telephone 0115 9061301 ? Fax 0115 9061304 ? Email jeni.tennison@xxxxxxxxxxxxxxxx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format