I looked at How to remove duplicate XML nodes using XSLT and other related questions, but they all seem about removing duplicates if the whole node is a duplicate. What I want to do is remove a node only if one properties within it matches a property within another node.
In my xml, I have 2 termTypes; Nd and Pt. Pts should be used. Where the system finds an Nd, the termName of it should not be used, instead, the name of the Pt referenced in relation termName should be used instead.
But something has gone wrong and some Nds have the same name as the name of the Pts they should be referencing. These terms are irrelevant and I need to delete them
I have:
<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Nd</termType>
<relation>
<relationType>USE</relationType>
<termId>2</termId>
<termName>A</termName>
</relation>
</term>
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Pt</termType>
</term>
<term>
<termId>3</termId>
<termUpdate>Add</termUpdate>
<termName>C</termName>
<termType>Nd</termType>
<relation>
<relationType>USE</relationType>
<termId>4</termId>
<termName>D</termName>
</relation>
</term>
<term>
<termId>4</termId>
<termUpdate>Add</termUpdate>
<termName>D</termName>
<termType>Pt</termType>
</term>
Is it possible to use xslt (or some other method) to go through and see that, if the <termName> of an Nd <term> matches the <termName> of a Pt term in its <relation>, delete the whole term? Terms referenced in <relation> are always Pt terms.
Output:
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Pt</termType>
</term>
<term>
<termId>3</termId>
<termUpdate>Add</termUpdate>
<termName>C</termName>
<termType>Nd</termType>
<relation>
<relationType>USE</relationType>
<termId>4</termId>
<termName>D</termName>
</relation>
</term>
<term>
<termId>4</termId>
<termUpdate>Add</termUpdate>
<termName>D</termName>
<termType>Pt</termType>
</term>
Thanks!
This transformation:
when applied on the provided XML document (wrapped into a top element to make it well-formed):
produces the wanted, correct output:
Explanation:
The identity rule copies every node “as is”.
There is just one template that overrides the identity template. It matches any
termwhosetermTypechild has a value ofNd. This template only calls theidentitytemplate if there is notermwith the same value of itsternamechild and whosetermTypechild has a value ofPt. This test is performed using a conveniently definedxsl:key