I spend a lot of time with googling but have not found the answer or solution. In XSLT 2, there is possibility to cast variable to a node-set. So something like this work.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="var" as="element()">
<element><subelement>A</subelement><subelement>B</subelement></element>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$var/subelement[1]" />-->
</xsl:template>
</xsl:stylesheet>
The output is
<?xml version="1.0" encoding="UTF-8"?>
<subelement>A</subelement>
But when I try this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="par" />
<xsl:template match="/">
<xsl:copy-of select="$par/subelement[1]" />
</xsl:template>
</xsl:stylesheet>
and pass value <element><subelement>A</subelement><subelement>B</subelement></element>
to the XSLT processor (Saxon, Altova) as parameter "par", then I got error:
Error in XPath 2.0 expression
Type error XPTY0004: Expected a node - current item is '<element><subelement>A</subelement><subelement>B</subelement></element>' of type xs:string
Ok, I can live without that but I'm just wondering why xsl:variable and xsl:param behave different in this.