I have implemented this feature for the next release. For reference, example code looks like this:
 
            XQDataSource ds = new SaxonXQDataSource();
            XQConnection conn = ds.getConnection();
 
            XQPreparedExpression exp = conn.prepareExpression(
                    "declare namespace jt = 'http://saxon.sf.net/java-type';\n" +
                    "declare namespace Locale = 'java:java.util.Locale';\n" +
                    "declare variable $locale as jt:java.util.Locale external;\n" +
                    "Locale:getDisplayLanguage($locale)");
 
            XQItemType localeType = conn.createAtomicType(
                    XQItemType.XQBASETYPE_ANYATOMICTYPE,
                    new QName("http://saxon.sf.net/java-type", "java.util.Locale"),
                    null);
            XQItem italianLocale = conn.createItemFromObject(Locale.ITALIAN, localeType);
            exp.bindItem(new QName("locale"), italianLocale);
            XQResultSequence result = exp.executeQuery();
            while (result.next()) {
                XQItem item = result.getItem();
                System.out.println("Locale: " + item.getItemAsString(null));
            }
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Peter
Sent: 11 December 2007 22:17
To: 'Mailing list for SAXON XSLT queries'
Subject: Re: [saxon] XQuery XQJ and Java objects as external variables

Michael,

 

I am probably missing something trivial here but I keep on getting

 

Exception in thread "main" net.sf.saxon.javax.xml.xquery.XQException: Java object cannot be converted to an XQuery value

        at net.sf.saxon.xqj.StandardDOMHandler.convertToItem(StandardDOMHandler.java:211)

        at net.sf.saxon.xqj.StandardDOMHandler.fromObject(StandardDOMHandler.java:56)

        at net.sf.saxon.xqj.SaxonXQDataFactory.createItemFromObject(SaxonXQDataFactory.java:280)

        at net.sf.saxon.xqj.SaxonXQDynamicContext.bindObject(SaxonXQDynamicContext.java:102)

        at foo.SaxonExtJavaObject.main(SaxonExtJavaObject.java:26)

 

Example code is

 

package foo;

import java.util.Properties;

import javax.xml.namespace.QName;

import net.sf.saxon.javax.xml.xquery.XQConnection;

import net.sf.saxon.javax.xml.xquery.XQException;

import net.sf.saxon.javax.xml.xquery.XQPreparedExpression;

import net.sf.saxon.javax.xml.xquery.XQResultSequence;

import net.sf.saxon.xqj.SaxonXQDataSource;

 

public class SaxonExtJavaObject {

  public String f() {

    return "Some string";

  }

  public static void main(String[] args) throws XQException {

    SaxonXQDataSource xqds = new SaxonXQDataSource();

    XQConnection xqc = xqds.getConnection();

    String expr=

      "declare namespace sejo='java:foo.SaxonExtJavaObject'; " +

      "declare variable $user external; " +

      "sejo:f($user)";

    XQPreparedExpression e = xqc.prepareExpression(expr);

    e.bindObject(new QName("user"), new SaxonExtJavaObject(), null);

    XQResultSequence resSeq = e.executeQuery();

    System.out.println(resSeq.getSequenceAsString(new Properties()));

    }

}

 

It’s unclear to me how I should bind the SaxonExtJavaObject.

 

All help greatly appreciated!

 

Peter

 

 

 

 

From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Michael Kay
Sent: Tuesday, December 11, 2007 1:27 PM
To: 'Mailing list for SAXON XSLT queries'
Subject: Re: [saxon] XQuery XQJ and Java objects as external variables

 

Yes, you can use any of the bindXXX methods in the XQDynamicContext interface, which is implemented by XQExpression and XQPreparedExpression. For example

 

XQPreparedExpression exp = ...

 

exp.bindString(new QName("user"), "myusername", null);

 

Michael Kay

http://www.saxonica.com/

 


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Peter
Sent: 11 December 2007 12:00
To: saxon-help@lists.sourceforge.net
Subject: [saxon] XQuery XQJ and Java objects as external variables

Hello,

 

I am trying to use XQJ to pass in a Java object as an external variable

 

E.g. this could be the query

 

declare namespace u="java:package.User";

declare variable $user external;

u:getName($user)

 

I have a way of doing this using Saxon’s “proprietary” API, but can this be done using XQJ?

 

Any guidance warmly welcomed,

 

Peter