Splitting XML message into multiple messages with generic root and common header



I am a beginner to XSLT and have spent sometime trying to figure out how to do the following transformation...


I want to divide an XML message that has one header and multiple items into multiple XML messages, where each message is having only one item element while the rest of the xml is the same.


So if we look at the following XML Input Message:



<RequestOne xmlns:h="http://ift.tt/1EsnmRM" xmlns:f="http://ift.tt/1jt69Ri" xmlns:p="http://www.w3.org/p">
<Header>
<ID>AB1234</ID>
<Number>61</Number>
</Header>

<Item>
<Name>Item1</Name>
<ItemID>I001</ItemID>
</Item>
<Item>
<Name>Item2</Name>
<ItemID>I002</ItemID>
</Item>
<Item>
<Name>Item3</Name>
<ItemID>I003</ItemID>
</Item>
</RequestOne>


I want to transform it to 3 messages having the same structure and elements, except that each message has one item of the original XML.


One important requirement is that the XSL code would be generic to any root element name and namespaces. That is, the request message's root can be <RequestOne>, <RequestTwo> ... <RequestN> and all its namespaces should be copied too.


I managed to create the following piece of code. I think it is missing copying the root element along with its namespaces.



<xsl:for-each select="//*[local-name()='Item']">
<!-- Missing here the part that would copy the root element along with the namespaces -->
<xsl:copy-of select="../Item"/>
<xsl:copy-of select="." />
</xsl:for-each>


If there is more than one way to do this, would it be possible to do it in only one template?


No comments:

Post a Comment