I am working with XSLT 1.0 through a Java project, and I am unable to update to 2.0. I am attempting to write a Parent template that should call within it a Child template. I wish for the Parent template to receive a param that is a list of elements (even just words), that I can quickly define in the moment I'm calling the template, and for each element in that list, to call the Child template, with that specific element as a parameter.
I have attempted to simply define an XML Array and pass it within the parameter's body:
<xsl:call-template name="Parent">
<xsl:with-param name="element-list">
<element-list type="array">
<value>FirstElement</value>
<value>SecondElement</value>
</element-list>
</xsl:with-param>
</xsl:call-template>
<xsl:template name="Parent">
<xsl:param name="element-list"/>
<xsl:for-each select="$element-list">
<xsl:variable name="element" select="."/>
<xsl:call-template name="Child">
<xsl:with-param name="element" select="$element"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
But I get an exception thrown with the following message:
Invalid conversion from 'com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl' to 'node-set'
From what I have learned, this solution would work with XSLT 2.0, but as I have said, that is not an available option for me. If the solution is still to work with simple XML Arrays, and just interact with them in a different way, it eludes me.
In short, I want to pass a list param, defined on the spot, to the Parent template, that will then call the Child template for each element of that list, using the element itself as a parameter for the Child.
How can I do that?
I know how to define an Array in XML. I define an Array in XML in the single example I provided. Defining an Array in XML does not solve my problem, because once I pass the Array that I defined in XML through the param, I get an exception thrown. I believe it would be enough to define an Array in XML and pass it through the param if I were using XSLT 2.0. I am not. Because of the circumstances of the work project I am editing, I am stuck with XSLT 1.0.
In XSLT 1.0 your element-list parameter is a result tree fragment and needs to be converted to a node-set before it can be processed as such (e.g. by xsl:for-each). Practically every XSLT 1.0 processor supports an extension function for such conversion, so you should be able to do:
<xsl:template name="Parent">
<xsl:param name="element-list"/>
<xsl:for-each select="exsl:node-set($element-list)/element-list/value">
<xsl:variable name="element" select="."/>
<xsl:call-template name="Child">
<xsl:with-param name="element" select="$element"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
after declaring xmlns:exsl="http://exslt.org/common".
P.S. I am not aware of an array type in XML and certainly declaring it as such is meaningless in XSLT. And BTW in Java you should be able to use an XSLT 3.0 processor such as Saxon.