0

Is there an easy way to optimize the following for-each loops? There are 3 for-each loops. Actually it is not very performant ;-) The bottleneck is the second and third foreach

Must be Xsl1.0!

 <xsl:for-each select="ViewMasterdataMaint/MaintMessageConsist">
     <Message>
        <xsl:variable name="Message_Device">
          <xsl:value-of select="Message_Device"/>
        </xsl:variable>
        <DataStructures>

            <xsl:for-each select="msxsl:node-set(/ViewMasterdataMaint/DeviceWebPage[Device=$Message_Device])">
                <xsl:variable name="WebPageGuid" select="Page" />

                <xsl:for-each select="msxsl:node-set(/ViewMasterdataMaint/DataRecordType[Page=$WebPageGuid])[not(Structure = preceding-sibling::DataRecordType/Structure)]">
                  <xsl:variable name="FacetGuid" select="Structure" />
                    <xsl:element name="DataStructure">
                      <xsl:attribute name="name">
                        <xsl:value-of select="/ViewMasterdataMaint/DataStructure[Guid=$FacetGuid]/Name" />
                      </xsl:attribute>
                    </xsl:element>
                 </xsl:for-each>

              </xsl:for-each>
        </DataStructures>
     </Message>
</xsl:for-each>

My only idea was to switch to apply-templates. But it's a huge xslt and the postet xsl is only a small snippet of it ;-(

  • 2
    It is hard to suggest improvements to code without knowing what the code is supposed to do - see: [mcve]. It would seem that part of your code attempts to do grouping, using a very inefficient method. The preferred solution in XSLT 1.0 is to use the Muenchian grouping method: http://www.jenitennison.com/xslt/grouping/muenchian.html. I am also not sure why you need to use the node-set() function on what appears to already be a node-set. – michael.hor257k May 05 '23 at 14:10
  • This pops out as a potential slow down. "preceding-sibling::DataRecordType/Structure". If you have a lot of preceding siblings, you could potentially be searching a large section of your xml over and over again. So, I would suggest following michael.hor257k's advice and use the Muenchian method on this loop. And get rid of the node-set functions, if you can. But this is probably not a big deal. – John Ernst May 05 '23 at 14:58

1 Answers1

0

thank you for your comments. You were both right:

  1. Muenchian grouping had great impact to performance
  2. unnecessary node-set() function removed
  3. added "preceding-sibling::DataRecordType/Structure" to a key-value dictionary which is build up once at the beginning of the transformation.
  4. replaced for-each through <template...>

So execution time is reduced from 9h to 25 minutes.

Thank you very much!