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

Subject: Re: [xsl] xslt create a variable from external xml file
From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 22 Aug 2023 14:57:11 -0000
Hm,


<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>

Or the nearly-the-same

<xsl:sequence select="'T'[$externalDoc/environment = 'test']"/>

... although it might not win any points for obviousness, and as Syd notes it
lumps the 'prod' and undefined cases together ...

It is not quite the same b/c it returns a string not text nodes.

Regards,
Wendell

From: Bauman, Syd s.bauman@xxxxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, August 22, 2023 10:24 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] xslt create a variable from external xml file

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 I'm 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)
XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/3302254> (by
email<>)

Current Thread