Solved

XSLT sorting using variables

Posted on 2011-09-26
1
228 Views
Last Modified: 2012-06-21
Hi,

I have two variables 'sort' and 'sort_order' that I would like to pass into the sort expression but am struggling.

  <xsl:variable name="sort">Story/Price
  </xsl:variable>
  <xsl:variable name="sort_order">ascending
  </xsl:variable>
  <xsl:variable name="data_type">
    <xsl:choose>
      <xsl:when test="$sort='Story/Price'">number</xsl:when>
      <xsl:when test="$sort='Story/Product_Rating'">number</xsl:when>
      <xsl:otherwise>text</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

<xsl:sort data-type="{$data_type}" order="{$sort_order}" select="$sort"/>

This produces zero results, however when I hard code the variable values into the sort expression it works fine.

Help please :-S
0
Comment
Question by:help-is-needed
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 36707766
the $data_type and $sort_order resolution should work fine
the select can't work that way.
you expect that a string "Story/Price" is magicaly transformed into an XPath and gets evaluated.
There is no such thing as dynamic XPath resolution (well, there is in XSLT3 or if you use saxon extension saxon:evaluate())

what does work is the following
<xsl:variable name="sort1">Story</xsl:variable>
<xsl:variable name="sort2">Price</xsl:variable>

<xsl:sort data-type="{$data_type}" order="{$sort_order}" select="*[name() = $sort1]/*[name() = $sort2]"/>
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JPA XML tag definition 2 68
selectSingleNode in Access 2013 11 59
XPath Syntax help in conjunction with perl XML::LibXML 18 112
parse convert xml feed to text (python) 2 82
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question