I'll look at it in the morning. Looks like an optimization gone wrong. Saxon is supposed to be rewriting string-length($href) > 0) as boolean($href) (to avoid computing the length, which can be expensive because of the possible presence of surrogate pairs) and perhaps this is going wrong.

Michael Kay
Saxonica

On 02/12/2011 00:06, Norman Walsh wrote:
Hi Michael,

In my XProc processor, when a "p:when" condition is evaluated,
such as this one:

  <p:when test="string-length($href) > 0">

I attempt to compute the effective boolean value of that expression
by evaluating "boolean(string-length($href) > 0)".

In the course of debugging, I discovered the weirdest thing. The
result of evaluating "boolean(string-length($href) > 0)" isn't true() or
false(), it's the value of $href.

I've manufactured a test case that reproduces the problem. I must
certainly be using one of the APIs in some incorrect way. If you could
tell me how, I'd appreciated it.

(Some parts of this test case certainly look bizarre in isolation, I
assure you it looks less so in context.)

package com.xmlcalabash.drivers;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.SaxonApiUncheckedException;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

public class SaxonTest {
    public static void main(String[] args) throws SaxonApiException, IOException, URISyntaxException {
        SaxonTest main = new SaxonTest();
        main.run(args);
    }

    public void run(String[] args) throws SaxonApiException, IOException, URISyntaxException {
        Vector<XdmItem> results = evaluateXPath("boolean(string-length($href) > 0)");
        System.err.println(results.get(0));
    }

    protected Vector<XdmItem> evaluateXPath(String xpath) throws SaxonApiException {
        Processor processor = new Processor(false);
        Hashtable<String,String> nsBindings = new Hashtable<String, String> ();
        Hashtable<QName,String> globals = new Hashtable<QName, String> ();

        globals.put(new QName("", "href"), "test");

        XdmNode doc = null;
        Vector<XdmItem> results = new Vector<XdmItem> ();

        XPathCompiler xcomp = processor.newXPathCompiler();

        for (QName varname : globals.keySet()) {
            xcomp.declareVariable(varname);
        }

        for (String prefix : nsBindings.keySet()) {
            xcomp.declareNamespace(prefix, nsBindings.get(prefix));
        }
        XPathExecutable xexec = xcomp.compile(xpath);
        XPathSelector selector = xexec.load();

        for (QName varname : globals.keySet()) {
            XdmAtomicValue value = new XdmAtomicValue("irrelevant");
            selector.setVariable(varname,value);
        }

        if (doc != null) {
            selector.setContextItem(doc);
        }

        try {
            Iterator<XdmItem> values = selector.iterator();
            while (values.hasNext()) {
                results.add(values.next());
            }
        } catch (SaxonApiUncheckedException saue) {
            throw saue;
        }

        return results;
    }
}

                                        Be seeing you,
                                          norm



------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d


_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help