How to Split an XML file into multiple XML Files based on number of nodes
<p>This question is very similar to <a href="https://stackoverflow.com/questions/12016946/how-to-split-an-xml-file-into-multiple-xml-files">this one</a> but with a small twist.</p>

<p>I am trying to split a object representing xml to multiple xml objects based on number of tag elements allowed per object. I'm trying to get the best possible approach to this. Any help on this will be great... Sample example on what I am trying to do...</p>

<p>xml source representation:</p>

<pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;DocType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pmlcore="urn:autoid:specification:interchange:xml:schema:1"&gt;
    &lt;id&gt;tbd&lt;/id&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c1&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c2&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
&lt;/DocType&gt;
</code></pre>

<p>Desired output given that number of allowed '<strong>Tag</strong>' elements per document is ... 3</p>

<p>xml 1:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;DocType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pmlcore="urn:autoid:specification:interchange:xml:schema:1"&gt;
    &lt;id&gt;tbd&lt;/id&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c1&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
&lt;/DocType&gt;
</code></pre>

<p>xml 2:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;DocType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pmlcore="urn:autoid:specification:interchange:xml:schema:1"&gt;
    &lt;id&gt;tbd&lt;/id&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c1&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c2&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
&lt;/DocType&gt;
</code></pre>

<p>I believe by now you got idea what's the requirement but I'll continue:</p>

<p>xml 3:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;DocType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pmlcore="urn:autoid:specification:interchange:xml:schema:1"&gt;
    &lt;id&gt;tbd&lt;/id&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c2&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
&lt;/DocType&gt;
</code></pre>

<p>xml 4:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;DocType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pmlcore="urn:autoid:specification:interchange:xml:schema:1"&gt;
    &lt;id&gt;tbd&lt;/id&gt;
    &lt;Observation&gt;
        &lt;Command&gt;c2&lt;/Command&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Tag&gt;
            &lt;id&gt;....&lt;/id&gt;
            &lt;Data&gt;...&lt;/Data&gt;
        &lt;/Tag&gt;
        &lt;Data&gt;...&lt;/Data&gt;
    &lt;/Observation&gt;
&lt;/DocType&gt;
</code></pre>