Solved

Using environment variable from within XSLT

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create associate webpages / websites 3 79
Lines Not Lining Up 2 70
XML Removal- Powershell 4 25
Grunt script for Build Process 1 29
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

772 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