xsl-list
[Top] [All Lists]

RE: XSL/XPath Dynamic sort key

2003-07-10 15:47:13
I know I know, it's not possible.

You're right. Well, almost.

The select="*[name()=$param]" trick only works for single level "child
element" selections. You can try stretching it to other cases, but it's
going to get combinatorially difficult.

I would go with one of the other possible approaches, which are:

(a) generate the XPath expression dynamically, using xx:evaluate() if
your processor offers this extension

(b) generate (or modify) the stylesheet dynamically before you run it.

Michael Kay



  Well I've got this line 
working so far, 
so I'm halfway through the "this is impossible in XSL" 
process so far.  
First some params, then some XSL.

<xsl:param name="sort_select_lvl_1">author</xsl:param>
<xsl:param name="sort_select_lvl_2">/</xsl:param>
<xsl:param name="sort_select_lvl_3">last_name</xsl:param>
<xsl:param name="sort_order">ascending</xsl:param>
<xsl:param name="sort_data_type">text</xsl:param>

<xsl:apply-templates select="document">
  <xsl:sort select="*[name()=$sort_select_lvl_1]" 
data-type="{$sort_data_type}" order="{$sort_order}" /> 
</xsl:apply-templates>


Now here's what I want.  Sometimes I want to sort by uid, 
accesses, which 
works fine.  Other times I want to sort by author/last_name.  
Now I can do 
this by changing the sort line to this...

<xsl:sort 
select="*[name()=$sort_select_lvl_1]\*[name()=$sort_select_lvl_2]" 
data-type="{$sort_data_type}" order="{$sort_order}" />

But then it doesn't work for uid or accesses anymore because 
the "\" at the 
end of the string.  If I remove the "\" i get a number 
expected error.  So 
I've come up with half a way around it.  I figure I will have lots of 
variables at the top, lvl_1 through 4 so that I can fill them 
all with parts 
of an xpath statement and then join them togather.  So I try it and 
unfortunately xpath's concat does not let the sort select 
evaluate the xpath 
statement.  In other words

xsl:sort select="concat('author','/','last_name')" />
is not the same as
<xsl:sort select="author/last_name" />

So how do I string togather the *[name()=$sort_select_lvl_1], 
one directly 
after the other?  I set the defaults in the xslt above to reflect a 
reference to "author/last_name".


Glimpse of XML

<catalog>
  <document>
    <uid>2301</uid>
    <url>http://www.who.com/document.htm</url>
    <subject>General Stuff</subject>
    <abstract>This is an abstractabstract>
    <accesses>1</accesses>
    <age>4</age>
    <last_accessed date="20011229">12/29/2001</last_accessed>
    <author>
      <first_name>Peon</first_name>
      <last_name>lover</last_name>
      <serial>9A1551645</serial>
    </author>
    <owner>
      <first_name>Jolly</first_name>
      <last_name>Goose</last_name>
      <serial>123456</serial>
    </owner>
    <manager>
      <first_name>Someone</first_name>
      <last_name>Special</last_name>
      <serial>123456</serial>
    </manager>
    <supervisor>
      <first_name>Someone</first_name>
      <last_name>Important</last_name>
      <serial>123456</serial>
    </supervisor>
  </document>
  ...
</catalog>

Thanks in advance!

Aaron

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online  
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>