xsl-list
[Top] [All Lists]

Re: hierarchical sorting problem

2003-10-07 10:57:53
Hi Andy,
  I feel, when you say you want to sort by ActionCode,
tags within ProductGroup (i.e. ChildProduct) are a
seperate group to be sorted, and
<Product> tags just below <Result> form a different
group to be sorted seperately.

I feel, you cannot sort between 
<Product MaterialNumber="494728" Team="ENJ"
ActionCode="C1" />
and ,
<ChildProduct MaterialNumber="376050" Team="RMT"
ActionCode="A1" />

based on ActionCode. 

<Product> and <ChildProduct> belong to two mutually
exclusive sets that have to be sorted seperately.

I believe this is implied by your description. I am
sorry, that I am still not able to understand the
requirement.

Regards,
Mukul



--- Andy_Freeman(_at_)bd(_dot_)com wrote:

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="{(_at_)MaterialNumber}"
Team="{(_at_)Team}">
        <xsl:for-each select="Product">
         <xsl:sort select="@ActionCode" />
           <Product
MaterialNumber="{(_at_)MaterialNumber}"
Team="{(_at_)Team}" Actioncode="{(_at_)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="{(_at_)MaterialNumber}"
Team="{(_at_)Team}">
       <xsl:for-each select="Product">
         <xsl:sort select="@MaterialNumber" />
         <Product MaterialNumber="{(_at_)MaterialNumber}"
Team="{(_at_)Team}" ActionCode="{(_at_)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(_at_)bd(_dot_)com 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" />

=== message truncated ===


__________________________________
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



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