0

I am trying to find the duplicate country in the following xml

<Forms>
    <Form_1>
        <Country>AFG</Country>
        <Country>AFG</Country>
        <Country>IND</Country>
    </Form_1>
    <Form_1>
        <Country>IND</Country>
        <Country>USA</Country>
    </Form_1>
</Forms>

Using this XPath expression

Forms//Form_1/Country[.=./following::Country]

It does give me the duplicate countries as AFG and IND but I want to find the duplicate countries within the scope of the parent Form_1 and not the whole document.

So for the above XML, my expected outcome is only AFG because it is duplicate within the one parent.

What should be adjustment that I should make to the xpath to get this desired result?

1
  • 1
    Which version of XSLT? Commented Apr 1 at 10:14

1 Answer 1

-1

Found the solution. I just needed to replace following with following-sibling

So the final Xpath would be

Forms//Form_1/Country[.=./following-sibling::Country]
Sign up to request clarification or add additional context in comments.

4 Comments

This is not the best solution, see here why: jenitennison.com/xslt/grouping/muenchian.html.
What if the input has 3 or more occurrences of AFG instead of just 2? Your expression will select all of them except the last one.
@y.arazim I do not want to select the last one, I just want to know the duplicate countries, no matter how many times they are repeated. When I will receive the result I will distinct them, so the final list that I will get will be the countries which are appearing more than once within one form.
I don't think you understand my point: you will not select the last one. But you will select all the other occurrences of a duplicate value, not just one of them. I don't know what you mean by "I will distinct them". If you have a tool that can return distinct values, you should use a completely different method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.