Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XSLT sorting using variables

Posted on 2011-09-26
1
Medium Priority
?
265 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

972 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