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 like to store them in a variable.
I was trying to do the following, but it doesn't seem to work
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<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:for-each>
</xsl:variable>
I was trying to sort the elements by @DepositDate
in the format 'yyyy-mm-dd' and store them all in the $deposits
variable. So that later, I could access them using $deposits[1]
.
I would appreciate any help and tips!
Thanks a lot!
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