Re: [xsl] How to cope with the complexity of an XSLT program with thousands of template rules?

Subject: Re: [xsl] How to cope with the complexity of an XSLT program with thousands of template rules?
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 Jun 2022 12:40:41 -0000
Hi Roger,

Thousands!!? I get nervous when there's dozens of templates. :)  Thanks for
posting your favorite set of complexity-reducing tricks.

I had one stylesheet where I needed to inline some elements with @idref
references, then reorganize the resulting content. I tried doing everything in
one pass with clever use of <xsl:apply-templates> and <xsl:next-match>, but I
always seemed to mishandle some corner case.

I eventually gave in and implemented multiple passes:

  <!-- TOP-LEVEL PASS - apply passes 1 through 4 to document -->
  <xsl:template match="/">
    <xsl:variable name="pass1-results" as="document-node()">
      <xsl:apply-templates select="/" mode="pass1"/>
    </xsl:variable>

    <xsl:variable name="pass2-results" as="document-node()">
      <xsl:apply-templates select="$pass1-results" mode="pass2"/>
    </xsl:variable>

    <xsl:variable name="pass3-results" as="document-node()">
      <xsl:apply-templates select="$pass2-results" mode="pass3"/>
    </xsl:variable>

    <xsl:variable name="pass4-results" as="document-node()">
      <xsl:apply-templates select="$pass3-results" mode="pass4"/>
    </xsl:variable>

    <xsl:sequence select="$pass4-results"/>
  </xsl:template>

That solved the problem and avoided problematic template interactions, but it
made me feel defeated because I resorted to a linear way of solving the
problem.

If anyone is curious, more details about the problem are available at

https://blog.oxygenxml.com/topics/preprocessing_ditaot_project_files.html

and the XSLT file itself is at

https://blog.oxygenxml.com/topics/preprocessing_ditaot_project_files/preproce
ss_project_file.xsl

 - Chris

Current Thread