javaxmldatabasexquerybasex

I got an Invalid XQuery syntax, syntax does not pass static validation error working with java


I'm getting this error: Invalid XQuery syntax, syntax does not pass static validation. eac4.gestors.GestorException at eac4.gestors.GestorOrdinador.inserir(GestorOrdinador.java:57) at eac4.gestors.TestGestor.bTestInserir(TestGestor.java:107) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

I'm working on an exercise with some tests provided by my professor, I have to make 5 methods and their tests run one after another, if one fails, the following will fail too. The first method has to erase everything that is inside an XML file (except the root label "ordinadors") and I've done this:

public static final String ARREL = "doc(\"ordinadors/ordinadors.xml\")/collection(\"ordinadors\")/";

public void eliminarTot() throws GestorException {
    try {
        XQExpression xqe = conn.createExpression();
        xqe.executeQuery("for $i in "+ ARREL + "ordinadors return delete node $i");
    } catch (XQException ex) {
        Logger.getLogger(GestorOrdinador.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The following method, that is the one giving the error, has to insert an object on that XML file: (little explanation: Utilitats.formaObjecteXML(ordinador) returns a String transforming an object into an XML-kind String)

public void inserir(Ordinador ordinador) throws GestorException {
    //TODO implementar mètode 
    String nom = ordinador.getNom(); 
    try { 
        XQExpression xqe = conn.createExpression(); 
        XQResultSequence xqrs = xqe.executeQuery(ARREL+"ordinadors/ordinador[nom="+nom+"]");

        if(xqrs.next()){ 
            throw new GestorException("this ordinador already exists"); 
        } else { 
            xqe.executeQuery("insert node "+Utilitats.formaObjecteXML(ordinador)+" into " +ARREL+"ordinadors"); 
        } 
    } catch (XQException ex) {
        throw new GestorException(ex.getMessage()); 
    } 
}

the fact is that I don't get where is the syntax wrong, also, this line: at eac4.gestors.GestorOrdinador.inserir(GestorOrdinador.java:57) refers to this one:

} catch (XQException ex) {
    *throw new GestorException(ex.getMessage());*
}

I don't know how to handle this. Maybe there's a mistake on the eliminarTot method or it's just on the inserir...

As the test eliminarTot is succeeding, I asume that connection with de DB (BASEX 10.5) is ok, the Libraries are also ok, and i'm using JDK 11 as my professor required. We also are told to use XQUF to interact with the DB from Netbeans (mandatory). TY all.


Solution

  • At last I've found my mistake, at the "eliminarTot()" method (delete every node inside the root tag) I changed the query: xqe.executeQuery("for $i in "+ ARREL + "ordinadors return delete node $i"); to: xqe.executeQuery("delete node "+ ARREL + "ordinadors/ordinador"); this way the root tag it's not removed. Also at the "inserir()" method I had another mistake I changed: XQResultSequence xqrs = xqe.executeQuery(ARREL+"ordinadors/ordinador[nom="+nom+"]"); to: String resultat = xqe.executeQuery(ARREL+"ordinadors/ordinador[@id="+id+"]").getSequenceAsString(null); and then the checking:

    if(resultat.isEmpty()){
                xqe.executeQuery("insert node 
    "+Utilitats.formaObjecteXML(ordinador)+" into "+ARREL+"ordinadors");
    

    That'll be all. TY for your answer Yitzhak Khabinsky :D