Advertisement

05.11.2005 at 06:09PM PDT, ID: 21421312
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Problem with xsl:sort and apply-templates

Asked by cklanac in Extensible Markup Language (XML)

Tags: , , ,


I simply need to select the most recent list of item (by date). The XML contains
multiple lists, in any order and will contain "future" items. The dates are converted
to UTC basic format (CCYYMMDD) so they can be treated as ordinary numbers. So the
logic is essentially, sort the list by "date", then select the last() item
where date is less than "now".

I can only get it to work if I do it in two steps (See Method #2 below). But this
means that each list node needs to be tested. Does this create any additional
overhead? Can it be done in one step?

<schedule>
   <list date="20041231">
      <item title="Ancient #1"/>
      <item title="Ancient #2"/>
      <item title="Ancient #3"/>
   </list>

   <list date="20050515">
      <item title="Future #1"/>
      <item title="Future #2"/>
      <item title="Future #3"/>
   </list>
   
   <list date="20050507">
      <item title="Current #1"/>
      <item title="Current #2"/>
      <item title="Current #3"/>
   </list>

   <list date="20050505">
      <item title="Recent #1"/>
      <item title="Recent #2"/>
      <item title="Recent #3"/>
   </list>
</schedule>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:param name="now">20050511</xsl:param>

   <xsl:template match="//schedule">

      Method #1 Doesn't work because the sort happens after the select<br/>
      <xsl:apply-templates select="list[@date &lt; $now][last()]/item">
         <xsl:sort select="@date"/>
      </xsl:apply-templates>

      <hr/>

      Method #2 Works: but it needs two steps. Can it be done together?<br/>
      <xsl:apply-templates select="list[@date &lt; $now]">
         <xsl:sort select="@date"/>
      </xsl:apply-templates>

   </xsl:template>

   <xsl:template match="list">
      Test if current node is last().
      <xsl:if test="position() = last()">
         <xsl:apply-templates select="item"/>
      </xsl:if>
   </xsl:template>

   <xsl:template match="item">
      <xsl:value-of select="@title"/><br/>
   </xsl:template>

</xsl:stylesheet>Start Free Trial
[+][-]05.11.2005 at 06:32PM PDT, ID: 13983340

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Extensible Markup Language (XML)
Tags: current, last, position, sort
Sign Up Now!
Solution Provided By: b1xml2
Participating Experts: 1
Solution Grade: B
 
 
[+][-]05.12.2005 at 05:23AM PDT, ID: 13985324

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32