Menu

Schema type in element test and instance of

Help
2012-02-27
2013-06-05
  • Pete Geraghty

    Pete Geraghty - 2012-02-27

    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.

    package hellomxquery;
    import java.io.FileReader;
    import ch.ethz.mxquery.contextConfig.CompilerOptions;
    import ch.ethz.mxquery.contextConfig.Context;
    import ch.ethz.mxquery.datamodel.QName;
    import ch.ethz.mxquery.exceptions.MXQueryException;
    import ch.ethz.mxquery.exceptions.QueryLocation;
    import ch.ethz.mxquery.model.XDMIterator;
    import ch.ethz.mxquery.query.PreparedStatement;
    import ch.ethz.mxquery.query.XQCompiler;
    import ch.ethz.mxquery.query.impl.CompilerImpl;
    import ch.ethz.mxquery.query.parser.SchemaParser;
    import ch.ethz.mxquery.xdmio.XDMInputFactory;
    import ch.ethz.mxquery.xdmio.XDMSerializer;
    public class StaticTypingExperiments {
      static final String query =
    //          "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)";
      public static void main(String... args) throws Exception {
        Context ctx = new Context();
        CompilerOptions co = new CompilerOptions();
        co.setSchemaAwareness(true);
    //    Context.initDictionary();
    //    ctx.addSchemaLocation("wot", "XPexample.xsd");
        SchemaParser.preLoadSchema("XPExample.xsd");
        ctx.addNamespace("wot", "wot");
        ctx.addTargetNamespace("wot");
        XQCompiler comp = new CompilerImpl();
        PreparedStatement statement;
        XDMSerializer ser = new XDMSerializer();
        try {
          statement = comp.compile(ctx, query, co);
        } catch (MXQueryException err) {
          MXQueryException.printErrorPosition(query, err.getLocation());
          System.err.println("Error:");
          throw err;
        }
        statement.addExternalResource(new QName("doc"), XDMInputFactory
                .createXMLInput(ctx,
                                new FileReader("XPexample.xml"),
                                false,
    //                            Context.NO_VALIDATION,
                                Context.SCHEMA_VALIDATION_STRICT,
                                QueryLocation.OUTSIDE_QUERY_LOC));
        XDMIterator it = statement.evaluate();
        System.out.println(ser.eventsToXML(it));
        statement.close();
        ctx.getStores().freeRessources();
      }
    }
    
     
  • Peter M. Fischer

    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

    package ch.ethz.mxquery.test.bugreports;
        import java.io.FileReader;
        import ch.ethz.mxquery.contextConfig.CompilerOptions;
        import ch.ethz.mxquery.contextConfig.Context;
        import ch.ethz.mxquery.datamodel.QName;
        import ch.ethz.mxquery.exceptions.MXQueryException;
        import ch.ethz.mxquery.exceptions.QueryLocation;
        import ch.ethz.mxquery.model.XDMIterator;
        import ch.ethz.mxquery.query.PreparedStatement;
        import ch.ethz.mxquery.query.XQCompiler;
        import ch.ethz.mxquery.query.impl.CompilerImpl;
        import ch.ethz.mxquery.query.parser.SchemaParser;
        import ch.ethz.mxquery.xdmio.XDMInputFactory;
        import ch.ethz.mxquery.xdmio.XDMSerializer;
        public class StaticTypingExperiments {
          static final String query =
                  "import schema namespace wot =\"wot\";\n" +
                  "declare variable $doc external; "
                  + "$doc//element(*,xs:string)";
          public static void main(String [] args) throws Exception {
            Context ctx = new Context();
            CompilerOptions co = new CompilerOptions();
            co.setSchemaAwareness(true);
    //      Context.initDictionary();
    //      ctx.addSchemaLocation("wot", "XPexample.xsd");
            SchemaParser.preLoadSchema(Context.getGlobalContext(),"bugreports/SchemaParsing/XPExample.xsd");
            //ctx.addNamespace("wot", "wot");
            //ctx.addTargetNamespace("wot");
            XQCompiler comp = new CompilerImpl();
            PreparedStatement statement;
            XDMSerializer ser = new XDMSerializer();
            try {
              statement = comp.compile(ctx, query, co,null,null);
            } catch (MXQueryException err) {
              MXQueryException.printErrorPosition(query, err.getLocation());
              System.err.println("Error:");
              throw err;
            }
            statement.addExternalResource(new QName("doc"), XDMInputFactory
                    .createXMLInput(ctx,
                                    new FileReader("bugreports/SchemaParsing/XPExample.xml"),
                                    true,
    //                              Context.NO_VALIDATION,
                                    Context.SCHEMA_VALIDATION_STRICT,
                                    QueryLocation.OUTSIDE_QUERY_LOC));
            XDMIterator it = statement.evaluate();
            System.out.println(ser.eventsToXML(it));
            statement.close();
            ctx.getStores().freeRessources();
          }
        }
    
     

Log in to post a comment.