bzak
asked on
How to read a query string with XSL
I need to get a value from a query string and use it in an xsl:if statment. How is this done. I am running my app on Apache.
ASKER
How do I use:
xslProc.addParameter("pMyP aram", getParam('foo'));
in an xsl:if statement?
xslProc.addParameter("pMyP
in an xsl:if statement?
This:
xslProc.addParameter("pMyP aram", getParam('foo'));
sets a parameter in the XSLT like:
<xsl:param name="pMyParam"/>
so that during transformation, the parameter functions as if it was created like:
<xsl:param name="pMyParam">bar</xsl:p aram>
Which is used like (for example, assuming the querystring was foo=bar):
<xsl:if test="$pMyParam = 'bar'">foo equals bar</xsl:if>
For a complete example of setting parameters during transformation, look at my dynamic filter and sort example:
http://rdcpro.com/Members/rdcpro/snippets/filterandsort/
This sets nodelists as parameters, as well, which has interesting implications.
Regards,
Mike Sharp
xslProc.addParameter("pMyP
sets a parameter in the XSLT like:
<xsl:param name="pMyParam"/>
so that during transformation, the parameter functions as if it was created like:
<xsl:param name="pMyParam">bar</xsl:p
Which is used like (for example, assuming the querystring was foo=bar):
<xsl:if test="$pMyParam = 'bar'">foo equals bar</xsl:if>
For a complete example of setting parameters during transformation, look at my dynamic filter and sort example:
http://rdcpro.com/Members/rdcpro/snippets/filterandsort/
This sets nodelists as parameters, as well, which has interesting implications.
Regards,
Mike Sharp
In any case, you'll need to use whatever flavor of a template processor you have available to you...like Xalan or whatever. Just make sure it has the ability to set parameters externally.
Regards,
Mike Sharp
Regards,
Mike Sharp
ASKER
Ok, now for the tricky part....
I am importing two templates into a master....
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="fpForm_ST.xsl"/>
<xsl:import href="fpTemplate.xsl"/>
</xsl:stylesheet>
The query string variable needs to be read by fpTemplate.xsl. This does not work. It only works if I remove the import statement for fpForm.xsl.
I am importing two templates into a master....
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="fpForm_ST.xsl"/>
<xsl:import href="fpTemplate.xsl"/>
</xsl:stylesheet>
The query string variable needs to be read by fpTemplate.xsl. This does not work. It only works if I remove the import statement for fpForm.xsl.
ASKER
Actually I was able to get it to work with your example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-m icrosoft-c om:xslt"
xmlns:url="uri:find-url.or g"
exclude-result-prefixes="m sxsl">
<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
<xsl:variable name="xmlpath" select="url:findURL(.)" />
<xsl:variable name="search">
<xsl:if test="contains($xmlpath,'? ')">
<xsl:value-of select="substring-after($x mlpath,'?' )" />
</xsl:if>
</xsl:variable>
<msxsl:script language="JScript" implements-prefix="url">
function findURL(nodelist)
{
return nodelist.nextNode().url;
}
</msxsl:script>
<html>
<link href="style.css" rel="stylesheet" type="text/css" />
<body>
<xsl:for-each select="planName">
<xsl:text>The search string is: </xsl:text>
<xsl:value-of select="$search" />
</xsl:for-each>
Bu I am not sure how to cut up the string....
I am getting:
The search string is: test1=Test&test2=test
I want to use the value of test1 in the if statement
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-m
xmlns:url="uri:find-url.or
exclude-result-prefixes="m
<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
<xsl:variable name="xmlpath" select="url:findURL(.)" />
<xsl:variable name="search">
<xsl:if test="contains($xmlpath,'?
<xsl:value-of select="substring-after($x
</xsl:if>
</xsl:variable>
<msxsl:script language="JScript" implements-prefix="url">
function findURL(nodelist)
{
return nodelist.nextNode().url;
}
</msxsl:script>
<html>
<link href="style.css" rel="stylesheet" type="text/css" />
<body>
<xsl:for-each select="planName">
<xsl:text>The search string is: </xsl:text>
<xsl:value-of select="$search" />
</xsl:for-each>
Bu I am not sure how to cut up the string....
I am getting:
The search string is: test1=Test&test2=test
I want to use the value of test1 in the if statement
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://dev.rdcpro.com/Members/rdcpro/snippets/xsltquerystrings/
The "right" way to do it is to pass the query string in server-side as a parameter to the transform. But this would require some sort of server-side scripting/programming language, which you haven't mentioned. Here's how I've done it client-side, using IE6:
function getParam(strParam)
{
strQueryString = decode(window.location.sea
iTerm = strQueryString.indexOf(str
if (iTerm != -1)
{
strSubQuery = strQueryString.substring(i
iTermEnd = strSubQuery.indexOf("&")
if (iTermEnd == -1)
{
iTermEnd = strSubQuery.length
}
strSubQuery = strSubQuery.substring(0, iTermEnd)
}
else
{
strSubQuery = null
}
return strSubQuery
}
// URL Decodes the string
function decode(str) {
return unescape(str.replace(/\+/g
}
Then when calling the transform, you add the parameter to the XSLT like:
xslProc.addParameter("pMyP
Regards,
Mike Sharp