Solved

Pass parameter to XSL stylesheet

Posted on 2002-06-12
4
12,879 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

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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, …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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