Jump to content

Problems sorting value in currency format <xsl:sort>


alexandreteles

Recommended Posts

Hi,I´m having a problem to sort the output in currency format. I have a currency in the xml file, that has the format (#.###,00). The result of the transformation is not correct because the XSLT recognize the tag <amount> as string. I tried the element data-type=”number”, but the XSLT results NaN (not a number), because of the comma(,). Does anyone can help me?My XML is:

<import><via><name>Air</name>	<amount>183.852,40</amount>< name >Sea</ name >	<amount>283.852,50</amount>< name >Terrestrial</ name >		<amount>5.885,90</amount></via></import>

My xsl file is:

<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="import">		<xsl:apply-templates select="via">		<xsl:sort select="amount"/>		</xsl:apply-templates>	</xsl:template>	<xsl:template match="via">			<tr>				<td>					<xsl:value-of select="name" />				</td>				<td>					<xsl:value-of select="amount" />				</td>			</tr>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

I converted your non-standard number value into a number by first reversing the decimal and thousands separator, than removed the thousands separator. This allowed the values to be sorted as numbers. Give this a try

<xsl:apply-templates select="via">	  <xsl:sort select="translate(translate(amount,'.,',',.'),',','')" data-type="number" /></xsl:apply-templates>

The inside translate does the switch of separators. The ouside one removed the thousands separtor so that it can be interpreted as a numberHope this helps

Link to comment
Share on other sites

I converted your non-standard number value into a number by first reversing the decimal and thousands separator, than removed the thousands separator. This allowed the values to be sorted as numbers. Give this a try
<xsl:apply-templates select="via">	  <xsl:sort select="translate(translate(amount,'.,',',.'),',','')" data-type="number" /></xsl:apply-templates>

The inside translate does the switch of separators. The ouside one removed the thousands separtor so that it can be interpreted as a numberHope this helps

Translation inside a translation. Wow, that's a neat trick. Never though about it. It's times like this I wish I could bookmark a topic in my account for future reference :) .
Link to comment
Share on other sites

I converted your non-standard number value into a number by first reversing the decimal and thousands separator, than removed the thousands separator. This allowed the values to be sorted as numbers. Give this a try
<xsl:apply-templates select="via">	  <xsl:sort select="translate(translate(amount,'.,',',.'),',','')" data-type="number" /></xsl:apply-templates>

The inside translate does the switch of separators. The ouside one removed the thousands separtor so that it can be interpreted as a numberHope this helps

thanks. I appreciate the help. Nice trick.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...