[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: xslt create a variable from external xml file

Subject: Re: 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
Re:  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 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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.