xsl-list
[Top] [All Lists]

Re: [xsl] Finding the first among equals using XPath

2006-06-22 10:37:08
Peter,

This is a de-duplicating problem.

As in the Muenchian technique for grouping, de-duplication can be achieved using keys.

For example, if you have a key declaration:

<xsl:key name="WXY" match="X" use="concat(generate-id(ancestor::W),'#',@Y)"/>

Then, given any W as context node,

<xsl:for-each select=".//X
  [generate-id() =
   generate-id(key('WXY',concat(generate-id(ancestor::W),'#',@Y))[1])]">

will iterate only over the first X elements that appear inside the W with a given @Y value.

This logic can be uncompressed if you need it to be clearer. (But note: untested, and David has already one-upped me once today. :-)

The solution is elusive because the problem usually appears in a different form.

Cheers,
Wendell

At 11:45 AM 6/22/2006, you wrote:
I am sure this is a hoary old one, but I could not work out the right
runes to achieve it, nor the right keywords to search for to find an
answer

If I have XML like the following:

<V>
 <W>
  <Z/>
      <X Y='0'/>
   <X Y='0'/>
    <X Y='1'/>
  <Z/>
  </W>
 <W>
  <Z/>
     <Z/>
      <X Y='0'/>
    <X Y='1'/>
     <X Y='2'/>
     </W>
  <W>
    <X Y='2'/>
     <X Y='2'/>
      <X Y='2'/>
  <Z/>
     <Z/>
         </W>
</V>

I a have a for-each loop over the W elements. Inside the for-each I
want to select one X with each value of Y that exists within that W.

So for the above example, for //W[1] I want to get the first and third
or second and third X elements, for //W[2] I want all three X elements
and for //W[3] I want any one of the X elements, but only one of them.

I hope that bit is clear.

I have run out of ideas now, so I have finally admitted defeat (a hard
thing for an engineer) and am asking for your collective wisdom.

I want a node-set so I can do a for-each on that, so I would prefer to
find a select specifier based on clever use of axes and predicates.


--~------------------------------------------------------------------
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>
--~--

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