xsl-list
[Top] [All Lists]

Re: [xsl] Sort XML based on Tokenized String of sort by fields

2008-05-23 13:07:12
Thanks Michael and Michael,

This is a great help for me. I think it is the solution that will give
me the most flexibility.  I have never done anything like this before
and this will really help me get started. 

- Rebecca

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(_at_)forelle:~/Werkstatt/xsl > expand -t2 Sapir-2008-05-22.xsl
<xsl:transform version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:import href="Sapir-sorting.xsl"/>
  <xsl:template match="REPORT">
    <xsl:copy>
      <ORDER-USED><xsl:value-of select="$order"/></ORDER-USED>
      <xsl:apply-templates select="*"/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

mludwig(_at_)forelle:~/Werkstatt/xsl > expand -t2 Sapir-sorting.xsl
<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <!-- 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:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <!-- sorting templates; should be generated -->
  <xsl:template match="X[ $order = 'DATE,ID_1,ID_2,TYPE' ]">
    <xsl:copy>
      <xsl:apply-templates select="X_ROW">
        <xsl:sort select="DATE"/>
       <xsl:sort select="ID_1"/>
        <xsl:sort select="ID_2"/>
        <xsl:sort select="TYPE"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <!-- more of these -->
</xsl:transform>



Merlin Securities - #1 Prime Broker North America and #1 Prime Broker Single 
Strategy Funds - Global Custodian 2007
#1 Prime Broker for Hedge Funds under $1 Billion - Alpha Survey 2007



 
--------------------------------------------------------------------------
This message contains information from Merlin Securities, LLC, or from one of 
its affiliates, that may be confidential and privileged. If you are not an 
intended recipient, please refrain from any disclosure, copying, distribution 
or use of this information and note that such actions are prohibited. If you 
have received this transmission in error, please notify the sender immediately 
by telephone or by replying to this transmission.
 
Merlin Securities, LLC is a registered broker-dealer. Services offered through 
Merlin Securities, LLC are not insured by the FDIC or any other Federal 
Government Agency, are not deposits of or guaranteed by Merlin Securities, LLC 
and may lose value. Nothing in this communication shall constitute a 
solicitation or recommendation to buy or sell a particular security.



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--