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
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
JScript Super Class To Handle XML Transformations
Test if a bit is on
Web Methodology (Layout Management)
Grouping an XML Node by First Letter


 
 

<< XQuery.NET and XML >>


By Dimitre Novatchev
First Posted 02/27/2001
Times viewed 343

Sorting (advanced) of dynamically constructed strings


Summary This snippet shows how to construct a sophisticated XPath expression which allows sorting of dynamically created strings. Categories: XPath, XSLT

This snippet shows:  
  1. How to construct a string with an (if-like) XPath conditional expression.
  2. How to get rid of unwanted characters in a string.
  At the beginning of February 2001 the following problem was raised in the xslt-list (http://sources.redhat.com/ml/xsl-list/2001-02/msg00178.html):

I have the following problem when sorting a list of words. My xml is:
<root>
   <line lineID=1>
      <word wordID=1>ABC-</word>
      <word wordID=2>ABCD</word>
   </line>
   <line lineID=2>
      <word wordID=1 type=end>DEF</word>
      <word wordID=2>XYZ</word>
   </line>
</root>


I have a stylesheet to create an alphabetical list of words. If a word contains a dash, it means it has to be joined with the following word that has an attribute type=end. The result is:
ABCDEF
ABCD
XYZ

which is not what I want because the alphabetical order is wrong.  I want the output to be:
ABCD
ABCDEF
XYZ

My solution is contained in the XSL Code below.

The main difficulty here is how to specify what exactly is to be sorted.  This must be any regular word (not an ending designated by @type='end') But there are two cases here -- either this word does not contain a hyphen, or it does contain a hyphen.  In the first case we must sort on the value of the current node (.)  In the second case, we have to concat the value of the current node with the value of the first following word node, which is an ending (@type='end').

How to have the combination of these two possible cases specified by one single XPath expression?  The following expression is quite close to achieving this:
concat(.,self::*[contains(.,'-')]/following::word[@type='end'])

In case the current node does not contain a hyphen, then the second argument of the concat() function will be the empty string -- therefore the result will be only the value of the current node.

In case the current node does contain a hyphen, then the second argument of the concat() function correctly will evaluate to the first of the following word nodes that are endings.

Just one little problem remains -- the hyphen in the current node remains as part of the concatenation. This will generally make the result of the sort incorrect.

How do we fix this?  Now the translate() function comes to help -- always when it's necessary to get rid of a single character from a string, this can be achieved as follows:
translate(aString, singleChar, '')

Therefore the complete solution is to have the select attribute of the xsl:sort as follows:
select=translate(concat(.,self::*[contains(.,'-')]/following::word[@type='end']), '-', '')      

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