Search code examples
xsltxslt-1.0exslt

func:function return result tree fragment


I am using Xalan-j 2.7.1. I have written a function using xalans implementation of exslt func:function extensions. I am trying to make my xslt cleaner by using repeatable portion of output xml into functions. The following function is a representation of what I am trying to do.

The expected output is a xml tree fragment but I am not seeing any output. I dont know why this doesn't work though it is mentioned in exslt.org documentation

xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:func="http://exslt.org/functions" 
   xmlns:common="http://exslt.org/common"
   xmlns:my="http://my.org/my"
   exclude-result-prefixes="func common my"> 
   <xsl:output type="xml" indent="yes" /> 

   <func:function name="my:personinfo"> 
       <xsl:param name="name" /> 
       <xsl:param name="address" /> 
    <func:result>
       <xsl:element name="details"> 
            <xsl:element name="name" select="$name" />
            <xsl:element name="address" select="$address" />  
       </xsl:element>          
    </func:result> 
   </func:function> 

   <xsl:element name="results">
         <xsl:value-of select="my:personinfo('john', '02-234 pudding lane, london')" />
   </xsl:element> 
</xsl:stylesheet>

Solution

  • Well if you have nodes in a result tree fragment and want to output them to the result tree you need to use <xsl:copy-of select="my:personinfo('john', '02-234 pudding lane, london')"/>, not value-of.

    Note however that xsl:element does not take a select attribute, if you want to create elements either simply use literal result elements like

    <details>
      <name><xsl:value-of select="$name"/></name>
      <address><xsl:value-of select="$address"/></address>
    </details>
    

    or if you want to use xsl:element make sure you populate elements with the proper syntax e.g.

    <xsl:element name="name"><xsl:value-of select="$name"/></xsl:element>