Reputation: 179
I'm struggling to split my XML file in multiple XML-files. I need all elements and attributes from the -element with the same status_ids in a seperate file with the status_id as the filename.
The XML-File looks somewhat like:
<some>
<more status="1" att="q" status_id="111">
<text>asdf</text>
</more>
<more status="2" att="c" status_id="111">
<text>fdas</text>
</more>
<more status="2" att="a" status_id="111">
<text>qwer</text>
</more>
<more status="1" att="w" status_id="222">
<text>yxcv</text>
</more>
<more status="2" att="f" status_id="222">
<text>vvbmn</text>
</more>
<more status="2" att="g" status_id="222">
<text>fgjh</text>
</more>
</some>
What I want: XML-File_111.xml
<some>
<more status="1" att="q" status_id="111">
<text>asdf</text>
</more>
<more status="2" att="c" status_id="111">
<text>fdas</text>
</more>
<more status="2" att="a" status_id="111">
<text>qwer</text>
</more>
</some>
XML-File_222.xml
<some>
<more status="1" att="w" status_id="222">
<text>yxcv</text>
</more>
<more status="2" att="f" status_id="222">
<text>vvbmn</text>
</more>
<more status="2" att="g" status_id="222">
<text>fgjh</text>
</more>
</some>
My XSLT so far:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/test">
<xsl:for-each select="test/some/more">
<xsl:result-document method="xml" href="file_{@status_id}.xml">
<root>
<xsl:copy-of select="//more[type = current()/status_id]/@*"/>
<elem>
<xsl:copy-of select="../@* | ." />
</elem>
</root>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
But I'm not getting anywhere with it. I get only one empty XML-File. So I'm doing something totally wrong, but what?
Upvotes: 1
Views: 1174
Reputation: 167516
Typical grouping problem:
<xsl:template match="some">
<xsl:for-each-group select="more" group-by="@status_id">
<xsl:result-document href="file_{current-grouping-key()}.xml">
<some>
<xsl:copy-of select="current-group()"/>
</some>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
Upvotes: 2