I was wondering whether it\'s possible to sort some elements first and store them (already sorted) in a variable. I would need to refer to them thought XSLT that\'s why I\'d
XSLT version 2.0
you could use perform-sort
and tell that your variable is of type of a sequence of MultiDeposits
using the as keyword
(as="element(MultiDeposits)+
")with this sample xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<BookingCostings>
<MultiDeposits depositDate="2001-10-09">1</MultiDeposits>
<MultiDeposits depositDate="1999-10-09">2</MultiDeposits>
<MultiDeposits depositDate="2010-08-09">3</MultiDeposits>
<MultiDeposits depositDate="2010-07-09">4</MultiDeposits>
<MultiDeposits depositDate="1998-01-01">5</MultiDeposits>
</BookingCostings>
and using the XSLT version 2.0 sheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:variable name="deposits" as="element(MultiDeposits)+">
<xsl:perform-sort select="BookingCostings/MultiDeposits">
<xsl:sort select="@depositDate"/>
</xsl:perform-sort>
</xsl:variable>
first date:<xsl:value-of select="$deposits[1]/@depositDate"/>,
last date:<xsl:value-of select="$deposits[last()]/@depositDate"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
the ouput will be:
first date:1998-01-01, last date:2010-08-09
Firstly, in your variable declaration, you do need to do something to create new nodes. Strictly speaking, you are not sorting them, but just reading through them in a given order. I think you need to add some sort of xsl:copy command.
<xsl:variable name="deposits">
<xsl:for-each select="/BookingCostings/MultiDeposits">
<xsl:sort select="substring(@DepositDate, 1, 4)" />
<xsl:sort select="substring(@DepositDate, 6, 2)" />
<xsl:sort select="substring(@DepositDate, 9, 2)" />
<xsl:copy-of select=".|@*" />
</xsl:for-each>
</xsl:variable>
What this creates is a 'node-set', but to access it you will need to make use of an extension function in XSLT. Which one you use depends on the XSLT processor you are using. In the example I am about to give, I am using the Microsoft one.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" version="1.0">
Then, to access the nodes in your variable, you can do something like this
<xsl:value-of select="ms:node-set($deposits)/MultiDeposits[1]/@DepositDate" />
Here is a good article to read up on node-sets
Xml.com article on Node-Sets
Guess (don't have dev env to hand):
Add
<xsl:value-of select="." />
Before the closing </xsl:for-each>