0

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.

Jirka Š.
  • 3,388
  • 2
  • 15
  • 17

1 Answers1

0

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Martin, thank you for your answer. I use Altova directly from their editor and Saxon from commandline. I think you are absolutelly right. I misunderstood the manner how processor handle input parameters - I suppose it is really only as string. Mea culpa. In my first example I actually construct the xml fragment while in second I just pass string. I haven't tried it yet but I think,if I used this: `` I would obtain exactly the same behaviour as with param. So I apologize for stupid question:-) – Jirka Š. May 28 '13 at 05:36
  • Yes, you can't put markup unescaped into an XML attribute (so `select="'A...'"` is not going to work) but if you had for instance `<![CDATA[A...]]>` then I think you would got the same problem that you currently get when passing in your param value from the command line. – Martin Honnen May 28 '13 at 08:44
  • Thank you again for your time, your answers were helpful for me. – Jirka Š. May 28 '13 at 15:10