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

Re: construct query string

Subject: Re: construct query string
From: Greg Faron <gfaron@xxxxxxxxxxxxxxxxxx>
Date: Tue, 07 May 2002 14:15:37 -0600
xsl query string
At 01:36 PM 5/7/2002, you wrote:
I'm attempting to construct a query string ("?param1=blah&param2=blah2",
for example) using the values of a series of tags in an XML document. My
first intuition was to create an XSL variable, and then send the
transformer into a loop which concats each param tag onto that variable.
However, after reading the W3C doc, it doesn't seem like that's feasible
because you can't "shadow" a XSLT variable in the same template context,
and you can't reference the variable you're trying to declare in the
variable definition.  TIA.

It rather depends on you XML and whether or not you're aware of the structure. Let's say, for example, that you assume that the element in question is known to not have grandchildren, and that the text within the children do not have whitespace. In other diagrams, it looks like this:
<foo>
<a>text</a>
<b/>
<c></c>
<d>more_text</d>
</foo>


  You can create "?a=text&b=&c=&d=more_text" using the stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:text>query-string = "</xsl:text>
    <xsl:apply-templates select="foo/*" mode="query"/>
    <xsl:text>"</xsl:text>
  </xsl:template>

  <xsl:template match="*" mode="query">
    <xsl:if test="position() = 1">
      <xsl:text>?</xsl:text>
    </xsl:if>
    <xsl:value-of select="local-name(.)"/>
    <xsl:text>=</xsl:text>
    <xsl:value-of select="string(.)"/>
    <xsl:if test="last() != position()">
      <xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>


If the text has whitespace, you'll need to convert it to an equivalent HTML-encoded character (i.e. a space becomes %20). the same goes for other special characters like ampersands, punctuation, etc.



Greg Faron Integre Technical Publishing Co.



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


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.