Solved

XSLT sorting using variables

Posted on 2011-09-26
1
245 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what are list of ebay api errors 1 35
Add to XML (Powershell) 1 42
xml files 7 54
Creating an element based on double condition with XSLT 9 18
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…
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, …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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