Search code examples
xsltmarklogic

Why doesn't it see the variable inside element declaration?


The following code gives an error:

[1.0-ml] XDMP-UNDVAR: (err:XPST0008) Undefined variable $ename

However if I change the xsl:element to <xsl:element name="yyyXXX"> it shows that it sees $ename in value-of, i.e.

<yyyXXX>zzz</yyyXXX>

The stylesheet below works just fine in Saxonica 9.x

Thanks.


xquery version "1.0-ml";

xdmp:xslt-eval(
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xdmp="http://marklogic.com/xdmp"        
   extension-element-prefixes="xdmp"
    version="2.0">     

 <xsl:template match="/">
  <xsl:variable name="ename" select="'zzz'"/>
  <xsl:element name="yyy{$ename}">       
   <xsl:value-of select="$ename"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
,document{ <doc/> })

Solution

  • You need two sets of curly brackets around your element name, e.g. yyy{{$ename}}. That tells the outer XQuery to treat those as literal curly brackets in the XSLT. If you were invoking the XSLT from an external document (e.g. with xdmp:xslt-invoke) you wouldn't need the extra brackets; this is equivalent to your Saxon test. The ability to evaluate XQuery to dynamically create XSLT is pretty powerful, but does impose a little more cognitive overhead.