My xml is:
<RowSet>
<Row>
<msg_id>1</msg_id>
<doc_id>1</doc_id>
<doc_version>1</doc_version>
</Row>
<Row>
<msg_id>2</msg_id>
<doc_id>1</doc_id>
<doc_version>2</doc_version>
</Row>
<Row>
<msg_id>3</msg_id>
<doc_id>1</doc_id>
<doc_version>3</doc_version>
</Row>
<Row>
<msg_id>4</msg_id>
<doc_id>2</doc_id>
<doc_version>1</doc_version>
</Row>
<RowSet>
What I need to do:
If there are Rows with the same doc_id
, I need to select only node with the bigger doc_version
number.
Expected output:
<RowSet>
<Row>
<msg_id>3</msg_id>
<doc_id>1</doc_id>
<doc_version>3</doc_version>
</Row>
<Row>
<msg_id>4</msg_id>
<doc_id>2</doc_id>
<doc_version>1</doc_version>
</Row>
<RowSet>
May be it might be helpful: msg_id
is unique, so Row with bigger msg_id
for the same doc_id
hold the last doc_version
.
This transformation works, unlike some other answers:
When applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
Proper use of the Muenchian Grouping method for finding one item belonging to each different group.
Proper use of sorting for finding a maximum item in a group.
Proper use of the
key()
function -- for selecting all items in a given group.XSLT 1.0 Solution
The logic is:
Try this