Question

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.

Was it helpful?

Solution

How do you use those processors? I think from the command line all you can provide is an XPath expression or a primitive value but not a node or sequence of nodes. But using the API http://www.saxonica.com/documentation/index.html#!javadoc/net.sf.saxon.s9api/XsltTransformer@setParameter you should be able to pass in a sequence of nodes. As an alternative if you want to pass in a string with markup to be parsed into nodes then use the extension function saxon:parse-xml or the XSLT 3.0 function http://www.w3.org/TR/xpath-functions-30/#func-parse-xml on the string value (only supported in the commercial versions of Saxon I think).

It seems with AltovaXML http://manual.altova.com/AltovaXML/altovaxmlcommunity/index.html?axjavaxslt2.htm you are restricted to pass in a string interpreted as an XPath expression; as XPath can't construct new nodes you can't directly pass in nodes and as AltovaXML does not seem to support an extension function to parse a string with XML markup into nodes I think with Altova you are indeed more restricted than with Saxon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top