Created
November 27, 2025 22:23
-
-
Save fancellu/4ca1ce38306a68ca00c54a23c2a455af to your computer and use it in GitHub Desktop.
A batch streaming XSLT processor (batches them up into multiple files each with 2 orders)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <root> | |
| <order id="ord-555" region="UK"> | |
| <line product-id="p1" qty="3"/> | |
| </order> | |
| <order id="ord-666" region="MARS"> | |
| <line product-id="p1" qty="1"/> | |
| <line product-id="p2" qty="2"/> | |
| <line product-id="p99" qty="1"/> | |
| </order> | |
| <order id="ord-777" region="EU"> | |
| <line product-id="p1" qty="4"/> | |
| </order> | |
| </root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2 files: | |
| batch-1.xml | |
| <?xml version="1.0" encoding="UTF-8"?><root><order id="ord-555" region="UK"> | |
| <line product-id="p1" qty="3"/> | |
| </order><order id="ord-666" region="MARS"> | |
| <line product-id="p1" qty="1"/> | |
| <line product-id="p2" qty="2"/> | |
| <line product-id="p99" qty="1"/> | |
| </order></root> | |
| batch-2.xml | |
| <?xml version="1.0" encoding="UTF-8"?><root><order id="ord-777" region="EU"> | |
| <line product-id="p1" qty="4"/> | |
| </order></root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
| xmlns:xs="http://www.w3.org/2001/XMLSchema" | |
| exclude-result-prefixes="#all" | |
| version="3.0"> | |
| <xsl:mode streamable="yes"/> | |
| <xsl:template match="/"> | |
| <xsl:iterate select="root/order"> | |
| <xsl:param name="batch" as="element(order)*" select="()"/> | |
| <xsl:param name="batch-id" as="xs:integer" select="1"/> | |
| <xsl:on-completion> | |
| <xsl:if test="exists($batch)"> | |
| <xsl:result-document href="output/batch-{$batch-id}.xml"> | |
| <root><xsl:sequence select="$batch"/></root> | |
| </xsl:result-document> | |
| <xsl:message>Final batch <xsl:value-of select="$batch-id"/></xsl:message> | |
| </xsl:if> | |
| </xsl:on-completion> | |
| <xsl:variable name="current-order-copy" as="element(order)"> | |
| <xsl:copy-of select="."/> | |
| </xsl:variable> | |
| <xsl:variable name="new-batch" select="($batch, $current-order-copy)"/> | |
| <xsl:choose> | |
| <xsl:when test="count($new-batch) eq 2"> | |
| <xsl:result-document href="output/batch-{$batch-id}.xml"> | |
| <root><xsl:sequence select="$new-batch"/></root> | |
| </xsl:result-document> | |
| <xsl:message>Wrote batch <xsl:value-of select="$batch-id"/></xsl:message> | |
| <xsl:next-iteration> | |
| <xsl:with-param name="batch" select="()"/> | |
| <xsl:with-param name="batch-id" select="$batch-id + 1"/> | |
| </xsl:next-iteration> | |
| </xsl:when> | |
| <xsl:otherwise> | |
| <xsl:next-iteration> | |
| <xsl:with-param name="batch" select="$new-batch"/> | |
| <xsl:with-param name="batch-id" select="$batch-id"/> | |
| </xsl:next-iteration> | |
| </xsl:otherwise> | |
| </xsl:choose> | |
| </xsl:iterate> | |
| </xsl:template> | |
| </xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment