Solved

XSLT sorting using variables

Posted on 2011-09-26
1
207 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now