I want to do a transformation from XML to text combining some elements, but avoiding duplicates in the output.
The XML would be something like that:
<A>
<B>
<param1>value0</param1>
<param2>value1</param2>
</B>
<B>
<param1>value2</param1>
<param2>value3</param2>
</B>
<C>
<param3>valueC1</param3>
<D>
<param4>value0</param4>
<param5>value4</param5>
</D>
<D>
<param4>value0</param4>
<param5>value5</param5>
</D>
<D>
<param4>value2</param4>
<param5>value6</param5>
</D>
</C>
<C>
<param3>valueC2</param3>
<D>
<param4>value0</param4>
<param5>value5</param5>
</D>
</C>
</A>
And the output:
OBJECT: param1=value0, param2=value1, param3=valueC1, param4=value0;
OBJECT: param1=value2, param2=value3, param3=valueC1, param4=value2;
OBJECT: param1=value0, param2=value1, param3=valueC2, param4=value0;
Notes:
- For every D object, look for a match with B objects using D.param4 = B.param1
- If there are two or more D objects into the same C and matching with the same B, print only one of them (in the example, nothing is done with the second D object because it would produce the same line that the first one)
- If there are two D objects matching with the same B, but into different C’s, print both (third row in the output example)
I looked for some similar question, but I couldn’t find any in the same case.
I guess that it could be done by using keys, but it’s too complex.
Thanks!
Regards,
Ale.
PS: Sorry for my English.
Given that you’re not making any use of
param5in your output it would appear to be possible to simplify the problem toparam1matches theparam4of any of the contained DsThis is one way to achieve that using templates.
“find all distinct B elements whose
param1matches theparam4of any of the contained Ds” is actually very straightforward due to the fact that when you pass a node set as the second argument to thekeyfunction it does precisely this – it returns the set of all nodes whose key value is the string value of any of the nodes in the argument node set, and the returned node set (being a set) is guaranteed to contain no duplicates.