Re: [xsl] Passing variables into XSLT via parameters ... can't get it to work

Subject: Re: [xsl] Passing variables into XSLT via parameters ... can't get it to work
From: Duncan Anker <danker@xxxxxxxxxxxxx>
Date: Wed, 24 May 2006 11:10:19 +1000
Hi David,

David Carlisle wrote:
<xsl:value-of select="exslt:node-set($parameters)//*[name() = ....

value-of produces the string value of whatever is selected, so there you
are selecting a node set but coercing it to a string by taking all its
character data and losing the element structure.
For the purposes of what I am currently trying to do, there should only ever be one element in the node set.

You probably want xsl:copy-of
Perhaps. I was anticipating replacing certain elements with string values so value-of seemed okay, although in the event of needing to pass HTML in maybe I will need to switch, I will keep it in mind.
You may or may not need exslt:node-set() it depends on how $parameters
is defined, which you don't show. Since it's not defined in this
template it must be a global parameter or variable in which case
it may well already be a node set.
$parameters is defined as a string passed in from the processor, and my understanding was that I needed to coerce to a node set explicitly; it would be something like

<namespace1 variable1="foo" variable2="bar" />
<namespace2 variable1= ... />

This may not be the ideal way to do it; What I was hoping to achieve was a way to pass in arbitrary variables without having to hardcode variable names into the stylesheet and preferably avoiding having to make 2 passes - initial experimentation had the HTML fragments as stylesheets to format the dynamic content and then the result passed into a layout stylesheet. Storing the HTML as content and being able to use aditional XML seems a bit cleaner.

I may actually have to rethink the whole thing again, since it occurred to me that I won't be able to use variables in attributes via this method, but for now that's what I have. I did another test and it seems that when I define $parameters in the stylesheet it is a node set and when I pass it in it is a string - I am not surprised that it is a string, that is whay I am using exslt:node-set in the first place, however maybe it is not coercing correctly. The difference between the node set and the string would probably be the root cause of my problem, although I thought I was working with a node-set because of the explicit call there.

[As an aside, I couldn't get the substring-before and -after to work directly in the XPath expression. Am I missing something, or do they need to be assigned to variables like that to be used?]

You can always replace a variable by its value in XPath, no expression
requires variables.
That's what I thought, so was confused when it didn't happen. The thing I was missing, as pointed out by Kasimier Buchcik, is the use of the current() function because the context node changes in a predicate.
> I know the string is making it in because
<xsl:value-of select="$parameters" /> prints out the escaped string.

by escape do you mean &lt; etc? value-of does not do any escaping in
that sense. If value-of is producing that then $parameters does not
contain an XML tree but rather a string containing XML markup in which
That was what I meant. I was using value-of to confirm that $parameters had a value when it was being passed in. As mentioned above, it does appear that my problem is a node set vs a string depending on internal or external use.
case you need to parse it with an XML parser first. Some processors
provide extension functions giving access to an XML parser or you could
parse it outside xslt and pass in the parsed document node as a
parameter. (I don't know the details of php/libxslt here)
I haven't seen any way to pass a parsed document node into libxslt, I don't believe it can be done. It's possible to call PHP functions from the stylesheet but I would rather keep them language-agnostic where possible, which means doing it in xslt. I had thought that exslt:node-set *was* the external XML parser but evidently not. I'll look into that.

Thanks for your help.


Current Thread