Solved

Pass parameter to XSL stylesheet

Posted on 2002-06-12
4
12,894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 100 total points
ID: 7073797
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
ID: 7116087
a grade b for a full answer is ridiculous. bl from now on...
0
 

Expert Comment

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

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

Expert Comment

by:mlg4035
ID: 24212836
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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, …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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