Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old January 11th, 2007, 04:51 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not as difficult as I thought ...

Hi again everyone,

With my trials and tribulations trying to get evaluate to work (msxsl), I felt a bit stumped and confused by recommendations to include external code: Here I assumed that I would have to declare libraries and supply extra (header) files. It was all a bit daunting.

But in the end it wasn't so difficult and it all ran with our old installation of MSXSL (albeit only on our internal IE browser).

Here is a link showing how it's done:

http://xmleverywhere.com/tips/xslt.htm

Basically you need two components ...

1. some extra declarations in the header of your XSLT:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dyn="http://exslt.org/dynamic"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                extension-element-prefixes="dyn msxsl">

2. the code:

<msxsl:script implements-prefix="dyn" language="jscript">
   function evaluate(context, expression)
      {
         return context.nextNode().selectNodes(expression);
      }
</msxsl:script>

(NB: This comes directly after the header and is specific to MSXSL)

Then to test I declared a variable thus ...

<xsl:variable name="testpath" select="'/dataroot/ref_dateset[1]/@PERTEXT'"/>

... with any node declaration as the value.

And then went on to call up the code thus ...

<xsl:value-of select="dyn:evaluate(., $testpath)"/>

This returned the contents of the node ...

dataroot/ref_dateset[1]/@PERTEXT

Yippeee: I was very pleased as this now means that I can include a whole bunch of stuff in various 'when' clauses which would not have run otherwise.

I wish all the best to anyone else who wishes to try this and many thanks to Michael for prompting me to go along this path.

Regards and thanks,
Alan Searle.

 
Old January 16th, 2007, 04:42 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again everyone,

I've been having partial success with my experiments with evaluate and find that I can declare a variable containing a simple XSLT call thus ...

<xsl:variable name="testsort1" select="'sum/dataroot/ref_dateset[1]/@PERTEXT'"/>

... and then later retrieve this value (i.e. the value from the XML retrieved by the XSLT) with 'evaluate' thus ...

<xsl:value-of select="dyn:evaluate(., $testsort1)"/>

That's great!

However, my issue with dynamic sorting involves keys (used for summing numeric data) and my 'evaluate' statements seem to be falling over on the variable declaration which seems to have a problem with the hyphens used to define the key (in this case kteam). Here is my syntax ...

<xsl:variable name="testsort1" select="'sum(key('kteam', @tm)/@zGR_CNT)'"/>

... which works fine when directly entered into the sort statement. But when I try to run it with 'evaluate', thus ...

<xsl:sort select="dyn:evaluate(., $testsort1)" order="ascending" data-type="number"/>

... my browser (IE6, SP2) complains thus: Expected Token 'eof' found 'NAME'. 'sum(key('-->kteam<--', @tm)/@zGR_CNT)' ... Which makes me think that there is a problem with the hyphens in the variable declaration (of the key kteam).

I am also worried that, even if I solve the problem of the variable declaration, my 'evaluate' statement might have problems with the key handling in the summing.

It would be great if someone could confirm whether or not I am going along the right path and whether I can expect to be able to sort on a key via an 'evaluate' statement.

That would be a great help to me.

Many thanks,
Alan Searle

 
Old January 16th, 2007, 06:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your problem is with the quotation marks: you're nesting single quotes within single quotes.

This is one of the few occasions I recommend the use of the alternative syntax for variables:

<xsl:variable name="testsort1">
   sum(key('kteam', @tm)/@zGR_CNT)
</xsl:variable>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 16th, 2007, 08:04 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Many thanks: Your tip worked and the variable ...

<xsl:variable name="testsort1">sum(key('kteam', @tm)/@zGR_CNT)</xsl:variable>

... was accepted and I could re-display the content of the string.

However, when I tried to apply the sort, thus ...

<xsl:sort select="dyn:evaluate(., $testsort1)" order="ascending" data-type="number"/>

... the page header would display but no data was retrieved.

I then went back to my reduced test (to display a simple item from the XML) and found that ...

<xsl:variable name="testsort1" select="'/dataroot/ref_dateset[1]/@PERTEXT'"/>
<xsl:value-of select="dyn:evaluate(., $testsort1)"/>

... would work but ...

<xsl:variable name="testsort1">/dataroot/ref_dateset[1]/@PERTEXT</xsl:variable>
<xsl:value-of select="dyn:evaluate(., $testsort1)"/>

... would not.

I tried both with and without quotes but got the same result. I hope it's a simple syntax thing and, indeed, it would be very useful to get this working.

Many thanks,
Alan Searle.

 
Old January 16th, 2007, 08:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try dyn:evaluate(., string($testsort1))

Normally I would expect a result-tree-fragment to be converted to a string automatically, but IIRC you are using an implementation of dyn:evaluate() written in Javascript, and as this doesn't have any type signatures on functions the implicit conversion may not work.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 16th, 2007, 09:06 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Yes, your tip with string() worked and I could use 'evaluate' to retrieve data from the XML file. Great!

However, when I tried to apply a sort, the data display stopped working. I'll quickly explain what I did:

First I declared the sort clause ...

<xsl:variable name="testsort1">sum(key('kteam', @tm)/@zGR_CNT)</xsl:variable>

... and checked that the string could be retrieved and re-displayed.

Next I checked my sort in a 'hard-coded' version, thus ...

<xsl:sort select="sum(key('kteam', @tm)/@zGR_CNT)"/>

... which worked OK.

After that, I tried incorporating the evaluate clause into the sort, thus ...

<xsl:sort select="dyn:evaluate(., string($testsort1))"/>

But found that no data was returned.

I think that I am getting closer but still have some syntax issues so, if you can see any obvious problems with this, then that would be a great help.

Many thanks,
Alan Searle

 
Old January 16th, 2007, 09:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's very possible that your implementation of dyn:evaluate isn't recognizing the key() function, since this is an XSLT rather than an XPath function, and the actual key names are defined in the stylesheet. Try doing it without keys. (I would expect an error rather than "no output", but the Javascript culture is to avoid giving errors).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 16th, 2007, 09:46 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Yes, I think you're right and it is a problem with the key. I tried the following ...

<xsl:variable name="testsort1">@tm</xsl:variable>

... with @tm being a team code and it worked fine (with the evaluate clause).

However, the following ...

<xsl:variable name="testsort1">@*[name()=$pcol]</xsl:variable>

... (with $pcol predeclared) which also creates a sort on '@tm' when directly implemented, also did not work.

I think that this is getting us closer to the explanation but I am now a bit worried that I may not be able to implement dynamic sorting on calculated columns (where a key has been used to create the grouping). Maybe you know of a little trick/work-round that could allow me to get the dynamic sorting in?

Many thanks for your tips.

Regards,
Alan Searle

 
Old January 16th, 2007, 10:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't know anything about the detail of your dyn:evaluate() implementation but unless you read otherwise in its documentation I would work on the assumption that the only context information available is the context node. No keys, no variables.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 18th, 2007, 06:12 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for your help/time on this one.

Yes, I also believe that I have reached the limit and that sorting on calculated columns is probably not possible.

My path now will be to implement sorts wherever I can (i.e. without evaluate) and then, maybe in a future release I might switch all the data to be pre-calculated / pre-summed (at the time of generation of the XML). This would increase speed (i.e. the XSLT would need to do any calculating and then would allow me to sort columns dynamically (as none would be calculated).

Thanks once again and, if I make progress on this point, I will post feedback on the forum.

Regards,
Alan.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Columns & Dynamic Grouping ??? nileshgambhava BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 1 March 24th, 2008 07:58 AM
help writing dynamic form data to dynamic table ublend SQL Server ASP 1 June 1st, 2007 08:09 AM
help writing dynamic form data to dynamic table ublend Classic ASP Professional 1 June 1st, 2007 08:08 AM
Dynamic Columns & Dynamic Grouping ??? nileshgambhava SQL Server 2005 0 December 12th, 2006 12:45 AM
Dynamic Table Filtering nancy Classic ASP Databases 1 July 8th, 2005 05:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.