RE: [xsl] Passing a variable number of parameters or rather an array

Subject: RE: [xsl] Passing a variable number of parameters or rather an array
From: "Matthieu Ricaud" <matthieu.ricaud@xxxxxxx>
Date: Thu, 21 Apr 2005 11:54:19 +0200
using XSLT 1.0, you can simulate the tokenize XSLT 2.0 function with an
simple reccursive named template, in your case something like this :

	<xsl:template name="MakeTreeFromStrList">
		<xsl:param name="strList"/>
		<xsl:choose>
			<xsl:when test="contains($strList,',')">
				<foo><xsl:value-of select="substring-before($strList,',')}"/></foo>
				<xsl:call-template name="MakeTreeFromStrList">
					<xsl:with-param name="strList" select="substring-after($strList,',')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<foo><xsl:value-of select="$strList"/></foo>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

calling the template inside a variable declaration with $sampleString as
param will make your variable a node-set, which you can interate :

	<xsl:variable name="tokenizedSample">
		<xsl:call-template name="MakeTreeFromStrList">
			<xsl:with-param name="strList" select="$sampleString"/>
		</xsl:call-template>
	</xsl:variable>

with <xsl:variable
name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>
$tokenizedSample is the folowing xml fragment :
	<foo>XML</foo>
	<foo>XSLT</foo>
	<foo>XPath</foo>
	<foo>SVG</foo>
	<foo>XPointer</foo>

which you can iterate using a node-set extension (depends on your parser)
with MSXML it looks like this :
    <xsl:for-each select="msxsl:node-set($tokenizedSample)/foo">
      <xsl:value-of select="."/>
      <xsl:text>! </xsl:text>
    </xsl:for-each>

this is untested..., and hoppefully it is not out of subjetc (i didn't read
the whole thread...)
hope this help anyway,
regards
Matthieu.

-----Message d'origine-----
De : kent [mailto:kent@xxxxxxxxxxxxxxxxx]
Envoye : mercredi 20 avril 2005 18:00
A : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : RE: [xsl] Passing a variable number of parameters or rather an
array


Thanks everyone for the help.

Argh, so I tried this approach and I even found another example of it at:
http://www.xml.com/lpt/a/2003/05/07/tr.html

And yet it doesn't work in xalan, which is frustrating:

<xsl:variable name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>

<xsl:variable name="tokenizedSample" select="tokenize($sampleString,',')"/>

    <xsl:for-each select="$tokenizedSample">
      <xsl:value-of select="."/>
      <xsl:text>! </xsl:text>
    </xsl:for-each>

does anyone know why this gives me the error:

ERROR:  'Error checking type of the expression 'funcall(tokenize,
[variable-ref(sampleString/result-tree), literal-expr(,)])'.'
FATAL ERROR:  'Could not compile stylesheet'


-----Original Message-----
From: Ramkumar Menon [mailto:ramkumar.menon@xxxxxxxxx]
Sent: Wednesday, April 20, 2005 3:49 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Passing a variable number of parameters or rather an
array

Just give this a try..... I havent tried it out myself....

Step1 :  Store it into a variable..
Step 2:  Iterate through using <for-each>

<for-each select="tokenize($category,'\s+')">
  <individualFruit>
    <xsl:value-of select="."/>
  </individualFruit>
</xsl:for-each>


On 4/20/05, kent <kent@xxxxxxxxxxxxxxxxx> wrote:
> Also a good idea, but do I have to go and save the xml as a file, is there
> no way of passing in the it in also as a parameter, performance is not a
> huge issue, but I wouldn't mind saving a bit of disk latency.
>
> More importantly how do I do array/string[1] in some sort of loop, sorry I
> am very new to xslt.
>
> thanks
>
>
> -----Original Message-----
> From: Colin Paul Adams [mailto:colin@xxxxxxxxxxxxxxxxxx]
> Sent: Wednesday, April 20, 2005 3:12 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Passing a variable number of parameters or rather an
> array
>
> >>>>> "kent" == kent  <kent@xxxxxxxxxxxxxxxxx> writes:
>
>     kent> I have had a trawl on this subject, but I the best answer I
>     kent> can get is 'You can pass a nodeset, which can be treated as
>     kent> the XML equivalent of an Array'.  However I am not sure how
>     kent> to create the nodeset, pass it, and then deal with it in the
>     kent> stylesheet.
>
>     kent> I would like to pass an array of strings to the
>     kent> stylesheet. Does anyone have an example of doing this, or
>     kent> know where one is. I looked at xalan examples and none of
>     kent> the examples seem to fit, plus I can't find one anywhere
>     kent> else.
>
> The details of how to pass stylesheet parameters to a transformer is
> specific to the particular XSLT processor.
>
> But there is a portable (*) way of doing what you want to do:
>
> Create your array as an xml document: e,g.:
>
> <array>
>  <string>string one</string>
>  <string>string two</string>
> </array>
>
> Then pass the URI of this document as a string-valued parameter, and
> use the document() function to access the array.
>
> Now you can use XPath expressions such as array/string[1] to get the
> first string-value from this document.
>
> (*) assuming that all XSLT processors allow you to pass a
> string-constant as a parameter.
> --
> Colin Paul Adams
> Preston Lancashire
>
>


--
Shift to the left, shift to the right!
Pop up, push down, byte, byte, byte!

-Ramkumar Menon
 A typical Macroprocessor

Current Thread