Splitting XML with a common head node and several bodies node into multiple files with XSLT



I have an XML document, structured like the following example:



<p:Document versione="1.0">
<DocumentHeader>
global header
</DocumentHeader>
<DocumentBody>
body 1
</DocumentBody>
<DocumentBody>
body 2
</DocumentBody>
</p:Document>


Note that "global header" and "body X" may rappresent nested xml-blocks, not just plain text.


and I need to split this XML file into - in this example - two XML files, like the followings:



<p:Document versione="1.0">
<DocumentHeader>
global header
</DocumentHeader>
<DocumentBody>
body 1
</DocumentBody>
</p:Document>


and



<p:Document versione="1.0">
<DocumentHeader>
global header
</DocumentHeader>
<DocumentBody>
body 2
</DocumentBody>
</p:Document>


Generally, I have to create several new XML, one for each DocumentBody node, putting the same DocumentHeader node in each new file created.


I think an XSL transformation is the best way to do this, but I dont know how. I've tried to use the example reported here, but these cases don't manage an "header" node.



<xsl:template match="/root">
<xsl:for-each select="DocumentBody">
<xsl:result-document method="xml" href="file_{@id}-output.xml">
<root>
<xsl:copy-of select="/root/@*" />
<DocumentBody>
<xsl:copy-of select="../@* | ." />
</DocumentBody>
</root>
</xsl:result-document>
</xsl:for-each>
</xsl:template>


Can you help me?


No comments:

Post a Comment