BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
In case you need a ""break"" from an xsl:for-each loop
Format XML/XSL for Documentation
Find all xsl:variables that are in scope?
Binary Search in XSLT
Get the newest 5 articles out of hundreds -- a super efficient sort in XSLT
getTemplate
getProcessor
Dynamically change the encoding of your XSLT stylesheet
Identifying an attribute or a namespace node
Build a XPath expression for a node
Avoiding an XSLT Processor crash due to deep recursive processing
Conditional generation of a string in a single XPath expression
Re: Re: ABS function implemented as a single XPath expression
Re: ABS function implemented as a single XPath expression
ABS function implemented as a single XPath expression
XML to HTML text editor
Dynamically Selecting Which Element To Sort On Using Parameters
Avoid NaN in XPath sum() function
Generic JScript Function To Load XML Data
JScript Super Class To Handle XML Transformations


 
 

<< XQuery.NET and XML >>


By Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First Posted 02/27/2001
Times viewed 1106

Sorting on Dates and other Fixed Formats using the substring Function


Summary The Substring function can be very powerful when sorting especially with time and date formats.

Substring Function format
substring(value, start, length)

Characters within a string are numbered 1, 2, 3 ... n.
Michael Kay XSLT Programmers Reference
Let's look at a few examples:
First lets look at a date in the format
mm/dd/yyyy
mm/dd/yyyy
<xsl:sort order=ascending select=substring(text(), 7,4) />
<xsl:sort order=ascending select=substring(text(), 1,2) />
<xsl:sort order=ascending select=substring(text(), 4,2) />
The first sort element singles out the
yyyy 
and sorts on that first.
The second sort element singles out the
mm 
and sorts on that.
The third sort element singles out the
dd 
and sorts on that
Different date formats are just as simple. Different formats just require adjusting the substring functios
yyyy/mm/dd
<xsl:sort order=ascending select=substring(text(), 1,4) />
<xsl:sort order=ascending select=substring(text(), 6,2) />
<xsl:sort order=ascending select=substring(text(), 9,2) />
dd/mm/yyyy
<xsl:sort order=ascending select=substring(text(), 7,4) />
<xsl:sort order=ascending select=substring(text(), 4,2) />
<xsl:sort order=ascending select=substring(text(), 1,2) />
hh:mm:ss
<xsl:sort order=ascending select=substring(text(), 1,2) />
<xsl:sort order=ascending select=substring(text(), 4,2) />
<xsl:sort order=ascending select=substring(text(), 7,2) />
One interesting sidenote if you want to do something with the sorted nodes after they are sorted
i.e. position() use the apply-templates rather than for-each

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML