Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSLT sorting using variables

Posted on 2011-09-26
1
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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. 
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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