How do I de-duplicate a list of nodes in XSLT - and return the last node encountered?

Posted by Broam on Stack Overflow See other posts from Stack Overflow or by Broam
Published on 2010-04-28T18:58:15Z Indexed on 2010/04/29 16:27 UTC
Read the original article Hit count: 348

Filed under:

I've seen lots of "de-duplicate this xml" questions but everyone wants the first node or the nodes are identical. I have a bit of a bigger puzzle.

I have a list of articles in XML, a relevant snippet is shown:

<item><key>Article1</key><stamp>100</stamp></item>
<item><key>Article1</key><stamp>130</stamp></item>
<item><key>Article2</key><stamp>800</stamp></item>
<item><key>Article1</key><stamp>180</stamp></item>
<item><key>Article3</key><stamp>900</stamp></item>
<item><key>Article3</key><stamp>950</stamp></item>
<item><key>Article4</key><stamp>990</stamp></item>
<item><key>Article5</key><stamp>999</stamp></item>

I'd like a list of nodes where the keys are unique and where the last instance is returned, not the first: Stamp (integer) is always increasing for elements of a particular key. Ideally I'd like "largest stamp" but they're always in order so the shortcut is ok.

Desired result: (Order doesn't really matter.)

<item><key>Article2</key><stamp>800</stamp></item>
<item><key>Article1</key><stamp>180</stamp></item>
<item><key>Article3</key><stamp>950</stamp></item>
<item><key>Article4</key><stamp>990</stamp></item>
<item><key>Article5</key><stamp>999</stamp></item>

I'm somewhat confused on how to get this list. Any ideas?

I'm using the Saxon processor if it matters.

© Stack Overflow or respective owner

Related posts about xslt-2.0