0

We have a site that uses XSLT. There is a config.xslt file that declares a few global variables and it's included in a bunch of other stylesheets. The issue is that we host the site in a few different places and we tweak some of these variables depending on the environment.

Usually, we would create both a main config file and a local config file to keep the local changes out of version control. The local config file can override any of the variables that it needs to without modifying the main config file. However, it is my understanding that this would not be possible within XSLT since XSLT variables can only be declared once and you can't check if they've been declared already.

My question then is: Is it possible to implement such a thing in XSLT?

If not, what would be a better way of managing our configuration?

Edit: Thanks to Martin Honnen for suggesting xsl:import. I've looked up how to use import and it does what I want. However, it leaves me with another issue where the local config file must always exist or else the whole application breaks. Since I want to remove the local config file from version control it's not completely ideal. I found this answer but it's for xslt-2.0 and I'm unfortunately stuck with xslt-1.0.

dashingdove
  • 304
  • 1
  • 4
  • 15

1 Answers1

1

I can suggest two options.

  1. Add your config as a parameter to your XSL transformer, prior to doing the transformation. How you do that depends on what transformer you are using. The config could be XML or a set of name=value pairs.

or

  1. Use a config.xml file to populate an xsl:param at the top of your XSLT. Read the values using document(). Something like this:
<xsl:param name="config">
  <xsl:choose>
    <xsl:when test="document('C:\config\config.xml')">
      <xsl:value-of select="document('C:\config\config.xml)/config" />
    </xsl:when>
    <xsl:otherwise><config/></xsl:otherwise>
  </xsl:choose>
</xsl:param>

(If you do this, the config will always be at least <config />)

Bryn Lewis
  • 580
  • 1
  • 5
  • 14
  • I really like your second suggestion but I get an error with the when clause if the file doesn't exist. Are you sure you can check for file existence in this way? – dashingdove Jul 17 '20 at 08:48
  • hmm, it seems to depend on the transformer. You might need a minimal file at least. – Bryn Lewis Jul 17 '20 at 08:56