• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12958
  • Last Modified:

Pass parameter to XSL stylesheet

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
Melange
Asked:
Melange
  • 2
1 Solution
 
b1xml2Commented:
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
 
b1xml2Commented:
a grade b for a full answer is ridiculous. bl from now on...
0
 
PrithivTVCommented:
i still get an jscript exception , can someone please tell me why

JScriptException
----------------
Function expected
0
 
mlg4035Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now