Skip to content

Instantly share code, notes, and snippets.

@dkarlovi
Last active March 9, 2020 23:42
Show Gist options
  • Save dkarlovi/01165f71c6bf1211b22d1efb96654d00 to your computer and use it in GitHub Desktop.
Save dkarlovi/01165f71c6bf1211b22d1efb96654d00 to your computer and use it in GitHub Desktop.
Split a large XML file into several smaller XML files

How to run

saxon -s:in.xml -xsl:split.xsl name=Products size=1200
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="size">1200</xsl:param>
<xsl:param name="name">out</xsl:param>
<xsl:variable name="timestamp">
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y0001]-[M01]-[D01]-[H01]-[m01]-[s01]')"/>
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="root" select="name(/*)" />
<xsl:for-each-group select="/*/*" group-adjacent="floor((position() - 1) div $size)">
<xsl:variable name="key">
<xsl:value-of select="format-number(current-grouping-key(), '00000')" />
</xsl:variable>
<xsl:result-document href="{$name}-{$timestamp}-{$key}.xml">
<xsl:element name="{$root}">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment