Re: mulitple parameters [was: Special characters and XML-to-WML problem]

Subject: Re: mulitple parameters [was: Special characters and XML-to-WML problem]
From: Jeni Tennison <jeni.tennison@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Aug 2000 11:46:01 +0100
Torsten,

>> the problem is I have a <A HREF> tag in the XSLT script that I want
>> to end up looking like this in the WML file:
>> 
>> <A
>> HREF="http://myserver.com:8001/addItemToOrder?item_name=CPU&order_
>> id=0&user_name=jdoe">
>
>How can we construct an attribute URL via XSL?

Let's say that you want to vary the values of item_name, order_id and
user_name within this URL.  Let's say that you've set them as variables
somewhere, for simplicity's sake:

  <xsl:variable name="item_number" select="'CPU'" />
  <xsl:variable name="order_id" select="'0'" />
  <xsl:variable name="user_name" select="'jdoe'" />

Now, constructing a piece of text in XSLT just involves writing out what
you want to have in that text.  Where you want some normal text, use
xsl:text.  Where you want to put the value of something in, use
xsl:value-of.  So, to construct the URL above, you want:

  <xsl:text>http://myserver.com:8001/addItemToOrder</xsl:text>
  <xsl:text>?item_name=</xsl:text>
  <xsl:value-of select="$item_name" />
  <xsl:text>&amp;order_id=</xsl:text>
  <xsl:value-of select="$order_id" />
  <xsl:text>&amp;user_name=</xsl:text>
  <xsl:value-of select="$jdoe" />

If you prefer, you can use the concat() function to concatenate text and
values together:

  <xsl:value-of select="concat('http://myserver.com:8001/addItemToOrder',
                               '?item_name=', $item_name,
                               '&amp;order_id=', $order_id,
                               '&amp;user_name=', $user_name)" />

Once you have a chunk of XSLT that's generating the text, you can make it
the value of a variable or the value of an attribute or the content of an
element or anything you want.  To add some text generated like this as the
value of an attribute, you can use the xsl:attribute element within the
result element you're creating:

<a>
  <xsl:attribute name="href">
    <xsl:text>http://myserver.com:8001/addItemToOrder</xsl:text>
    <xsl:text>?item_name=</xsl:text>
    <xsl:value-of select="$item_name" />
    <xsl:text>&amp;order_id=</xsl:text>
    <xsl:value-of select="$order_id" />
    <xsl:text>&amp;user_name=</xsl:text>
    <xsl:value-of select="$jdoe" />
  </xsl:attribute>
  ...
</a>

or

<a>
  <xsl:attribute name="href">
    <xsl:value-of select="concat('http://myserver.com:8001/addItemToOrder',
                                 '?item_name=', $item_name,
                                 '&amp;order_id=', $order_id,
                                 '&amp;user_name=', $user_name)" />
  </xsl:attribute>
  ...
</a>

When you are constructing a value for an attribute that can be summarised
using just one xsl:value-of (as you are here), then you can shorten this
further by using an attribute value template.  Take the value of the select
attribute of the xsl:value-of that you're using, put it in curly brackets
(i.e. {}), and then use that as the value of the attribute declared
directly on the element.  The curly brackets tell the XSLT processor to
calculate the value of whatever is inside them and insert that value in its
place.

<a href="{concat('http://myserver.com:8001/addItemToOrder',
                 '?item_name=', $item_name,
                 '&amp;order_id=', $order_id,
                 '&amp;user_name=', $user_name)}">
  ...
</a>

If you are constructing a value for an attribute that is simply a
concatenation of strings and calculated values (as you are here), then you
can go one step further.  Forget about the concat() and just insert the
content where you want them, with the calculated values included within
curly brackets:

<a
href="http://myserver.com:8001/addItemToOrder?item_name={$item_name}&amp;ord
er_id={$order_id}&amp;user_name={$user_name}">
  ...
</a>

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



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


Current Thread