[xsl] Re: Sorting and Merge

Subject: [xsl] Re: Sorting and Merge
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 17 Nov 2003 21:44:32 +0100
> We have a member structure and allow users to define
> filters based on it. We can generate the result after
> applying those filters. But the problem is that result
> is messed up since the filter was applied
> sequentially.

[snip]

> After those filters are applied sequentially, we got
> the following result, but it contains duplicated
> members and order of members is not the same as
> original one.

[snip]

> But we need
> to go down each level and figure out the
> position(recursive)and get rid of duplicate members.
> This transformation seems doing merge and sorting.
> Please help me on this!

This can be done much easier if you transform the original document and use
the "messed" result of the searches as criteria which nodes from the
original document should be copied.

Something like this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="doc/MemberList">
    <MemberList>
      <xsl:apply-templates/>
    </MemberList>
  </xsl:template>

  <xsl:template match="Member[ancestor::doc]">
    <xsl:if test="/*/results/descendant::Member
                            [@name = current()/@name]">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

When the above transformation is performed on this source.xml (the original
xml document appended with the search results):

<search>
  <doc>
    <MemberList>
      <Member name="P1">
        <Member name="P11">
          <Member name="P111"/>
        </Member>
        <Member name="P12">
          <Member name="P121"/>
        </Member>
      </Member>
      <Member name="P2">
        <Member name="P21"/>
        <Member name="P22"/>
      </Member>
    </MemberList>
  </doc>
  <results>
    <MemberList>
      <Member name="P1">
        <Member name="P12">
          <Member name="P121"/>
        </Member>
      </Member>
      <Member name="P2">
        <Member name="P21"/>
        <Member name="P22"/>
      </Member>
      <Member name="P1">
        <Member name="P11"/>
        <Member name="P12"/>
      </Member>
    </MemberList>
  </results>
</search>

the wanted result is produced:

<MemberList>
   <Member name="P1">
      <Member name="P11"/>
      <Member name="P12">
         <Member name="P121"/>
      </Member>
   </Member>
   <Member name="P2">
      <Member name="P21"/>
      <Member name="P22"/>
   </Member>
</MemberList>


Hope this helped!


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"Dongling Ding" <dling61@xxxxxxxxx> wrote in message
news:20031117194453.92821.qmail@xxxxxxxxxxxxxxxxxxxxxxxxxx
> Thank Dimitre,
>
> Let me give a clearer description of the problem.
>
> We have a member structure and allow users to define
> filters based on it. We can generate the result after
> applying those filters. But the problem is that result
> is messed up since the filter was applied
> sequentially.
>
>
> Here is an example of the original member structure.
> The actual structure can be very deep in terms of
> hierarchy.
>
>
> <MemberList>
>     <Member name="P1">
> <Member name="P11">
> <Member name="P111"/>
> </Member>
> <Member name="P12">
> <Member name="P121"/>
> </Member>
>     </Member>
>     <Member name="P2">
> <Member name="P21"/>
>                 <Member name="P22"/>
>     </Member>
> </MemberList>
>
> Here there are three filers created by user:
>
> 1. select the member name "P121" and its ancestors.
> They are P121, P12, and P1.
> 2. select the member name "P2" and its children. They
> are P2, P21, and P22.
> 2. select the member name "P1" and its children. So,
> P1, P11, and P21,
>
> After those filters are applied sequentially, we got
> the following result, but it contains duplicated
> members and order of members is not the same as
> original one.
>
> <MemberList>
> <Member name="P1">
> <Member name="P12">
> <Member name="P121"/>
> </Member>
> </Member>
> <Member name="P2">
> <Member name="P21"/>
>                 <Member name="P22"/>
> </Member>
> <Member name="P1">
> <Member name="P11"/>
> <Member name="P12"/>
> </Member>
> </MemberList>
>
> Therefore, the transformation requirement is to merge
> and reorder the above result based on the original
> member structure. Dimitre suggested earlier to add one
> attribute "_pos" to indicate the member position in
> the above result. It looks like this:
>
> <Member _pos="1" name="P1"/>
>
> It helps when we have one or two levels. But we need
> to go down each level and figure out the
> position(recursive)and get rid of duplicate members.
> This transformation seems doing merge and sorting.
> Please help me on this!
>
>
> Thanks
>
>
> Dongling
>
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Protect your identity with Yahoo! Mail AddressGuard
> http://antispam.yahoo.com/whatsnewfree
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




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


Current Thread