Improve company productivity with a Business Account.Sign Up

x
?
Solved

Using environment variable from within XSLT

Posted on 2002-05-21
5
Medium Priority
?
2,733 Views
Last Modified: 2013-11-18
Hi,
I am new to XSLT.
Is it possible to use the value of a system environment variable (e.g. TEMP), from within XSLT?
0
Comment
Question by:mco
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 7025860
Since you are so new to XSLT, you must understand that XSLT is not a panacea for every coding dilemma. It was designed to carry out transforms on XML Documents which are nothing more than structured TEXT. For optimal performance and yes! there are situations where you can cause a stack overflow or crash your XSLT Engine for any parser.

One of the things that you will learn is NOT to re-invent the wheel. There are existing API calls that allow you to get system environment variables like Windows Scripting Host. XSLT is NOT a scripting language. It is not a procedural language.

::TIP::
1. Get the System Environment Variable.
2. Pass It To The Stylesheet
2a. External Values can be passed to Stylesheets either thru brute DOM Methods or via the AddParameter() Method of the IXSLProcessor (if you are using MSXML 3+)
0
 
LVL 1

Author Comment

by:mco
ID: 7026161
Could you give me a code example?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7026266
In which language? C#, VB, JScript, VBScript

On Which Side? Client Or Server?

Using What Technology? HTA, ASP, ASP.NET, COM (ActiveX)

0
 
LVL 1

Author Comment

by:mco
ID: 7026617
C++, client, COM (msxml3.dll)
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 400 total points
ID: 7027330
I didnt offer C++ as I have no working knowledge of that.

The following example requires:
1. Windows Scripting Host
2. MSIE 5+
3. MSXML3.

xml/data.xml
=============
<?xml version="1.0"?>
<files>
<file name="Windows Installer 3.0" href="msinstaller.exe" />
</files>


xslt/data.xsl
=============
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="yes" />
<xsl:param name="temp-folder" />
<xsl:template match="/">
<xsl:for-each select="//file">
<div><a href="{$temp-folder}\{@href}"><xsl:value-of select="@name" /></a></div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


data.hta
=========
<html>
<head>
<title>IXSLProcessor::MSXML3</title>
<script language="javascript">
var oShell = new ActiveXObject("WScript.Shell");
var oEnv = oShell.Environment("PROCESS");
var szTemp = oEnv("TEMP");
//clear WScript Objects
delete oEnv; oEnv = null; CollectGarbage();
delete oShell; oShell = null; CollectGarbage();
//create XSLTemplate30
var oTemplate = new ActiveXObject("Msxml2.XSLTemplate.3.0");
//create DOMDocument30
var oXML = new ActiveXObject("Msxml2.DOMDocument.3.0");
//create FreeThreadedDOMDocument30
var oXSLT = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
oXSLT.async = oXML.async = false;
//loading XSLT Document
oXSLT.load("xslt/data.xsl");
//setting stylesheet property to the XSLT Tree
oTemplate.stylesheet = oXSLT;
//clear XSLT reference - no longer needed
delete oXSLT; oXSLT = null; CollectGarbage();
oXML.load("xml/data.xml");
//create IXSLProcessor
var oProc = oTemplate.createProcessor();
//set the input property to the XML Tree
oProc.input = oXML;
//passing external values to a named parameter
oProc.addParameter("temp-folder",szTemp);
window.onload = init;
function init() {
  //perform transformation
  oProc.transform();
  document.body.innerHTML = oProc.output;

}
</script>
</head>
<body></body>
</html>
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question