 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 10th, 2007, 09:00 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dynamic filtering
Hallo everyone,
I have XML/XSL Reporting functionality which uses dynamic sorting. I do it in this way:
I have a variable $pcol which receives the column name for a javascript procedure embedded in the XSL. The javascript then requeries the page with the following syntax enabling the new sort (i.e. a sort on a different column). Here is the sort clause ...
<xsl:sort select="*[name()=$pcol]"/>
This all worked fine but now I have restructured my code quite a lot and amongst other things I have changed the following ...
- switched from using apply-template to for-each
- moved a lot of data into the attributes of the XML so that now a lot of field declarations are the following: @MyField instead of MyField.
And now (some reason) my sorting nolonger works. Arrgghh. I have included some debug displays and find that the parameter ($pcol) is being populated correctly and the code seems to run (the display flickers and refreshes) but the new sort request is not processed.
I have tried hard-coding the sorts that I require and then they come through OK. But now with my dynamic sort (which previously worked).
Does anyone have any ideas about why my dynamic sorting is falling over? Maybe 'for-each' works differently to 'apply-templates'?
Any ideas on this point would be a great help.
Regards,
Alan Searle.
|
|

January 10th, 2007, 09:28 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
<xsl:sort select="*[name()=$param]"/> will only work if $param identifies a child element, because "*" selects child elements. If you want to select an attribute, use select="@*[name()=$param]"/>. If you don't know whether it's an element or an attribute in advance, use select="(*|@*)[name()=$param]"/>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

January 10th, 2007, 09:28 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem solved !!!
I did some more googling and stumbled upon the following change that I need to make to my syntax ...
I was assuming that I pass the '@' character to my sort in the parameter but this doesn't work. The '@' (attribute) symbol needs ot be in the sort clause thus:
<xsl:sort select="@*[name()=$pcol]"/>
Previously I only had this:
<xsl:sort select="*[name()=$pcol]"/>
i.e. missing the '@'.
Now it works fine.
I hope this information will be useful for others.
Regards,
Alan Searle.
|
|

January 10th, 2007, 09:30 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
It looks like we got there at the same time.
And many thanks for expanding on this point.
Regards,
Alan Searle.
|
|

January 10th, 2007, 10:02 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I thought it was too good to be true ...
Yes, I've now got the answer to the dynamic sorting (with attributes) question and that works fine with most data. However, when I apply this to grouped columns I have a problem.
As it is, I can sort sort the group headers (dynamically) thus ...
<xsl:sort select="*[name()=$pcol]"/>
... and the grouped columns (statically) thus ...
<xsl:sort select="sum(key('kteam', team)/@GR_CNT)" data-type="number" />
But how can I parameterise the whole thing? Especailly as the '@' character needs to be included? Michael, could I use your syntax '(*|@*)'? Or am I simply asking too much of XSLT
If you have any tips, then I would be very pleased to hear your ideas.
Regards,
Alan Searle.
|
|

January 10th, 2007, 10:30 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Or maybe I can 'wrap' the sort-clause in an if-clause?
For example ...
<xsl:for-each select="rep_base[count(.| key('kteam', @team)[1])=1 and @plt=$pfiltfld]">
<xsl:choose>
<xsl:when test="$pcol='sort1'">
<xsl:sort select="MySortColumn" />
</xsl:when>
<xsl:when test="$pcol='sort2'">
...
etc ...
</xsl:for-each>
Or is this illegal / cheating ?
I'll play around with it and tell you what I find.
Regards,
Alan Searle
|
|

January 10th, 2007, 10:31 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
When the *[name()=$p] technique runs out of steam because the sort keys become too complicated, there are two ways you can go:
(a) if your processor offers it, use an extension such as saxon:evaluate(), which allows you to construct an XPath expression dynamically from a string
(b) construct or modify a stylesheet dynamically before executing it. Because XSLT is XML, you can do this using XSLT itself.
Both techniques, incidentally, are quite common practice in larger applications.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

January 10th, 2007, 10:54 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You'll find it doesn't work.
In 2.0 however you can do
<xsl:sort select="my:function(.)"/>
and write an arbitrary function to compute the sort key, which can of course have conditional logic.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

January 10th, 2007, 11:34 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
This sounds hopeful and so far I have been trying a few things.
We have MSXML2, v3 and the application will be running client-side.
Anyway tried the following ...
I took a look at EXSLT and notice that they have an add-in which should do this (dyn.zip). I tried downloading this but kept getting corrupt zip file. I will ty again.
I imagine that all I need to do is to possition the add-in files with the other XSL file and it should work. Is this correct?
If this is the case, then probably this would be the easiest path for me. All users are internal and have the same browser configuration.
The second option that you mention sounds more complicated and I don't think I could manage that at the moment.
Anyway, I'll be experimenting and will give feedback to the forum.
Regards and thanks,
Alan Searle
|
|

January 10th, 2007, 11:52 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've been googling again and EXSLT and their add-in dyn.zip but there seems to be a lot of confusion out there about this.
So don't worry about answering my question on this ... I need to investigate more what options I have for implementing 'evaluate' under our MSXML2.
In the meantime I'll implement limited sorting functionality which is a bit of a shame because it would be good to sort on some of the summed columns.
Thanks for your tips and, yes, it's 'never-a-dull-moment' in this field, is it? :-)
Regards,
Alan.
|
|
 |