This is the mail archive of the mailing list .

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: XSLT: Sort Question

Thanks Peter for all the good info. 
I adjusted my XLST and it did infact work!

-----Original Message-----
From: Peter Davis []
Sent: Tuesday, February 12, 2002 4:05 PM
Subject: Re: [xsl] XSLT: Sort Question

On Tuesday 12 February 2002 15:00, Ragan, Mitch wrote:
> Need Sort Help
> Can't get to sort by quote, if I put "@date" it works fine, but "Quote"
> does nothing.

You have:
<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]" mode="Totals">
  <xsl:sort select="//Quotes/Quote" data-type="number" order="descending"/>

The select attribute of sort is evaluated with each node selected by the 
apply-templates call as the context node and converted to a string as if
the string() function, and then used as the key for the sort.  Your sort key

in effect is the result of string(//Quotes/Quote), which is the same for 
every Quote node, and thus the sort has no effect.

The select attribute should be relative to each node selected by the 
apply-templates call.  So if you want to sort on the price, you would do:

<xsl:sort select="." data-type="number" order="descending"/>

The "." tells sort to use the value of the current node as the sort key, and

in this case that would be the value of each Quote object.  If you want to 
sort on the symbol (I know you don't but just for the sake of argument), you

would do:

<xsl:sort select="@symbol"/>

Also, it's generally considered bad practice to use "//" in your XPath 
expressions, and you should possibly consider redesigning your templates to 
explicitely match the desired Quote objects.  For example, you could do 
<xsl:apply-templates select="/Quotes/Quote" .../>

> <xsl:template name="QuoteTotals">
> 	 <xsl:param name="CurrentSymbol" select =
> "Symbol[position()=current()]"/>
> 		<!--  Gets Current Symbol being worked on from above code,
> not shown-->
> 	<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]"
> mode="Totals">
> 		 <xsl:sort select="//Quotes/Quote" data-type="number"
> order="descending"/>
> 	</xsl:apply-templates>
> </xsl:template>
> <xsl:template match="Quote"  mode="Totals">
>  	<h3>
> 		<xsl:value-of select="."/>
> 	</h3>
> </xsl:template>
> XML:
>   <Quotes>
>     <Quote symbol="TK" date="2001-09-30">28.40</Quote>
>     <Quote symbol="TK" date="2001-10-24">26.95</Quote>
>     <Quote symbol="TK" date="2001-08-07">30</Quote>
>     <Quote symbol="TK" date="2001-11-11">31.125</Quote>
>     <Quote symbol="TK" date="2002-02-15">38.875</Quote>
>     <Quote symbol="MCDTA" date="2001-09-15">12.30</Quote>
>     <Quote symbol="MCDTA" date="2001-10-15">14.95</Quote>
>     <Quote symbol="MCDTA" date="2001-11-15">16.00</Quote>
>     <Quote symbol="MCDTA" date="2001-12-15">24.5</Quote>
>     <Quote symbol="MCDTA" date="2002-02-15">28.0</Quote>
>     <Quote symbol="MSFT" date="2001-09-01">53.50</Quote>
>     <Quote symbol="MSFT" date="2002-02-15">65.25</Quote>
>   </Quotes>
>   Mitch K. Ragan
>     Boeing Commercial Aircraft Group
>     Global Electronic Commerce
>     Senior Systems Analyst
>     Primary: 425-266-3155
>     Secondary: 425-237-1735
>  XSL-List info and archive:

Peter Davis
Elephant, n.:
	A mouse built to government specifications.

 XSL-List info and archive:

 XSL-List info and archive:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]