How to Split an XML file by rows!!

I want to split the xml file by 5,000 records. Furthermore, I need to merge the files back to one pdf file. I can split the xml file by each record but I am not sure how to accomplish by 5k. I am sure it goes into the “for-each” but I can’t find the syntax for it.

<?xml version=“1.0” encoding=“UTF-8”?>
<xsl:stylesheet version=“2.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=“xml” version=“1.0” encoding=“UTF-8” indent=“yes”/>
<xsl:template match=“/”>

&lt;xsl:for-each select="Root/Record"&gt;
     &lt;xsl:result-document href="[file:///{encode-for-uri('{WATCHTEMPFOLDER}')}{format-number(position(),'000000000')}.xml](file:///{encode-for-uri('{WATCHTEMPFOLDER}')}{format-number(position(),'000000000')}.xml)"&gt;
      &lt;Root&gt;
          &lt;xsl:apply-templates select="."/&gt;
      &lt;/Root&gt;
     &lt;/xsl:result-document&gt;
    &lt;/xsl:for-each&gt; 
&lt;/xsl:template&gt;

<xsl:template match=“@|node()“>
<xsl:copy>
<xsl:apply-templates select=”@
|node()”/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Check out https://stackoverflow.com/questions/24538999/xslt-split-an-xml-file-by-number-of-records-and-split-file-if-element-text-diff, it actually seems to have been asked (and resolved) by a Workflow user.

I actually saw that and tried something like this but it still didn’t work.

My XML file:

xxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxx

I tried the following code in the XML Splitter and I believe it achieves what you’re looking for:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
&lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
    &lt;xsl:param name="recordNum" select="2"/&gt;
    &lt;xsl:template match="/"&gt;
        &lt;xsl:for-each-group select="Root/Record" group-adjacent="(position()-1) idiv $recordNum"&gt;
         &lt;xsl:result-document href="file:///{encode-for-uri('{WATCHTEMPFOLDER}')}{format-number(position(),'000000000')}.xml"&gt;
          &lt;Root&gt;
             &lt;xsl:apply-templates select="current-group()"/&gt;
          &lt;/Root&gt;
         &lt;/xsl:result-document&gt;
        &lt;/xsl:for-each-group&gt; 
    &lt;/xsl:template&gt;
  &lt;xsl:template match="@*|node()"&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select="@*|node()"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

Thank you phil

Phil,

I have one more question in regards to this. Because the file I am using is very large and that’s the reason I am needing it to split. Unfortunately, workflow is saving that file in memory. I would like to split the files in JavaScript and save it in disk space. Afterwards drop a trigger file that would start another process. What’s the best possible way to accomplish this task?

Not sure I understand what you mean by “Unfortunately, workflow is saving that file in memory”. As I stated in my reply, the code I posted must be used in the XML Splitter task (not the XSLT task). The splitter then creates as many distinct files as the result of the transform code dictates (creating them on disk, not in memory) and the rest of the process loops though each of these files just like any splitter task does.

Am I misunderstanding your question?

I am using an xml splitter task. It actually writes to “Wrote file:///C%3A%2FProgramData%2FObjectif%20Lune%2FPlanetPress%20Workflow%208%2FPlanetPress%20Watch%2FDebug%2Fxml00ZYWDGFTKAQI01%2F000000001.xml”

Is this not memory?

No, that’s a file, and it’s only the first file in the loop.

For testing purposes, try putting a Send To Folder task immediately after the Splitter task. You’ll see that after running the process, that destination folder will contain as many small files as the splitter created.