xsltProcessor setParameter : send an undefined number of params

前端 未结 3 1877
眼角桃花
眼角桃花 2021-01-23 04:24

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

相关标签:
3条回答
  • 2021-01-23 04:44

    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

    0 讨论(0)
  • 2021-01-23 04:45

    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>
    
    0 讨论(0)
  • 2021-01-23 04:53

    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:

    1. Every parameter is in its own element and this can be matched by a separate template in XSLT.

    2. No recursive processing of a pipe-delimited string and no extension functions are needed.

    3. More extensible and scalable.

    0 讨论(0)
提交回复
热议问题