Re: [xsl] xslt sort dilemna

Subject: Re: [xsl] xslt sort dilemna
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 09 Sep 2008 14:57:53 -0400
Mohit,

In order to understand where to put the xsl:sort instruction, you need to understand what it does. It really isn't "sorting" anything. XSLT is a declarative language, so any XSLT stylesheet is really a specification for how an XSLT processor is supposed to build its result out of its input. Along these lines, xsl:sort overrides the processor's ordinary behavior, in which the order of the results corresponds to the order of elements in the input from which the results are built, and instead corresponds to a sorted order in the input.

So you're not really "apply[ing] sort" on any output, merged or not. If you really want to sort your output, the easiest way to do that is to run a second transformation in which your input is the output of the first transformation.

And what you'd want is probably something like (what you said you've tried)

<xsl:for-each select="//ret|$file2//ret">
  <xsl:sort select="filing"  order="ascending" data-type="text"/>
  <xsl:sort select="State" order="ascending"  data-type="number"/>

Assuming your input 'ret' elements all have both 'filing' and 'State' child elements.

Now why isn't this working? You say your sort on 'filing' is working, only the 'ret' elements from the two input documents are not sorted together. This might well be a bug in your processor, which might have evaded detection since this is actually a fairly rare thing to be doing. Careful testing can determine this, but in the meantime you might try something like:

<xsl:variable name="merged-set" select="//ret|$file2//ret"/>
<xsl:for-each select="$merged-set">
  <xsl:sort ...

Conceivably, this could fool the processor into treating the two sets of nodes as one, as you want them to be treated.

Even if this works, you should tell us what processor you are using so that others may try to duplicate the bug, if that's what it is. (Several developers of processors monitor this list to see what bugs might be reported.)

As to why your sort on 'State' elements is not working, you need to show us your input data (again?) if we are to see what could be going wrong there. Without seeing the input (which you should post in reduced form with your question, so the email is self-contained and does not require us to look back in the archive ), it's really impossible to know anything for certain.

Cheers,
Wendell


At 02:10 PM 9/9/2008, you wrote:
I have 2 xml that I am merging, something like this:

<xsl:for-each select="//ret|$file2//ret">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates>
<xsl:sort select="filing" order="ascending" data-type="text"/>
<xsl:sort select="State" order="ascending" data-type="number"/>
</xsl:apply-templates>
</xsl:element>
</xsl:for-each>



Couple of questions: 1. Above xsl:for-each merges 2 xml documents. Now I want to apply sort on the merged output. I tried putting sort inside apply-templates as well as moving inside for-each. It doesn't seem to work. I am trying to sort on 2 elements. Is there a way I can sort on merged output.

2. Also, when I put sort in for-each it sort only one document at a
time, but second sort on "State" doesn't work.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread