How to sort items and store them in an XSLT variable

I was wondering if it is possible to sort some elements first and save them (already sorted) in a variable. I will need to access them, XSLT thought, why I would like to store them in a variable.

I tried 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 tried to sort the elements @DepositDatein the format "yyyy-mm-dd" and save them all in a variable $deposits. So later I could access them using $deposits[1].

I would be grateful for any help and advice!

Thank you so much!

+3
source share
3 answers

-, - . , , . , - xsl: copy.

<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> 

"node -set", XSLT. , XSLT-. , , Microsoft .

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" version="1.0"> 

, , -

<xsl:value-of select="ms:node-set($deposits)/MultiDeposits[1]/@DepositDate" />

node -sets

Xml.com node -Sets

+3
  • XSLT version 2.0, perform-sort , MultiDeposits as keyword (as="element(MultiDeposits)+ ")
  • yyyy-mm-dd,

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>

XSLT 2.0:

<?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>

:

first date:1998-01-01, last date:2010-08-09
+4

Guess (you don't have dev env to hand):

Add <xsl:value-of select="." />

Before closing </xsl:for-each>

0
source

Source: https://habr.com/ru/post/1732984/


All Articles