Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL Sort

Reply
Thread Tools

XSL Sort

 
 
aerotops
Guest
Posts: n/a
 
      11-29-2005
Hi,
I am trying to sort something using XSLT. I am going to give examples.

Original.xml

<Root>

<Car>
<Name>Ford</Name>
<DealerRating>3</DealerRating>
<MyRating>1</MyRating>
</Car>
<Car>
<Name>Honda</Name>
<DealerRating>4</DealerRating>
<MyRating>5</MyRating>
</Car>
<Car>
<Name>Nissan</Name>
<DealerRating>7</DealerRating>
<MyRating>3</MyRating>
</Car>
</Root>

Note:
1. The number of cars is limited to 3.
2. <DealerRating> has a higher priority than <MyRating> as long as DR>0
(Not 0). For example, if DR=3 and MR=2, then rating =3. Or if DR=0
and MR=1, now however, rating =1 because DR=0.
3. Apply some XSLT to Original.xml to get the following Result.xml

Result.xml

<Root>

<Car name1="Ford" rating1="1" name2="Nissan" rating2="3"
name3="Honda" rating3="5"/>

</Root>

As you can see, the attributes are generated by sorting the <Car>
elements in Original.xml in ascending order of rating as described
above.
Right now, I am doing this using dom4j. I would like to do this in
XSLT.


Note: Don't worry about the XSLT logic which compares <MyRating> and
<DealerRating> and then gives me a rating. I have already done that.

My problem is that I would like to sort on the rating1, rating2 and
rating3 attributes.

Some pseudocode for my XSLT sorting part (Right now, I parse the
Original.xml file using dom4j. Then, I add a <rating> element to the
Original.xml after deciding which rating to use in each <Car> element.
I don't want to change the Original.xml by inserting my rating
attribute.):


<xsl:for-each select="Car">

<!-- <xsl:sort> on <rating> which I get from my Java code

<ifosition()=1>
Then write name1="XXX" and rating1="XXX"
</if>
<if position()=2>
Then write name2="XXX" and rating2="XXX"
</if>
<if position()=3>
Then write name3="XXX" and rating3="XXX"
</if>
</xsl:for-each>




Any suggestions would be appreciated.
Thanks,
Santa.

 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      11-29-2005
aerotops wrote:

> Hi,
> I am trying to sort something using XSLT. I am going to give examples.
>
> Original.xml
>
> <Root>
>
> <Car>
> <Name>Ford</Name>
> <DealerRating>3</DealerRating>
> <MyRating>1</MyRating>
> </Car>
> <Car>
> <Name>Honda</Name>
> <DealerRating>4</DealerRating>
> <MyRating>5</MyRating>
> </Car>
> <Car>
> <Name>Nissan</Name>
> <DealerRating>7</DealerRating>
> <MyRating>3</MyRating>
> </Car>
> </Root>
>
> Note:
> 1. The number of cars is limited to 3.
> 2. <DealerRating> has a higher priority than <MyRating> as long as
> DR>0
> (Not 0).
> For example, if DR=3 and MR=2, then rating =3.


But what if DR=2 and MR=3? Is the rating then 3 or 2?

> Or if DR=0
> and MR=1, now however, rating =1 because DR=0.
> 3. Apply some XSLT to Original.xml to get the following Result.xml
>
> Result.xml
>
> <Root>
>
> <Car name1="Ford" rating1="1" name2="Nissan" rating2="3"
> name3="Honda" rating3="5"/>
>
> </Root>
>
> As you can see, the attributes are generated by sorting the <Car>
> elements in Original.xml in ascending order of rating as described
> above.
> Right now, I am doing this using dom4j. I would like to do this in
> XSLT.
>
>
> Note: Don't worry about the XSLT logic which compares <MyRating> and
> <DealerRating> and then gives me a rating. I have already done that.
>
> My problem is that I would like to sort on the rating1, rating2 and
> rating3 attributes.
>
> Some pseudocode for my XSLT sorting part (Right now, I parse the
> Original.xml file using dom4j. Then, I add a <rating> element to the
> Original.xml after deciding which rating to use in each <Car> element.
> I don't want to change the Original.xml by inserting my rating
> attribute.):
>
>
> <xsl:for-each select="Car">
>
> <!-- <xsl:sort> on <rating> which I get from my Java code
>
> <ifosition()=1>
> Then write name1="XXX" and rating1="XXX"


<xsl:attribute name="name1">
<xsl:value-of select="Name"/>
</xsl:attribute>
<xsl:attribute name="rating1">
<xsl:value-of select="rating"/>
</xsl:attribute>

Presumably you have generated a composite value for <rating> somewhere.

You can find more help on the XSL List (http://www.mulberrytech.com)

///Peter
--
XML FAQ: http://xml.silmaril.ie/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:sort using an xsl:variable as the sort key jobooker@gmail.com XML 2 09-05-2006 03:51 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 0 09-08-2004 02:11 PM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments