Thanks for reporting this. It appears that saxon:evaluate in 7.6 only works reliably if the parameters passed as $p1, .... $pn are single items, not sequences. The following fix appears to work: change the method iterate() in net.sf.saxon.functions.Evaluate to read:
 
    public SequenceIterator iterate(XPathContext c) throws XPathException {
        PreparedExpression pexpr = prepareExpression(c);
 
        if (operation == EXPRESSION) {
            return SingletonIterator.makeIterator(new ObjectValue(pexpr));
        } else {
            for (int i=1; i<getNumberOfArguments(); i++) {
                pexpr.variables[i-1].setValue(ExpressionTool.eagerEvaluate(argument[i],c));
            }
            return pexpr.expression.iterate(c);
        }
    }
 
and make the corresponding change to the evaluateItem method:
 
    public Item evaluateItem(XPathContext c) throws XPathException {
        PreparedExpression pexpr = prepareExpression(c);
 
        if (operation == EXPRESSION) {
            return new ObjectValue(pexpr);
        } else {
            for (int i=1; i<getNumberOfArguments(); i++) {
                pexpr.variables[i-1].setValue(ExpressionTool.eagerEvaluate(argument[i],c));
            }
            return pexpr.expression.evaluateItem(c);
        }
    }
 
Michael Kay
-----Original Message-----
From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Dirk Roorda
Sent: 12 December 2003 10:04
To: saxon-help@lists.sourceforge.net
Subject: [saxon] saxon 7.8 union and saxon:evaluate problem

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:saxon="http://saxon.sf.net/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

<!--
This stylesheets illustrates a problem in saxon 7.8.

Its occurs using saxon:evaluate and unions.
                 ==============     ======
There are two manifestions: sometimes the program crashes, and sometimes results are wrong.

There is a sensitivity to equivalent but different ways to do things.

For example,

    saxon:evaluate(concat('$p1/element/',$name,'/@attribute'),$nodeset)

should be equivalent to

    saxon:evaluate(concat('$p1/',$name,'/@attribute'),$nodeset/element)

but the former leads to a crash if $nodeset is the result of a union;
while the latter succeeds, but it does not yield all values.

As a check, when you run

    <xsl:for-each select="$nodeset/element/*>
        <xsl:value-of select="@attribute"/>
    </xsl:for-each>

you do get all values.

I have not found a way to work around this yet.


In the real life situation, I want to read lots of variables. A subset of these variables comes from a big xml
file under control of the user. Another subset of the variables comes from a fixed xml file, and yet another
subset comes from an xml file that has been created on the fly.
I want to collect them all in a big set, so that the stylesheets no longer can see where they came from.
Either there is something in the union mechanism and in saxon:evaluate that I do not understand, or there are
some subtle bugs in 7.8.

Below are some experiments, with the resulting output in comments.

I run the examples in this stylesheet on a dummy xml input:

====================================================================

<?xml version='1.0'?>
<dummy/>

====================================================================

You also must have xml files to be read in by the XPATH document() function.
Here are they

===settings1.xml====================================================

<?xml version='1.0'?>
<settings>
    <setting1 v="value1"/>
</settings>

====================================================================

===settings2.xml====================================================

<?xml version='1.0'?>
<settings>
    <setting2 v="value2"/>
</settings>

====================================================================


===settingsall.xml====================================================

<?xml version='1.0'?>
<settings>
    <setting1 v="value1"/>
    <setting2 v="value2"/>
</settings>

====================================================================

By the way, I was running saxon7.8 on Windows XP and using this java:
java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

No special commandline options were passed to saxon.
-->

<!-- get the settings from the two files -->
<xsl:variable name="settings1" select="document('settings1.xml')"/>
<xsl:variable name="settings2" select="document('settings2.xml')"/>

<!-- combine all settings into one node set, this could be done in several ways
     here are two ways -->
<xsl:variable name="settings" select="($settings1 | $settings2)/settings"/>
<xsl:variable name="settingsalt" select="($settings1/settings) | ($settings2/settings)"/>

<!-- for comparison, take the same settings from a single file, so that union
    is not used -->
<xsl:variable name="settingsall" select="document('settingsall.xml')/settings"/>

<!-- it will turn out that it works better if we descend to the settings level later on
    when we use the node sets -->
<xsl:variable name="settingstop" select="$settings1 | $settings2"/>
<xsl:variable name="settingstoprev" select="$settings2 | $settings1"/>

<!-- the problem does not occur when we fetch the settings statically, but only
    if we use saxon:evaluate -->
<xsl:variable name="name1" select="'setting1'"/>
<xsl:variable name="name2" select="'setting2'"/>

<!-- we need to get an ' inside a saxon:expression -->
<xsl:variable name="apos">'</xsl:variable>

<xsl:template match="/">

    <!--static fetching of values-->

    <xsl:message>STATIC setting1=[<xsl:value-of select="$settings/setting1/@v"/>]</xsl:message>
    <xsl:message>STATIC setting2=[<xsl:value-of select="$settings/setting2/@v"/>]</xsl:message>

    <!-- yields expected results:

STATIC setting1=[value1]
STATIC setting2=[value2]

    -->

    <!--dynamic fetching of values from $settings-->
    
    <!-- for the moment skip this example because it leads to a crash
    <xsl:message>DYNAMIC(A) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(A) setting2=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(A-alt) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settingsalt)"/>]</xsl:message>
    <xsl:message>DYNAMIC(A-alt) setting2=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settingsalt)"/>]</xsl:message>
    -->
    <!--
        !======================
        fails: program crashes!
        !======================

Exception in thread "main" java.lang.AssertionError: Attempting to access a sequence as an item
        at net.sf.saxon.value.Value.asItem(Value.java:65)
        at net.sf.saxon.expr.VariableReference.evaluateItem(VariableReference.java:166)
        at net.sf.saxon.functions.Evaluate.iterate(Evaluate.java:140)
        at net.sf.saxon.expr.Atomizer.iterate(Atomizer.java:77)
        at net.sf.saxon.expr.AtomicSequenceConverter.iterate(AtomicSequenceConverter.java:82)
        at net.sf.saxon.functions.StringJoin.evaluateItem(StringJoin.java:39)
        at net.sf.saxon.expr.ComputedExpression.evaluateAsString(ComputedExpression.java:283)
        at net.sf.saxon.instruct.ValueOf.processLeavingTail(ValueOf.java:88)
        at net.sf.saxon.instruct.Instruction.process(Instruction.java:152)
        at net.sf.saxon.instruct.Instruction.processChildren(Instruction.java:194)
        at net.sf.saxon.instruct.Message.processLeavingTail(Message.java:64)
        at net.sf.saxon.instruct.Instruction.processChildrenLeavingTail(Instruction.java:261)
        at net.sf.saxon.instruct.Sequence.processLeavingTail(Sequence.java:144)
        at net.sf.saxon.instruct.Template.expand(Template.java:105)
        at net.sf.saxon.instruct.Template.processLeavingTail(Template.java:74)
        at net.sf.saxon.Controller.applyTemplates(Controller.java:326)
        at net.sf.saxon.Controller.run(Controller.java:210)
        at net.sf.saxon.Controller.transformDocument(Controller.java:1599)
        at net.sf.saxon.Controller.transform(Controller.java:1417)
        at net.sf.saxon.Transform.processFile(Transform.java:688)
        at net.sf.saxon.Transform.doMain(Transform.java:394)
        at net.sf.saxon.Transform.main(Transform.java:67)

    -->

    <!-- the following does not crash, so the union is really involved -->

    <xsl:message>DYNAMIC(A-all) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settingsall)"/>]</xsl:message>
    <xsl:message>DYNAMIC(A-all) setting2=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settingsall)"/>]</xsl:message>

    <!-- output is normal:

DYNAMIC(A-all) setting1=[value1]
DYNAMIC(A-all) setting2=[value2]

    -->

    <!--dynamic fetching of values from $settingstop: we descend to the settings level in the second
        parameter of saxon:evaluate -->
    
    <xsl:message>DYNAMIC(B) count=<xsl:value-of select="count($settingstop/settings)"/></xsl:message>
    <xsl:for-each select="$settingstop/settings/*">
        <xsl:message>DYNAMIC(B-for) <xsl:value-of select="concat(name(),'=[',@v,']')"/></xsl:message>
    </xsl:for-each>
    <xsl:message>DYNAMIC(B-top) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settingstop/settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(B-top) setting2=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settingstop/settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(B-1) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settings1/settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(B-2) setting2=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settings2/settings)"/>]</xsl:message>

    <!-- yields unexpected results: no crash, but not both variables are fetched!

DYNAMIC(B) count=2
DYNAMIC(B-for) setting2=[value2]
DYNAMIC(B-for) setting1=[value1]
!==========================
DYNAMIC(B-top) setting1=[]
!==========================
DYNAMIC(B-top) setting2=[value2]
DYNAMIC(B-1) setting1=[value1]
DYNAMIC(B-2) setting2=[value2]

    !=============================================
    Why is setting1 not fetched in the union case?
    !=============================================
    -->

    <!--dynamic fetching of values from $settingstoprev-->
    
    <xsl:message>DYNAMIC(C) count=<xsl:value-of select="count($settingstoprev/settings)"/></xsl:message>
    <xsl:for-each select="$settingstoprev/settings/*">
        <xsl:message>DYNAMIC(C-for) <xsl:value-of select="concat(name(),'=[',@v,']')"/></xsl:message>
    </xsl:for-each>
    <xsl:message>DYNAMIC(C-top) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name1,'/@v'),$settingstoprev/settings)"/>]</xsl:message>
    <xsl:message>DYNAMIC(C-top) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1/',$name2,'/@v'),$settingstoprev/settings)"/>]</xsl:message>

    <!--yields same results: same variable is not fetched

DYNAMIC(C) count=2
DYNAMIC(C-for) setting2=[value2]
DYNAMIC(C-for) setting1=[value1]
!==========================
DYNAMIC(C-top) setting1=[]
!==========================
DYNAMIC(C-top) setting1=[value2]

    !===========================
    Why is setting1 not fetched?
    !===========================
    -->

    <!--dynamic fetching of values from other ways to specify the nodes-->
    
    <xsl:message>DYNAMIC(D) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1[name()=',$apos,$name1,$apos,']/@v'),($settings1 | $settings2)/settings/*)"/>]</xsl:message>
    <xsl:message>DYNAMIC(D) setting1=[<xsl:value-of select="saxon:evaluate(concat('$p1[name()=',$apos,$name2,$apos,']/@v'),($settings1 | $settings2)/settings/*)"/>]</xsl:message>

    <!--yields same results: same variable is not fetched

!==========================
DYNAMIC(D) setting1=[]
!==========================
DYNAMIC(D) setting1=[value2]

    !===========================
    Why is setting1 not fetched?
    !===========================
    -->
</xsl:template>
    
</xsl:stylesheet>

------------------------------------------------------- This SF.net email is sponsored by: IBM Linux Tutorials. Become an expert in LINUX or just sharpen your skills. Sign up for IBM's Free Linux Tutorials. Learn everything from the bash shell to sys admin. Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click _______________________________________________ saxon-help mailing list saxon-help@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/saxon-help