Problem
The current issue is that with below XSLT code, when the input payload is large for example 80MB, the input payload is getting loaded into memory and because of which its consuming the memory and causing performance issue. So is there any way to handle this issue in XSLT3.0?
If we use Streaming will it resolve the issue?
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<TOBRequest>
<xsl:apply-templates select="TOBRequest/MessageHeader"/>
<TOBRequest>
<xsl:for-each select="TOBRequest/TOB">
<xsl:variable name="PEG" select="SAData/PEGroup"/>
<!-- Apply templates, skipping few segments -->
<xsl:apply-templates select="@* | node()[not(name() = 'Item' or name() = 'CDE'/>
<xsl:for-each select="Item">
<Item>
<xsl:apply-templates select="@* | node()"/>
<xsl:variable name="ItemID" select="ID"/>
<xsl:variable name="Category" select="CategoryCode"/>
<xsl:for-each select="../Item[HRelationship/ParentItemID = $ItemID]">
<xsl:choose>
<xsl:when test="$Category = 'ABC' and not(contains($PEG, 'XYZ'))">
<SubItem>
<xsl:apply-templates select="@* | node()"/>
</SubItem>
</xsl:when>
<xsl:when test="contains($PEG, 'XYZ')">
<SubItem>
<xsl:apply-templates select="@* | node()"/>
</SubItem>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</Item>
</xsl:for-each>
</xsl:for-each>
</TransportationOrderBooking>
</n0:TransportationOrderBookingRequest>
</xsl:template>
<!-- Template for copying elements -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Template for copying attributes -->
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
Your XSLT code isn’t well-formed so it’s hard for me to verify. The code isn’t streamable as written, because you visit the children/descendants of TOBRequest/TOB
several times – each of the three instructions
<xsl:variable name="PEG" select="SAData/PEGroup"/>
<xsl:apply-templates select="@* | node()[...]"/>
<xsl:for-each select="Item">
does a downward selection. A further problem is
<xsl:apply-templates select="TOBRequest/MessageHeader"/>
The typical solution would be (a) if the TOB elements are of manageable size, do <xsl:for-each select="copy-of(TOBRequest/TOB)">
so that each TOB is constructed as an in-memory subtree and the processing within each TOB isn’t streamed, and (b) handle the MessageHeader elements in the same loop as the TOB elements, so it becomes:
<xsl:for-each select="copy-of(TOBRequest/*)">
<xsl:if test="self::MessageHeader">...</xsl:if>
It would be easier to help you with the rewrite if you supplied working XSLT code and a specimen document.
2
below is the formatted.
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<TOBRequest>
<xsl:apply-templates select="TOBRequest/MessageHeader"/>
<TOB>
<xsl:for-each select="TOBRequest/TOB">
<xsl:variable name="PEG" select="SAData/PEGroup"/>
<!-- Apply templates, skipping 'Item' and 'ShippingTypeCode' -->
<xsl:apply-templates select="@* | node()[not(name() = 'Item' or name() = 'SParty')]"/>
<xsl:for-each select="Item">
<Item>
<xsl:apply-templates select="@* | node()"/>
<xsl:variable name="ItemID" select="ID"/>
<xsl:variable name="Category" select="CategoryCode"/>
<xsl:for-each select="../Item[HRelationship/ParentItemID = $ItemID]">
<xsl:choose>
<xsl:when test="$Category = 'PKG' and not(contains($PEG, 'STO'))">
<SubItem>
<xsl:apply-templates select="@* | node()"/>
</SubItem>
</xsl:when>
<xsl:when test="contains($PEG, 'STO')">
<SubItem>
<xsl:apply-templates select="@* | node()"/>
</SubItem>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</Item>
</xsl:for-each>
</xsl:for-each>
</TOB>
</TOBRequest>
</xsl:template>
<!-- Template for copying elements -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Template for copying attributes -->
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
I can’t make a suggestion for streaming without seeing the input and the wanted result but in terms of performance with a non-streaming approach I think you can use a key for
<xsl:variable name="ItemID" select="ID"/>
<xsl:variable name="Category" select="CategoryCode"/>
<xsl:for-each select="../Item[HRelationship/ParentItemID = $ItemID]">
i.e. by declaring a top-level
<xsl:key name="parent" match="Item" use="HRelationship/ParentItemID">
and then rewriting the three lines as
<xsl:variable name="ItemID" select="ID"/>
<xsl:variable name="Category" select="CategoryCode"/>
<xsl:for-each select="key('parent', $ItemID)">
That hopefully improves performance with non-streaming code.
I would also suggest to rewrite <xsl:apply-templates select="@* | node()[not(name() = 'Item' or name() = 'CDE'/>
as <xsl:apply-templates select="@*, node() except (Item, CDE)"/>
.