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 February 24th, 2008, 06:00 AM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to evaluate parameter?

Hi, I'm trying to use a passed parameter in the xsl:sort tag, but from what I have researched it treats it as a string when it should be evaluated as an xslt expression. How can I get the following to work? Thanks!


Code:
  <xsl:param name="order" />
  <xsl:param name="dataType" />
  <xsl:param name="sortExpression" />

  <xsl:template match="/">    
      <xsl:for-each select="//aws:Item">
        <xsl:sort select="$sortExpression" data-type="{$dataType}" order="{$order}" />
        <xsl:value-of select="$sortExpression" />



 
Old February 24th, 2008, 06:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't do this directly. Dynamic evaluation of XPath is not supported in XSLT. You might be able to modify the path or use other techniques but you need to show a sample of the XML and XSLT.

--

Joe (Microsoft MVP - XML)
 
Old February 24th, 2008, 07:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the string is just an element name you can use

select="*[name()=$param]"

Otherwise you will have to use an extension such as saxon:evaluate().

Another approach (which I use sometimes when running transformations in the browser) is to modify the stylesheet before compiling it, to include the text of the expression.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 24th, 2008, 04:21 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only reason I need to pass a select expression is to test whether or not to sort the content. The xsl:sort command is quite picky and from what I know, you can't use conditionals with it. Would you guys have any suggestions for choosing between two different sorting scenarios?

 
Old February 24th, 2008, 05:30 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured out a way to do a conditional sort :) For my case there were two different sort scenarios, one where I didn't want to sort and one where I did. What I did was create and pass a dummy parameter to the style sheet from my c# file, much like I did with my other parameters. In the xsl:sort statement I added a condition to the end of the select portion.

Here is some code snippets
Code:
<xsl:sort select="aws:OfferSummary/aws:LowestNewPrice/aws:Amount[number($dummyValue) >= 0]" data-type="{$dataType}" order="{$order}" />
If I want to sort, I set the $dummyValue to 0 in my c# code and pass it to the stylesheet, if I don't want to sort I pass -1. I also pass parameters for the datatype and order. Those parameters don't need any special attention like the select value one, so just pass and use those like I did above.

Hope this helps anyone out there trying to do conditional sorting!

 
Old February 25th, 2008, 05:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Is my memory playing tricks on me or does the following not work:

Code:
<xsl:for-each ...>
  <xsl:if test="$dummy = 0">
    <xsl:sort select="..."/>
  </xsl:if>
  ...
</xsl:for-each>
/- Sam Judson : Wrox Technical Editor -/
 
Old February 25th, 2008, 06:11 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think xsl:sort has to have apply-templates/for-each as an immediate parent.

--

Joe (Microsoft MVP - XML)
 
Old February 25th, 2008, 06:32 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yeah you're right, my mind was playing tricks on me.

Another option would be to take the contents of the xsl:for-each and put it in a template, then have two for-each statements inside a xsl:choose/xsl:when, one with a sort, the other without.

Or just use the method you've got above.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 25th, 2008, 07:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Personally, rather than incurring the potential expense of doing a sort in which all the sort keys are equal, I would do

<xsl:choose>
  <xsl:when test="$dummy = 0">
     <xsl:for-each select="xxx">
        <xsl:sort .../>
        <xsl:call-template name="body"/>
     </xsl:for-each>
  </xsl:when>
  <xsl:otherwise>
     <xsl:for-each select="xxx">

        <xsl:call-template name="body"/>
     </xsl:for-each>
  </xsl:otherwise>
</xsl:choose>

(or better, an equivalent using xsl:apply-templates)


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Expression must evaluate to a node-set. XMLUser XSLT 3 February 5th, 2008 01:27 PM
Evaluate string in the where clause mat41 SQL Language 5 February 1st, 2008 02:07 AM
XPath Evaluate Document safin XML 2 September 19th, 2005 07:29 AM
evaluate the size of a file BEFORE it gets uploade ALEX_GRIM ASP.NET 1.0 and 1.1 Professional 7 May 10th, 2005 10:30 PM
How do i evaluate A_josh VB How-To 1 December 22nd, 2003 01:47 PM





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