Note that there are some explanatory texts on larger screens.

plurals
  1. POStylesheet referencing a variable in curly braces doesn't compile
    text
    copied!<p>I am trying to perform an XSLT transformation in Java. Here is the stylesheet:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="person"&gt; &lt;xsl:variable name="varname"&gt;info&lt;/xsl:variable&gt; &lt;xsl:element name="{$varname}"&gt;content&lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>(It's a very simplified example demonstrating the problem. In fact, I really need to define the variable and then use it).</p> <p>Here is the Java code:</p> <pre><code>DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc=builder.parse(new File("C:\\temp\\test.xsl")); DOMSource xsl=new DOMSource(doc); Transformer transformer=TransformerFactory.newInstance().newTransformer(xsl); </code></pre> <p>When running, it emits an exception:</p> <pre><code>ERROR: 'Variable or parameter 'varname' is undefined.' FATAL ERROR: 'Could not compile stylesheet' javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:824) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:619) </code></pre> <p>The same stylesheet works fine in other engines (in Internet Explorer, in MSXML, in GNU xsltproc). What am I doing wrong? Maybe this feature is not supported in Java? Or maybe I should first set some parameters to the TransformerFactory or whatever?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload