I have an XSL file.
I have a PHP file with a XSLTProcessor named $bob
.
I want to send to my xsl transformation some parameters.
So, I write
Well decide on a separator character for your messages (i.e. a character that is ensured not to occur in a message), then pass in a string with the messages separated by that character e.g. if you choose |
you pass in $bob->setParameter('', 'message', "Hi|It's Bob|How are you?");
, then in your XSLT code to be used with libxslt use e.g.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="str">
<xsl:param name="message"/>
<xsl:variable name="msgs-rtf">
<messages>
<xsl:for-each select="str:tokenize($message, '|')">
<message>
<xsl:value-of select="."/>
</message>
</xsl:for-each>
</messages>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$msgs-rtf"/>
</xsl:template>
</xsl:stylesheet>
As far as I know that extension function str:tokenize
is supported in libxslt which PHP 5 uses for XSLT so you should be able to use it. Or you need to write a template that does the tokenization in XSLT itself
How about concatenation in PHP?
$bob->setParameter("", "message", "Hi:it's bob:How are you ?");
and tokenization in XSLT code:
<messages>
<xsl:for-each select="tokenize($message,':')">
<message>
<xsl:value-of select="." />
</message>
</xsl:for-each>
</messages>
I would just have one parameter, that is an XML document. I would create this XML document before I invoke the transformation.
This has some definit benefits compared to sending a single, pipe-delimited string:
Every parameter is in its own element and this can be matched by a separate template in XSLT.
No recursive processing of a pipe-delimited string and no extension functions are needed.
More extensible and scalable.