[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Problem Dynamically Building Query String In XSLT

Subject: RE: Problem Dynamically Building Query String In XSLT 1.0
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 31 Jul 2007 17:38:29 +0100
RE:  Problem Dynamically Building Query String In XSLT
Given the limitations of 1.0, where you can't manipulate sequences of
strings, or create and then process temporary trees, I would do:

<xsl:variable name="u">
  <xsl:text>pagename.aspx?</xsl:text>
  <xsl:if test="$varTarget">
    <xsl:text>target=</xsl:text>
    <xsl:value-of select="$varTarget">
    <xsl:text>&amp;</xsl:text>
  </xsl:if>
  <xsl:if test="$varPage">
    <xsl:text>page=</xsl:text>
    <xsl:value-of select="$varPage">
    <xsl:text>&amp;</xsl:text>
  </xsl:if>
  .. repeat for other parameters
</xsl:variable>
<xsl:value-of select="substring($u, 1, string-length($u - 1))"/>

that is, add an & after every parameter, then at the end strip off the last
character of the string.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Trish@xxxxxxxxxxxxxx [mailto:Trish@xxxxxxxxxxxxxx] 
> Sent: 31 July 2007 17:25
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Problem Dynamically Building Query String In XSLT 1.0
> 
> Hi:
> 
> I need to construct a query string of an unknown number of 
> parts. I have to use XSLT 1.0. We are also purposefully not 
> using any asp in the template, so I have to rely on XSLT 1.0 
> functionality alone.
> 
> My problem is detecting whether or not the element I'm adding 
> to the query string is the first element and therefore 
> requires a "?" in front of it or whether I should use "&amp;" instead.
> 
> For instance, here's a possible URL that I want to build:
> 
> pagename.aspx?page=2&sort=default&target=newpagename.aspx
> 
> When I'm building the string, none or part or all of the 
> possible query string elements may be present. In the 
> interest of keeping the query string as short as possible, I 
> don't want to show any of the elements that don't have values 
> (aye, there's the rub). I also don't want to have one of the 
> elements always present so it is always first and eliminates 
> the problem of whether to add the &amp; or the ?.
> 
> My attempts so far have been REALLY ugly as they are 
> repetitious and code heavy, as you'll see below, as well as 
> totally unsuccessful. Has anyone else come up with a better 
> way to do this?
> 
> Here's my last desperate attempt (the variable names used are 
> the possible values for the query string elements and are 
> defined elsewhere). I was trying to change the value of 
> $paramQueryMark by submitting as a parameter to queryMark, 
> but the value of the parameter is lost when I go back to queryBuilder:
> 
> <xsl:value-of select="$varPage" />
> <xsl:call-template name="queryBuilder" />
> 
> <xsl:template name="queryBuilder">
> 	<xsl:param name="paramQueryMark" select="'false'" />
> 	<xsl:if test="string($varTarget) or 
> string($varPageNumber) or string($varSortSelected)">
> 		<xsl:text>?</xsl:text>
> 	</xsl:if>
> 	<xsl:if test="string($varTarget)">
> 		<xsl:if test="$paramQueryMark = 'true'">
> 			<xsl:text>&amp;</xsl:text>
> 		</xsl:if>
> 		target=<xsl:value-of select="$varTarget" />
> 		<xsl:call-template name="queryMark">
> 			<xsl:with-param name="paramQueryMark"
> select="'true'" />
> 		</xsl:call-template>d
> 	</xsl:if>
> 	<xsl:if test="string($varPageNumber) and $varPageNumber != '1'">
> 		<xsl:if test="$paramQueryMark = 'true'">
> 			<xsl:text>&amp;</xsl:text>
> 		</xsl:if>
> 		page=<xsl:value-of select="$varPageNumber" />
> 		<xsl:call-template name="queryMark">
> 			<xsl:with-param name="paramQueryMark"
> select="'true'" />
> 		</xsl:call-template>
> 	</xsl:if>
> 	<xsl:if test="string($varSortSelected)">
> 		<xsl:if test="$paramQueryMark = 'true'">
> 			<xsl:text>&amp;</xsl:text>
> 		</xsl:if>
> 		sort=<xsl:value-of select="$varSortSelected" />
> 		<xsl:call-template name="queryMark">
> 			<xsl:with-param name="paramQueryMark"
> select="'true'" />
> 		</xsl:call-template>
> 	</xsl:if>
> </xsl:template>
> 
> <xsl:template name="queryMark">
> 	<xsl:param name="paramQueryMark" />
> </xsl:template>
> </xsl:stylesheet>
> 
> Thanks for your help!
> Trish

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.