0

I have to use XSLT 1.0 with the XML below. It has a function jr:itext which I have surrogated by a user-defined function with exslt. I could strip jr:itext() and call the function explicitly as shown in the example, but it looks ugly and I have other functions in the full version.

How do I call(func-string) in XSLT`?

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/2002/xforms" xmlns:jr="http://openrosa.org/javarosa">
  <label ref="jr:itext('calculate_test_output')"/>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:jr="http://openrosa.org/javarosa" 
   xmlns:func="http://exslt.org/functions"
     extension-element-prefixes="func" >

    <func:function name="jr:itext">
      <xsl:param name="ref" />
        <func:result select="concat('itext ', $ref)"/>
    </func:function>


    <xsl:template match="/">
       Requested output
      <xsl:value-of select="jr:itext('calculate_test_output')" />,

    Using "call function from string?"
        <xsl:variable name="ref" select="html/label/@ref"/>
        <xsl:value-of select="$ref"/>
<!--      <xsl:execute-this-function select="$ref" />,-->
   
    </xsl:template>

</xsl:stylesheet>
1
  • 1
    There is exslt.org/dyn/functions/evaluate/index.html so you could try whether the processor supports <xsl:value-of select="dyn:evaluate($ref)" xmlns:dyn="http://exslt.org/dynamic"/>. I have no idea, however, whether any processor supporting EXSLT functions and EXSLT dyn:evaluate exposes the functions declared with func:function to the XPath context of the dyn:evaluate function. Commented Nov 17, 2020 at 21:45

1 Answer 1

2

Using xsltproc (xsltproc was compiled against libxml 20910, libxslt 10134 and libexslt 820) it works for me to use the EXSLT dyn:evaluate function to call both XPath 1.0 functions as well as func:function defined functions dynamically:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:jr="http://openrosa.org/javarosa" xmlns:func="http://exslt.org/functions"
    extension-element-prefixes="func">

    <func:function name="jr:itext">
        <xsl:param name="ref"/>
        <func:result select="concat('itext ', $ref)"/>
    </func:function>

    <xsl:template match="label">
        <xsl:copy>
            <xsl:value-of select="dyn:evaluate(@ref)" xmlns:dyn="http://exslt.org/dynamic"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

when run against (for simplicity of the example I used the elements in no namespace)

<html  xmlns:jr="http://openrosa.org/javarosa">
    <label ref="jr:itext('calculate_test_output')"/>
    <label ref="concat('test', ' 1')"/>
</html>

outputs

<label>itext calculate_test_output</label>
<label>test 1</label>
Sign up to request clarification or add additional context in comments.

1 Comment

Works for me with libxsl 1.1.32 (in R-library xslt), fails with Saxon 9.1.0.5. Ok for me as I am using R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.