[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sort XML based on Tokenized String of sort by fiel

Subject: Re: Sort XML based on Tokenized String of sort by fields
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Thu, 22 May 2008 12:23:22 +0200
Re:  Sort XML based on Tokenized String of sort by fiel
Michael Kay schrieb:
As David suggests, I think I'd go for the approach of generating a
stylesheet. In effect your <REPORT_FORMAT> element defines a miniature
programming language, and a good way of implementing such languages is
often to translate them to XSLT. I've done similar things with report
specifications entered interactively on the screen. You're already
doing dynamic construction/evaluation of XPath expressions, so dynamic
construction of the entire stylesheet (or of the controlling
framework, it can always include/import a fixed module) isn't a major
step from that.

I don't have the code to generate the stylesheet, but I'd like to propose what I imagine the result should look like.

There is a string representation of the sorting order, captured in the
xsl:variable $order. Then there is one generated matching template for
each value of $order that may occur in reality. (Well, I didn't write
the code to generate these templates, so I hand-coded one and left the
rest up to imagination.) And there is a template to format the output
(the copy template). Auto-generated stuff should go in one module,
hand-coded stuff in the other one.

Surely, this could be improved. Any comments welcome.

Two stylesheets and XML data follow.

Michael Ludwig

mludwig@forelle:~/Werkstatt/xsl > expand -t2 Sapir-2008-05-22.xsl
<xsl:transform version="2.0"
  <xsl:import href="Sapir-sorting.xsl"/>
  <xsl:template match="REPORT">
      <ORDER-USED><xsl:value-of select="$order"/></ORDER-USED>
      <xsl:apply-templates select="*"/>

mludwig@forelle:~/Werkstatt/xsl > expand -t2 Sapir-sorting.xsl
<xsl:transform version="2.0"
  <!-- Generate this module to include all needed permutations
  of your sort parameters. Hopefully, not too many! -->
  <!-- sorting criteria identifier; normalize by eliminating spaces -->
  <xsl:variable name="order"
    select="translate(/REPORT/REPORT_FORMAT/ORDER_BY, ' ', '')"/>
  <!-- just the copy template to generate output -->
  <xsl:template match="@*|node()">
      <xsl:apply-templates select="@*|node()"/>
  <!-- sorting templates; should be generated -->
  <xsl:template match="X[ $order = 'DATE,ID_1,ID_2,TYPE' ]">
      <xsl:apply-templates select="X_ROW">
        <xsl:sort select="DATE"/>
        <xsl:sort select="ID_1"/>
        <xsl:sort select="ID_2"/>
        <xsl:sort select="TYPE"/>
  <!-- more of these -->

mludwig@forelle:~/Werkstatt/xsl > expand -t2 Sapir-2008-05-22.xml

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.