I have an app that currently uses Xalan (which I'm trying to move over to Saxon) to run some XSLT and pass in some parameters. One of these parameters is a NodeSet. In some paths through the code there is only one element in it and in some there are multiple, so it always gets put in a NodeSet to be consistent.

Either (for a single node):
        org.jaxen.dom.XPath oPathEffective = new org.jaxen.dom.XPath(sElementPathEffective);
        oNode = (Node) oPathEffective.selectSingleNode(doc);
        if(oNode!= null)
        {
                NodeSet oNodeSetEffective = new NodeSet(oNode);
                oTransformer.setParameter("xnlSelectedElements", oNodeSetEffective );
        }

or (for multiple nodes):
        ArrayList oNodeListEffective = (ArrayList) oPathEffective.selectNodes(doc);
        NodeSet oNodeSetEffective = new NodeSet();
        Iterator oIter = oNodeListEffective.iterator();
        while (oIter.hasNext())
        {
                Node oNode = (Node) oIter.next();
                oNodeSetEffective.addElement(oNode);
        }
        oTransformer.setParameter("xnlSelectedElements", oNodeSetEffective);

I know the code isn't great anyway, but that's how its written at the moment.

This works under Xalan, however when I switch to using Saxon using:
        System.setProperty("javax.xml.transform.TransformerFactory",
                "net.sf.saxon.TransformerFactoryImpl");

I get errors thrown when I try to access the variable, for example when I try:

        <xsl:value-of select="$xnlSelectedElements[1]/@OID"/>
I get an error:
Required item type of first operand of '/' is node(); supplied value has item type java:org.apache.xpath.NodeSet

In the code paths that only ever pass one element into it I tried changing the code to pass a Node rather than a NodeSet:


        Node oNode = (Node) oPathEffective.selectSingleNode(doc);
        oTransformer.setParameter("xnlSelectedElements", oNode);

This fixes the problem for that case, as a Node is passed which is what Saxon seems to be expecting. I can't find a way though to get it to let me use the NodeSet I'm passing in, it always gives me that error. Is there something stupid I'm doing wrong? It seems pretty trivial but I'm obviously missing something. Any pointers would be greatly appreciated.


Cheers,
Kevin