BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


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


XSLT
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
Dependent ComboBox in HTML build with XSLT
Re: How to use XSL function sum() to sum 2 XML fields
Re: How to use XSL function sum() to sum 2 XML fields
How to use XSL function sum() to sum 2 XML fields
Helpful XSLT ""break"" template
Support automatic transformation using XSLT in IE in side by side mode
Displaying XML data in Pages
Repeated search and replace in XSLT
DVC Algorithms in XSLT
Another way of avoiding XSLT processor crash
Re: DVC Algorithms in XSLT
trig functions in xslt
XSLT string Replace function
A Slide-In Menu made with XML/XSLT
What is SVG?


 
 

<< 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 04/25/2001
Times viewed 214

Dynamically Selecting Which Element To Sort On Using Parameters


Summary Many times it is desirable to specify at run-time upon which element our xml should be sorted on. Here's how we can do that.

It is not unusual to use XSLT to sort an XML document based on specific values. For example, in our XSL we may use something like the following to sort our data: In this case we have identified that we want to process each "repeating" element in our XML and that we want to run through them ordered on the "childnode" element. We have also specified the sort order and that we should treat the data as numbers instead of text (in "number" order, 2 comes before 10 as in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. In "text" order 10 comes before 2 as in 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9 because the it is sorted alphabetically, not numerically). A downside of the above sort is that it is hardcoded into our XSL. Meaning that the sort will ALWAYS be done on the "repeating" element, sorted ascending and as a number sort. We could conceivably load the XSL into a parser and alter the XSL programmatically, however this is not the ideal solution. So how can we allow our XSL to implement sort functionality on any field we choose, any order we choose and any sort type we choose at the time of transformation? By using parameters of course! Brief Introduction to Parameters Parameters, in typical programming languages, are used to pass some kind of data into a function or object. The function or object then processes this data, or uses the data in the internal process to determine logic flow. Parameters are similar in XSL. They are a means for you to pass some data to the XSL when you call it. This parameter data could be a flag indicating to enable/disable portions of the XSL, could be a value to search on in the XSL, etc. You can even use XSL to hold node-sets or even entire XML documents. It should also be noted that parameters are not restricted to the XSL as a document either. They can also be defined at the template level meaning when a template is called by another template, the callING template can pass parameters to the callED template (using with-params). If you are interested in more details on parameters I would encourage you to take a look at the following link which discusses parameters and variables (similar to parameters in a lot of ways): http://www.vbxml.com/xsl/articles/xpath_xsl_style/33230404.asp I am also not going to get into how to pass parameters to your XSL. For information on passing parameters into your XSL from your web page or application take a look at Trace's snippet at: http://www.vbxml.com/snippetcentral/main.asp?view=viewsnippet&id=v20010410230220 For info on how to pass parameters from one template to another, pick up your favourite XSL reference and look up the xsl:with-param element. We are going to use parameters to alter the element that the XSL sorts on and also the sorting direction and sorting type (number, text). Getting to the Good Stuff Let's assume our XML has the following structure: 19361 Acme Industries Inc. Wile E. Coyote 500000 ... Now our XML has a whole bunch of these and we want to present this data to the user in a form suitable for them. We want to allow them to choose which field (element) to sort on. If they're sales they'll probably want to sort on lastyearssales. If they're PR they will probably want to sort on custname. We could write XSL for each possible sorting, but that would be wasteful (not to mention boring). Instead we are going to use parameters to allow the sort criteria to be passed to the XSL. The first step we have to do is to define the parameters we are going to use to control the sorting. To define a parameter we use the xsl:param element. So we said earlier we were going to allow the passing of the sort field, sort direction and sort type (number/text), so we obviously need three parameters. We would declare them like so: custname ascending text Because these parameters are being passed at the document level (versus the template level) they are defined immediately after the xsl:stylesheet node and before your first template: custname ascending text You'll notice the parameters have a name attribute. This is the unique name you want to give it. It will be used to access the value in the parameter later in your XSL. Notice also that we have supplied a value to the parameters in our case. It is possible to define empty parameters, but if you do so you must either ensure that the calling application passes the parameters (because if they don't the parameters will be empty) or write your XSL to check for empty parameters. By putting a value in the parameter declaration as we did, we are supplying a default value that will be used if the parameters is not passed by the calling application. Now we would write our XSL as we would any other XSL, declaring our templates, creating the output etc. But at some point we have to use an xsl:sort element to sort the xml as it is processed. This is where our story picks up. The next step in the process is to define the sort element to use the parameters. The Sorting Show Typically we would define a sort element something long the lines of: However, as we said above this just isn't dynamic enough for us. So we are going to use the values of the parameters passed into the xsl. Typically to extract the value contained in a parameter you use the syntax: {$myParam} So in theory (doesn't that sound ominous) we could rewrite our sort element as: Great......except (here's the ominous part) you can't use a parameter directly in the select attribute like that. You can use parameters in pretty much any attribute you like for any element you like....EXCEPT the select attribute. The select attribute must evaluate to an object (an element). This is then converted to a string which is used for sorting. For this reason we cannot use our parameter directly (which contains a string). We can however still use the parameter in the select attribute. It's just a little messier. We have to use an expression. So we are going to use the following expression: If we look carefully at the select attribute you will see that what we are doing is selecting from all nodes ("*") all nodes that have a name equal to the contents of the parameter ("[name(.) = $sortfield]") (Thanks to Steve Kienle for that explanation). And that's it. That's all there is to it. By using the above construct for our sort we can now dynamically alter which element is being sorted on by changing the value of the sortfield parameter. We can also alter the sort direction by changing the order parameter to either ascending or descending and we can change the sort type by setting the data-type parameter to either text or number. You will find example xml and xsl attached to this article demonstrating everything we've discussed.

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