Solved

Pass parameter to XSL stylesheet

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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