XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
ian mcdonaldSubject: ordering by two elements with .net 2.0 and xsl
Author: ian mcdonald
Date: 07 Jun 2007 07:53 PM
Hello,

I've been bashing my head against the wall with this one, because I've tried things which should work but don't.

I need to order an xml file of structure to be shown below using xsl with .net. My problem lies with the fact that I need to order by city a-z and within each city price lowest to highest.

Here is an example section of xml:
<listings>
<listing>
<mls>935655</mls> <imageLink>http://trianglepictures.marketlinx.com/LowRes/55/935655_0.jpg</imageLink>
<description>Exquisite home with no detail over looked!</description>
<price>649,900</price>
<saved>
</saved>
<openHouse>0</openHouse>
<featuredHouse>1</featuredHouse>
<soldHouse>0</soldHouse>
<city>Cary</city>
<zipCode>27519</zipCode>
<neighborhood>The Reserve</neighborhood>
<totalRooms>11</totalRooms>
<bedrooms>5</bedrooms>
<fullBaths>4</fullBaths>
<halfBaths>1</halfBaths>
<totalSqFt>4478</totalSqFt>
<unfinishedSqFt>0</unfinishedSqFt>
<style>Traditional/Transitional</style>
<roof>Shingle</roof>
<diningRoom>Large formal dining room plus breakfst room</diningRoom>
<fireplace>Gas log fireplace in living room</fireplace>
<basement>Finished daylight basement with inside and outside entries</basement>
<washDryLocation>Second floor utility room</washDryLocation>
<lotDescription>Hardwood trees and wooded</lotDescription>
<flooring>Carpet, hardwood and tile</flooring>
<yearBuilt>2001</yearBuilt>
<acres>0.52</acres>
<waterSewer>city, both</waterSewer>
<shortDescription>Exquisite home with no detail over looked!</shortDescription>
<address>202 Briardale Ave.</address>
</listing>
</listings>

And I'll just attach the XSL I'm using. It will allow sorting by either city OR price but not both.. Multiple xsl:sort seems to have no effect. Keep in mind that I am using xsltransform() in dot net 2.0 and dumping the result into a literal control...

I would really appreciate any help with this..

Ian


UnknownallListings1.xsl
XSL file

Postnext
Ivan PedruzziSubject: ordering by two elements with .net 2.0 and xsl
Author: Ivan Pedruzzi
Date: 07 Jun 2007 11:04 PM
Ian,

The following shows how to do sorting on two elements

Hope this helps
Ivan

XML:

<?xml version="1.0"?>
<listings>
<listing>
<city>New York</city>
<price>200</price>
</listing>
<listing>
<city>Boston</city>
<price>200</price>
</listing>
<listing>
<city>New York</city>
<price>100</price>
</listing>
<listing>
<city>Boston</city>
<price>100</price>
</listing>
</listings>

XSLT


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<result>
<xsl:for-each select="/listings/listing">
<xsl:sort select="city"/>
<xsl:sort select="price"/>
<item city="{city}" price="{price}"/>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>


result

<result>
<item city="Boston" price="100"/>
<item city="Boston" price="200"/>
<item city="New York" price="100"/>
<item city="New York" price="200"/>
</result>

Postnext
ian mcdonaldSubject: ordering by two elements with .net 2.0 and xsl
Author: ian mcdonald
Date: 08 Jun 2007 12:02 PM
Thank you very much! It worked perfectly!

<xsl:sort select="city"/>
<xsl:sort select="substring-before(price, ',')"/>

I do have two additional questions:

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

2) What statement would ignore everything but numbers for the purpose of sorting in the following example: $1,000,000

I have used substring-before() with success but there are some numbers this will not work on, so it is better to do it the right way..

Thank you for your help.

ian

Postnext
Tony LavinioSubject: 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.

Postnext
ian mcdonaldSubject: 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

Postnext
Ivan PedruzziSubject: 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>

Posttop
ian mcdonaldSubject: ordering by two elements with .net 2.0 and xsl
Author: ian mcdonald
Date: 14 Jun 2007 10:24 PM
Thank you all!

I appreciate your help very much.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.