I can't get a simple example of a query using schema types to work. If I specify an expression using an element test with a type name I get elements whose types are unrelated to the specified type name. If I specify an expression using instance of I get nothing.
I have tried varous permutations, currently the code looks like this….
Any help appreciated.
Thanks.
packagehellomxquery;importjava.io.FileReader;importch.ethz.mxquery.contextConfig.CompilerOptions;importch.ethz.mxquery.contextConfig.Context;importch.ethz.mxquery.datamodel.QName;importch.ethz.mxquery.exceptions.MXQueryException;importch.ethz.mxquery.exceptions.QueryLocation;importch.ethz.mxquery.model.XDMIterator;importch.ethz.mxquery.query.PreparedStatement;importch.ethz.mxquery.query.XQCompiler;importch.ethz.mxquery.query.impl.CompilerImpl;importch.ethz.mxquery.query.parser.SchemaParser;importch.ethz.mxquery.xdmio.XDMInputFactory;importch.ethz.mxquery.xdmio.XDMSerializer;publicclassStaticTypingExperiments{staticfinalStringquery=//"import schema default element namespace \"\" at \"XPexample.xsd\";\n"//+"declare variable $doc external; "+"declare variable $v as schema-element(wot:book) := validate{$doc};"+"$v//element(*, xs:string)";publicstaticvoidmain(String...args)throwsException{Contextctx=newContext();CompilerOptionsco=newCompilerOptions();co.setSchemaAwareness(true);//Context.initDictionary();//ctx.addSchemaLocation("wot","XPexample.xsd");SchemaParser.preLoadSchema("XPExample.xsd");ctx.addNamespace("wot","wot");ctx.addTargetNamespace("wot");XQCompilercomp=newCompilerImpl();PreparedStatementstatement;XDMSerializerser=newXDMSerializer();try{statement=comp.compile(ctx,query,co);}catch(MXQueryExceptionerr){MXQueryException.printErrorPosition(query,err.getLocation());System.err.println("Error:");throwerr;}statement.addExternalResource(newQName("doc"),XDMInputFactory.createXMLInput(ctx,newFileReader("XPexample.xml"),false,//Context.NO_VALIDATION,Context.SCHEMA_VALIDATION_STRICT,QueryLocation.OUTSIDE_QUERY_LOC));XDMIteratorit=statement.evaluate();System.out.println(ser.eventsToXML(it));statement.close();ctx.getStores().freeRessources();}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've slightly modfied your example (against the trunk version, but apart from the additional parameters for SchemaParser.preLoadSchema() and Compiler.compile() there should not be any difference.
It seems that you told the parser not to return a document, but just the root element. As a result, your path expression starts from the node it should select.
I can't get a simple example of a query using schema types to work. If I specify an expression using an element test with a type name I get elements whose types are unrelated to the specified type name. If I specify an expression using instance of I get nothing.
I have tried varous permutations, currently the code looks like this….
Any help appreciated.
Thanks.
Dear peteg22,
I've slightly modfied your example (against the trunk version, but apart from the additional parameters for SchemaParser.preLoadSchema() and Compiler.compile() there should not be any difference.
It seems that you told the parser not to return a document, but just the root element. As a result, your path expression starts from the node it should select.
Best Regards,
Peter