|
next
|
 Subject: ordering by two elements with .net 2.0 and xsl Author: Tony Lavinio Date: 13 Jun 2007 09:25 AM
|
> 1) Why exactly does the former work and the following not?
> <xsl:sort select="substring-before(price, ',')" order="ascending" data-type="number" />
> <xsl:sort select="city" order="ascending" data-type="text" />
a. You had your xsl:sort's backwards; you order them from major criteria
to minor, not minor to major.
b. substring-before will return "" if the separator character is not
found (per http://www.w3.org/TR/xpath#function-substring-before ), so
you were comparing "" to "".
> 2) What statement would ignore everything but numbers for the purpose of sorting in the following example: $1,000,000
translate(., translate(., '0123456789', ''), '')
The inner 'translate' removes all of the characters you want to keep,
and passes the resulting string (which now contains only characters
you DON'T want) to the outer 'translate', which them removes them from
the a copy of the same original string. What's left is just the
characters you care about, which in this case is the digits.
If you want to preserve the decimal, you can add '.' or ',' to the list.
But remember to translate the result so that when xsl:sort sees it, the
decimal uses a '.' and not a ','. See
http://www.w3.org/TR/xpath#NT-Number for the number format.
|
next
|
 Subject: ordering by two elements with .net 2.0 and xsl Author: ian mcdonald Date: 14 Jun 2007 11:59 AM
|
My last question is how would I use the translate() example with
<xsl:sort select="substring-before(price, ',')"/>
in place of the substring-before()...
I *know* this is a stupid question, but I can't figure out where to place the reference to the price element (contains the value to be translated) in the translate function...
I tried this:
<xsl:sort select="translate(., translate(price, '0123456789', ''), '')"/>
and this:
<xsl:sort select="translate(price, translate(., '0123456789', ''), '')"/>
but they broke the sort...so I am doing something wrong.
However
<xsl:sort select="translate(price, translate(price, '0123456789', ''), '')"/>
operates as it did before, placing the value $1,300,000 in the incorrect order.
Here is the output for example: (this page is typically loaded in a smaller window, this is why some things seem improperly sized)
http://www.concept2001online.com/allListings.aspx
Thanks in advance..
ian
|
next
|
 Subject: ordering by two elements with .net 2.0 and xsl Author: Ivan Pedruzzi Date: 14 Jun 2007 12:28 PM Originally Posted: 14 Jun 2007 12:25 PM
|
<xsl:sort select="translate(price, translate(price, '0123456789', ''), '')" order="ascending" data-type="number"/>
Example
---------------------------------------------------------
<?xml version="1.0"?>
<listings>
<listing>
<price>$1,200,000</price>
</listing>
<listing>
<price>500,000</price>
</listing>
<listing>
<price>$2,400,000</price>
</listing>
<listing>
<price>$100</price>
</listing>
</listings>
---------------------------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<result>
<xsl:for-each select="/listings/listing">
<xsl:sort select="translate(price, translate(price, '0123456789', ''), '')" order="ascending" data-type="number"/>
<item price="{price}" a="{translate(price, translate(price, '0123456789', ''), '')}"/>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
---------------------------------------------------------
<?xml version='1.0' ?>
<result>
<item price="$100" a="100"/>
<item price="500,000" a="500000"/>
<item price="$1,200,000" a="1200000"/>
<item price="$2,400,000" a="2400000"/>
</result>
|
|
|
|