XSL Transformation - Merge elements



I am trying to merge 2 xml files (as strings in the code).



<Root>
<AMA>
<Profile>
<UniqueID id="3"/>
<Name type="UN">TOTO</Name>
<Address>AAA</Address>
</Profile>
<Profile>
<UniqueID id="4"/>
<Name>TOTA</Name>
<Address>BBB</Address>
</Profile>
<Profile>
<UniqueID id="5"/>
<Name>TOTQ</Name>
</Profile>
<Profile>
<UniqueID id="6"/>
<Name>TOTG</Name>
</Profile>
<Profile>
<UniqueID id="7"/>
<Name>TOTB</Name>
<Address>CCC</Address>
</Profile>
</AMA>
<External>
<Profile>
<UniqueID id="3"/>
<Miles>5</Miles>
</Profile>
<Profile>
<UniqueID id="4"/>
<Miles>4</Miles>
<Points>22222</Points>
</Profile>
<Profile>
<UniqueID id="5"/>
<Miles>3</Miles>
</Profile>
<Profile>
<UniqueID id="6"/>
<Miles>2</Miles>
</Profile>
<Profile>
<UniqueID id="7"/>
<Miles>1</Miles>
</Profile>
</External>
</Root>


I want to obtain



<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
<Profile>
<UniqueID id="3"/>
<Name type="UN">TOTO</Name>
<Address>AAA</Address>
<Miles>5</Miles>
</Profile>
<Profile>
<UniqueID id="4"/>
<Name>TOTA</Name>
<Address>BBB</Address>
<Miles>4</Miles>
<Points>22222</Points>
</Profile>
<Profile>
<UniqueID id="5"/>
<Name>TOTQ</Name>
<Miles>3</Miles>
</Profile>
<Profile>
<UniqueID id="6"/>
<Name>TOTG</Name>
<Miles>2</Miles>
</Profile>
<Profile>
<UniqueID id="7"/>
<Name>TOTB</Name>
<Address>CCC</Address>
<Miles>1</Miles>
</Profile>
</Root>


I managed to write something like:



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
<Root>
<xsl:template match="/Root/AMA/Profile">
<Profile>
<xsl:for-each select=".">
<xsl:copy-of select="@*|node()"/>
<xsl:copy-of select="/Root/External/Profile/UniqueID[@id=current()/UniqueID/@id]/../(Miles|Points)"/>
</xsl:for-each>
</Profile>
</xsl:template>
<xsl:template match="/root/External"/>
</xsl:transform>
</Root>


How is it possible to avoid "hardcoding" the Profile node? It seems another method exists, using the XSLT Lookup Tables and here... But it seems a little overkill.


Do you think there is a better solution?


No comments:

Post a Comment