Re: [xsl] hierarchical sorting problem

Subject: Re: [xsl] hierarchical sorting problem
From: Andy_Freeman@xxxxxx
Date: Tue, 7 Oct 2003 11:13:50 -0400
Hi Mukul,

Your first solution is removing my top level Product tags and it does not
sort the Product tags that have been grouped together.  I am posting a
different view of that same XML structure that might make it easier to
understand what I need:

<Result>
  <Product MaterialNumber="494728" Team="ENJ" ActionCode="C1" />

  <ProductGroup MaterialNumber="376050" Team="RMT">
    <ChildProduct MaterialNumber="376050" Team="RMT" ActionCode="A1" />
    <ChildProduct MaterialNumber="376009" Team="RMT" ActionCode="D4" />
  </ProductGroup>

  <Product MaterialNumber="70133" ActionCode="" />

  <ProductGroup MaterialNumber="75050" Team="RKL">
    <ChildProduct MaterialNumber="75050" Team="RKL" ActionCode="J0" />
    <ChildProduct MaterialNumber="76009" Team="RKL" ActionCode="A0" />
  </ProductGroup>

  <ChildProduct MaterialNumber="70309" Team="DDE" ActionCode="A5" />

  <ProductGroup MaterialNumber="75051" Team="RKP">
    <ChildProduct MaterialNumber="75051" Team="RKP" ActionCode="J1" />
    <ChildProduct MaterialNumber="76109" Team="RKP" ActionCode="A4" />
  </ProductGroup>
</Result>


Here is what I have come up with for the first problem:

<xsl:template match="/Result">
  <Result>
    <xsl:for-each select="child::*">
         <xsl:sort select="descendant-or-self::Product/@ActionCode"/>
     <xsl:copy-of select="."/>
  </xsl:for-each>
  </Result>
</xsl:template>

This preserved the structure of the XML and got me close to the expected
results.  However, it did not sort the child tags correctly.

Thanks for trying!

Andy



Hi Andy,
 I feel a logical sorting solution will be --

For 1st requirement
-------------------

<xsl:template match="/">
    <Result>
     <xsl:for-each select="Result/Group">
       <Group MaterialNumber="{@MaterialNumber}"
Team="{@Team}">
        <xsl:for-each select="Product">
         <xsl:sort select="@ActionCode" />
           <Product MaterialNumber="{@MaterialNumber}"
Team="{@Team}" Actioncode="{@ActionCode}">

           </Product>
         </xsl:for-each>
        </Group>
     </xsl:for-each>
    </Result>
</xsl:template>

For 2nd requirement
-------------------

<xsl:template match="/">
  <Result>
    <xsl:for-each select="Result/Group">
      <xsl:sort select="@MaterialNumber" />
      <Group MaterialNumber="{@MaterialNumber}"
Team="{@Team}">
       <xsl:for-each select="Product">
         <xsl:sort select="@MaterialNumber" />
         <Product MaterialNumber="{@MaterialNumber}"
Team="{@Team}" ActionCode="{@ActionCode}">
         </Product>
       </xsl:for-each>
     </Group>
   </xsl:for-each>
  </Result>
</xsl:template>

Its not very clear to me, *how you want to output
<Product> tags which are not within <Group>* . Some
such tags, you are outputting at top and some at
bottom(which does not seem to be a natural sorted
output).

The above XSLs are not producing <Product> tags which
are not within <Group> tags.

I feel, you need to make the _requirement more clear_.

Regards,
Mukul


--- Andy_Freeman@xxxxxx wrote:
> I am trying to sort an XML document by a variety of
> different attributes.
> Here is an example of the source document:
>
> <Result>
>   <Product MaterialNumber="494728" Team="ENJ"
> ActionCode="C1" />
>   <Group MaterialNumber="376050" Team="RMT">
>     <Product MaterialNumber="376050" Team="RMT"
> ActionCode="A1" />
>     <Product MaterialNumber="376009" Team="RMT"
> ActionCode="D4" />
>   </Group>
>   <Product MaterialNumber="70133" ActionCode="" />
>   <Group MaterialNumber="75050" Team="RKL">
>     <Product MaterialNumber="75050" Team="RKL"
> ActionCode="J0" />
>     <Product MaterialNumber="76009" Team="RKL"
> ActionCode="A0" />
>   </Group>
>   <Product MaterialNumber="70309" Team="DDE"
> ActionCode="A5" />
>   <Group MaterialNumber="75051" Team="RKP">
>     <Product MaterialNumber="75051" Team="RKP"
> ActionCode="J1" />
>     <Product MaterialNumber="76109" Team="RKP"
> ActionCode="A4" />
>   </Group>
> </Result>
>
> I need to sort by the Product ActionCode attribute
> at either level to
> produce the following output:
>
> <Result>
>   <Product MaterialNumber="70133" ActionCode="" />
>   <Group MaterialNumber="75050" Team="RKL">
>     <Product MaterialNumber="76009" Team="RKL"
> ActionCode="A0" />
>     <Product MaterialNumber="75050" Team="RKL"
> ActionCode="J0" />
>   </Group>
>   <Group MaterialNumber="376050" Team="RMT">
>     <Product MaterialNumber="376050" Team="RMT"
> ActionCode="A1" />
>     <Product MaterialNumber="376009" Team="RMT"
> ActionCode="D4" />
>   </Group>
>   <Group MaterialNumber="75051" Team="RKP">
>     <Product MaterialNumber="76109" Team="RKP"
> ActionCode="A4" />
>     <Product MaterialNumber="75051" Team="RKP"
> ActionCode="J1" />
>   </Group>
>   <Product MaterialNumber="70309" Team="DDE"
> ActionCode="A5" />
>   <Product MaterialNumber="494728" Team="ENJ"
> ActionCode="C1" />
> </Result>
>
> I also need to sort by the Product|Group
> MaterialNumber attribute to
> produce the following output:
>
> <Result>
>   <Product MaterialNumber="70133" ActionCode="" />
>   <Product MaterialNumber="70309" Team="DDE"
> ActionCode="A5" />
>   <Group MaterialNumber="75050" Team="RKL">
>     <Product MaterialNumber="75050" Team="RKL"
> ActionCode="J0" />
>     <Product MaterialNumber="76009" Team="RKL"
> ActionCode="A0" />
>   </Group>
>   <Group MaterialNumber="75051" Team="RKP">
>     <Product MaterialNumber="75051" Team="RKP"
> ActionCode="J1" />
>     <Product MaterialNumber="76109" Team="RKP"
> ActionCode="A4" />
>   </Group>
>   <Group MaterialNumber="376050" Team="RMT">
>     <Product MaterialNumber="376009" Team="RMT"
> ActionCode="D4" />
>     <Product MaterialNumber="376050" Team="RMT"
> ActionCode="A1" />
>   </Group>
>   <Product MaterialNumber="494728" Team="ENJ"
> ActionCode="C1" />
> </Result>
>
> Note: The example I have shown only has two children
> per group.  The live
> data for this document can have an unlimited number
> of children.
>
> TIA,
> Andy
>
>
>  XSL-List info and archive:
> http://www.mulberrytech.com/xsl/xsl-list
>


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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





**********************************************************************
This message is intended only for the designated recipient(s).  It may
contain confidential or proprietary information and may be subject to
the attorney-client privilege or other confidentiality protections.
If you are not a designated recipient, you may not review, use, copy
or distribute this message.  If you receive this in error, please
notify the sender by reply e-mail and delete this message.  Thank you. 

***********************************************************************


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


Current Thread