473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XSL sort/if confusion

6
Hi all,

This is a small issue to make things prettier, but we all know how important that can be!

I had an xsl:if to check if a node was the last one in a collection of nodes and if it was a horizontal divider would not be displayed underneath it. This worked a treat but now I'm trying to sort my data using xsl:sort. The if statement technically still works but now the node identified by last() could be anywhere in the output order. I only want to remove the horizontal divider at the end.

Does anyone have any suggestions on how I can alter my if statement to reflect this? I imagine it is going to have to check that it is the latest year/month/day against every other sibling but I can't figure out how to do this properly.

My sort:
Expand|Select|Wrap|Line Numbers
  1. <xsl:apply-templates select="primarycontent" mode="_1">
  2.   <!-- Break down into components and sort ascending as a number -->
  3.   <xsl:sort data-type="number" select="substring(detaildate,7,4)"/>
  4.   <!-- year  -->
  5.   <xsl:sort data-type="number" select="substring(detaildate,4,2)"/>
  6.   <!-- month -->
  7.   <xsl:sort data-type="number" select="substring(detaildate,1,2)"/>
  8.   <!-- day   -->
  9. </xsl:apply-templates>
My xsl:if that worked before:
Expand|Select|Wrap|Line Numbers
  1. <xsl:if test=".!=/page/primarycontent[last()]">
  2.   <div class="divider2"></div>
  3. </xsl:if>
Any suggestions?
Nov 6 '08 #1
7 5068
Dormilich
8,658 Expert Mod 8TB
idea you could store the node set in a variable and test the variable's last element.... like <xsl:if test="$var[last()]"> (something like that)

regards
Nov 6 '08 #2
otis
6
That sounds interesting Dormilich... how would I store the sorted node set in a variable? I have to be honest, this is my first major foray into manually writing XSLT!
Nov 6 '08 #3
Dormilich
8,658 Expert Mod 8TB
without any testing I'd say
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="..." mode="_1">
  2.   <xsl:variable name="var" select="."/> // change name as desired
  3. // further code here
  4. </xsl:template>
at least that looks sensible to me (the element passed over to the template should be already sorted (that's what I assume after your apply-templates call)).

regards

PS you may have to declare the variable globally
Nov 6 '08 #4
jkmyoung
2,057 Expert 2GB
A couple ideas:
Use a for-each to sort, and then use apply-templates. You could pass in a parameter to see if it's the last one.
eg:

Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="primarycontent"> 
  2.   <!-- Break down into components and sort ascending as a number --> 
  3.   <xsl:sort data-type="number" select="substring(detaildate,7,4)"/> 
  4.   <!-- year  --> 
  5.   <xsl:sort data-type="number" select="substring(detaildate,4,2)"/> 
  6.   <!-- month --> 
  7.   <xsl:sort data-type="number" select="substring(detaildate,1,2)"/> 
  8.   <!-- day   --> 
  9.   <xsl:apply-templates select="." mode="_1">
  10.     <xsl:with-param name="last" select=".=last()"/>
  11.   </xsl:apply-templates> 
  12. </xsl:for-each>
  13. ...
  14. ...
  15. <xsl:template match="primarycontent" mode="_1">
  16.   <xsl:param name="last">
  17.  
Nov 6 '08 #5
otis
6
idea you could store the node set in a variable and test the variable's last element.... like <xsl:if test="$var[last()]"> (something like that)

regards
Unfortunately, my attempt at this meant every single one thought that it was $var[last()] because I had to put the variable declaration inside the xsl:template for the repeating group that has been sorted... so "." is a single node rather that the sorted group. I also tried "./..".

With the way it is sorted and that fact that xsl:variable is not valid within xsl:apply-templates I don't see another place to put the xsl:variable where it would be effective. Am I being really useless on this one?

jkmyoung, I don't understand your suggestion but I'll give it a try!
Nov 6 '08 #6
jkmyoung
2,057 Expert 2GB
Rechecked the concept. You don't even need a parameter.
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="your nodes...">
  2.   <xsl:sort select="..."/>
  3.   <xsl:sort select="..."/>
  4.   <xsl:sort select="..."/>
  5.   <xsl:apply-templates select="."/>
  6. </xsl:for-each>
  7.  
  8. <xsl:template match="nodes">
  9. ...
  10.   <xsl:if test="position() != last()">
  11.      display your horizontal divider
  12.   </xsl:if>
  13.  
  14.  
The for-each automatically reorders the nodes with the sorting. Then you can just check if the node is the last one the same way that you used to.
Nov 6 '08 #7
otis
6
Unfortunately when I try that position() and last() are always equal to 1 because you're only passing a single node to the apply-templates request.

However, I moved the if statement into the for-each as shown below and it worked a treat!

Thanks for your help guys.

Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="primarycontent">
  2.   <!-- Break date down into components and sort ascending as a number -->
  3.   <xsl:sort data-type="number" select="substring(detaildate,7,4)"/>
  4.   <!-- year  -->
  5.   <xsl:sort data-type="number" select="substring(detaildate,4,2)"/>
  6.   <!-- month -->
  7.   <xsl:sort data-type="number" select="substring(detaildate,1,2)"/>
  8.   <!-- day   -->
  9.   <xsl:apply-templates select="." mode="_1" />
  10.   <!-- Add a conditional test to hide the divider for the last entry -->
  11.   <xsl:if test="position() != last()">
  12.     <div class="divider2"></div>
  13.   </xsl:if>
  14. </xsl:for-each>
Nov 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
6
by: UDBDBA | last post by:
All: We are running UDB V8.2 FP8. We have sql query which uses DGTT. The access plan for the query changes based on rows selected into DGTT. 1. Secnario I: DGTT has atlest 1 row. I see INDEX...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
5
by: TM | last post by:
I am using an access database in my vb.net application and it is tied to a datagrid. My problem is that the field I want to sort on is a text field, 5 characters long, and it contains not only...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
2
by: siromega | last post by:
Hello all, I'm having a problem with XSL:Sort. I've parameterized it (and use javascript to populate the parameters, but that part is working fine).. <xsl:sort select="*" /> The problem...
6
by: Julian | last post by:
Hi, I am a very beginner in databases. I created a database table in Access 2003 and OOo 2.03 that includes name, address, postcode, phone numbers etc of our customers. I would like to sort...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.