Solved

Using environment variable from within XSLT

Posted on 2002-05-21
5
2,420 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 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 59
Where do I learn ES6? 3 77
SQL XML ALL Nodes Compare in function 2 37
Need VB Script to scan through XML file and export content to csv file 9 38
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

809 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