Solved

Pass parameter to XSL stylesheet

Posted on 2002-06-12
4
12,800 Views
Last Modified: 2009-04-23
Is it possible to pass a parameter from a URL to an XSL stylesheet via an XML file?

For example:

URL = "xmlfile.xml?paramvalue=datavalue"

and the XML file references the stylesheet "xslfile.xsl"

can xslfile.xsl read the value from paramvalue and then base its formatting on the value?
0
Comment
Question by:Melange
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 100 total points
Comment Utility
Here's the XML Document
=======================
<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
<?xml-stylesheet href="../xslt/url.xsl" type="text/xsl"?>
<document>This is a test</document>

Here's the XSLT Document
========================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt"
   xmlns:url="uri:find-url.org"
   exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
<!--getting url of xml document-->
<xsl:variable name="xmlpath" select="url:findURL(.)" />
<!--getting only xml document-->
<xsl:variable name="xmlname" select="url:findName(.)" />
<!--getting search for xml document-->
<xsl:variable name="search">
     <xsl:if test="contains($xmlpath,'?')">
          <xsl:call-template name="search">
               <xsl:with-param name="raw" select="substring-after($xmlpath,'?')" />
          </xsl:call-template>
     </xsl:if>
</xsl:variable>
<msxsl:script language="JScript" implements-prefix="url">
     function findURL(nodelist) {
          return nodelist.nextNode().url;
     }
     function findName(nodelist) {
          var szURL = new String(nodelist.nextNode().url);
          return szURL.substring(szURL.lastIndexOf("/") + 1,szURL.indexOf(".xml") + 4);
     }
</msxsl:script>
<xsl:template match="/">
<html>
<head>
<title>Getting URL</title>
<script language="javascript">
<xsl:comment>
<![CDATA[
var szURL = window.location.href;
var szFileName = szURL.substring(szURL.lastIndexOf("/") + 1,szURL.indexOf(".xml") + 4);
var szSearch = window.location.search;
if (szSearch.indexOf("?") == 0) szSearch = szSearch.substring(1);
window.onload = showURL;

function showURL() {
     var oFile = document.getElementById("fileurl");
     oFile.href = oFile.innerText = szURL;
     document.getElementById("filename").innerText = szFileName;
     if (szSearch.length != 0) {
          var oParameters = document.getElementById("parameters");
          var aRows = szSearch.split("&");
          var aFields;
          var szHTML = "";
          for (var i=0;i<aRows.length;i++) {
               aFields = aRows[i].split("=");
               szHTML += "<div>Parameter: <b>" + aFields[0] + "</b>; Value: <b>" + aFields[1] + "</b></div>";
          }
          oParameters.innerHTML = szHTML;
     }
     document.getElementById("filedetails").style.display = "";
}
]]>
</xsl:comment>
</script>
<style>
<![CDATA[
body {background:#ffffff;font-family:Verdana;font-size:8pt;}
a {color:#0000ff;text-decoration:none;}
a:hover {color:#800000;text-decoration:underline;}
]]>
</style>
</head>
<body>
<div><xsl:value-of select="." /></div>
<div id="filedetails" style="display:none;">
     <div>Full File Path From Script Is <a id="fileurl"></a></div>
     <div>File Name From Script Is <span id="filename"></span></div>
     <div id="parameters">No Parameters Found From Script</div>
</div>
<div id="xmlpath">
     <div>File Path From XSLT Is <a href="{$xmlpath}"><xsl:value-of select="$xmlpath" /></a></div>
     <div>File Name From XSLT Is <span><xsl:value-of select="$xmlname" /></span></div>
     <div>
          <xsl:choose>
          <xsl:when test="not(msxsl:node-set($search)/node())">No Parameters Found From XSLT</xsl:when>
          <xsl:otherwise>
               <xsl:for-each select="msxsl:node-set($search)/node()">
                    <div>Parameter: <b><xsl:value-of select="@name" /></b>; Value: <b><xsl:value-of select="@value"
/></b></div>
               </xsl:for-each>
          </xsl:otherwise>
          </xsl:choose>
     </div>
</div>
</body>
</html>
</xsl:template>

<xsl:template name="search">
<xsl:param name="raw" />
<xsl:if test="$raw and contains($raw,'=')">
     <xsl:choose>
     <xsl:when test="contains($raw,'&amp;')">
          <xsl:variable name="name" select="substring-before($raw,'=')" />
          <xsl:variable name="value" select="substring-after(substring-before($raw,'&amp;'),'=')" />
          <parameter name="{$name}" value="{$value}" />
          <xsl:call-template name="search">
          <xsl:with-param name="raw" select="substring-after($raw,'&amp;')" />
          </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
          <xsl:variable name="name" select="substring-before($raw,'=')" />
          <xsl:variable name="value" select="substring-after($raw,'=')" />
          <parameter name="{$name}" value="{$value}" />
     </xsl:otherwise>
     </xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

typing in the url like so

http://server/data/myxml.xml?var1=first&var2=second



you will see the parameter. what you want to do with it is up to you =)))
0
 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
a grade b for a full answer is ridiculous. bl from now on...
0
 

Expert Comment

by:PrithivTV
Comment Utility
i still get an jscript exception , can someone please tell me why

JScriptException
----------------
Function expected
0
 

Expert Comment

by:mlg4035
Comment Utility
I was looking for an XSLT-native way of getting parameters from the URL, but I guess there isn't one...
Still, this is a cool, easy to understand solution to the problem, thank you!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
parsing xml in javascript 2 33
VB.NET WEb Service complex type 14 57
VB.NET XML Processing a XMLNodeList with Namespace 16 61
Create XML 5 29
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now