Re: [xsl] xslt create a variable from external xml file

Subject: Re: [xsl] xslt create a variable from external xml file
From: "Bauman, Syd s.bauman@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 22 Aug 2023 14:23:07 -0000
Do you have control over the ./inc/env.xml file? If so, my thought would be to
skip the <root> and have it say


<?xml version="1.0" encoding="UTF-8"?>

<environment>test</environment>

(Just easier for humans to read, I think.)
Given that env file the following XSLT should do the trick (untested):

  <xsl:param name="env" select="document('./inv/env.xml')/environment =>
normalize-space()" as="xs:string"/>
  <!-- ... -->
    <xsl:choose>
      <xsl:when test="$env eq 'test'">
        <xsl:text>T</xsl:text>
      </xsl:when>
      <xsl:when test="$env eq 'prod'"/>
      <xsl:otherwise>
        <xsl:message terminate="yes" select="'I cannot figure out if I am on
test or prod, so I am giving up.'"/>
      </xsl:otherwise>
    </xsl:choose>

If you do not have control over the env.xml file, use
document('./inv/env.xml')/root/environment instead. Note that using
<xsl:param> rather than <xsl:variable> is not at all necessary, it just allows
you to set the environment on any particular run by passing in a parameter.
(BTW, I have used a param for this sort of thing in the past, but never an
external file like that. From a bash script something like
  saxon -s:input.xml -xsl:transform.xslt -o:/tmp/output.xml host=$(hostname
--short)
allows me to test which machine I am running on. Warning, though, the above is
from memory, and mine is fading. :-)

Also note I did not bother with the <xsl:text></xsl:text> in the 'prod' clause
of the <choose>. Seems to me there is no point in asking the XSLT processor to
go through the hassle of creating a text node only to find there is nothing in
it, so it ends up disappearing, anyway.
________________________________

I have a need to determine what environment Im working on without hardcoding
the differences in the large xslt file.

I have tried



<xsl:variable name="externalDoc" select="document('inc/env.xml')"/>



File content

<?xml version="1.0" encoding="UTF-8"?>

<root>

    <environment>test</environment>

</root>



And in the main xslt file I use, I just want to add a T at the end.



<xsl:choose>

  <xsl:when test="$externalDoc/environment = 'test'">

    <xsl:text>T</xsl:text>

  </xsl:when>

  <xsl:when test="$externalDoc/environment = 'prod'">

    <xsl:text></xsl:text>

  </xsl:when>

</xsl:choose>





XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/649132> (by
email<>)

Current Thread