Here is a solution that uses some of the nice new features in XSLT 2.0:
This conversion is :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/*"> <xsl:variable name="vTop" select="."/> <xsl:for-each-group select="Item" group-by="@group"> <xsl:result-document href="file:///C:/Temp/file{position()}.xml"> <xsl:element name="{name($vTop)}"> <xsl:copy-of select="current-group()"/> </xsl:element> </xsl:result-document> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
when applied to the Xml document provided by the OP (fixed to be correctly formed!):
<List> <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::"> Hello World! </Item> <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::"> Goodbye World! </Item> <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::"> This example text should be in the first file </Item> <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::"> This example text should be in the second file </Item> <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::"> Hello World! </Item> </List>
creates the required two files : file1.xml and file2.xml
source share