Project

Profile

Help

How to access non premitive Java Object like HashMap in HE

Added by Saurabh Jain almost 5 years ago

Here is my XQuery function

declare variable $javaArgs as jt:java.util.HashMap external;

declare variable $testXpath := //BOOKLIST/BOOKS/ITEM/TITLE/text();

declare function local:testJavaArgs($parameter as xsd:string) { trace($javaArgs, 'Java Args') };

0

How to do I access key value pair of javaArgs in HE


Replies (5)

Please register to reply

RE: How to access non premitive Java Object like HashMap in HE - Added by Saurabh Jain almost 5 years ago

Or how do I create inbuilt XQuery Map in Java and pass it as argument to XQuery

RE: How to access non premitive Java Object like HashMap in HE - Added by Michael Kay almost 5 years ago

To access an external Java object you need to use reflexive extension functions, and these require Saxon-PE or higher. The details are documented at http://www.saxonica.com/documentation/index.html#!extensibility/functions and if you are having difficulty following that, it would be helpful to know how far you got, and where you got stuck.

To create an XDM (XQuery) map in Java you can either build it up incrementally using new XdmMap() and XdmMap.put(key, value), or you can convert a Java map using the static factory method XdmMap.makeMap(java.util.map).

RE: How to access non premitive Java Object like HashMap in HE - Added by Saurabh Jain almost 5 years ago

I was able to make it work this way, thanks again for the excellent tool !!!

	Processor proc = new Processor(false);
	XQueryCompiler comp = proc.newXQueryCompiler();

	XdmNode document = proc.newDocumentBuilder().build(new File("data/books.xml"));

	XQueryEvaluator qe;

	String fun = new String(Files.readAllBytes(Paths.get("query/funTest.xq")));
	qe = comp.compile(fun).load();
	qe.setContextItem(document);
			
	Map<XdmAtomicValue, XdmValue> javaArgs = new HashMap<>();
	javaArgs.put(new XdmAtomicValue("key"), new XdmAtomicValue(1));
	XdmMap xdmMap = new XdmMap(javaArgs);
	
	qe.setExternalVariable(new QName("javaArgs"), new XdmValue(xdmMap));
	
	XdmValue xdmValue = qe.callFunction(new QName("http://www.w3.org/2005/xquery-local-functions", "testJavaArgs"),
			new XdmValue[] {new XdmAtomicValue("1/1/2")});
	System.out.println(xdmValue);

RE: How to access non premitive Java Object like HashMap in HE - Added by Michael Kay almost 5 years ago

Only one comment on that: you don't need to do new XdmValue(XdmMap), because the XdmMap already is an XdmValue.

    (1-5/5)

    Please register to reply