how delete root element?!?

Hello everyone,
i need to delete an xml instance doc!
My first idea it’s to do somethink like:

  public void doDelete(String AuthorName) {
        if(dataSource.chkConnection()) {
                dataSource=new DataSource();
        }
        String q="update delete input()/Author[AuthorName='"+AuthorName+"']";
        TXQuery query = TXQuery.newInstance(q);
		TXMLObjectAccessor xmlObjectAccessor = dataSource.getTXMLObjectAccessor();
		try {
       		TResponse response = xmlObjectAccessor.xquery( query );
        }
		catch (TException insertException) {
		    System.out.println( "Delete failed!" );
		    System.out.println( insertException );
		}
		finally{
            dataSource.close();
        }
    }</pre><BR><BR>but i had:<BR><pre class="ip-ubbcode-code-pre">  NestedException:Tamino access failure (INOXQE6450, Update results in non-well-formed document, Attempt to delete the root element)



I think maybe i need to use a dom object!
I know how get a document from tamino but i don’t know how delete it!

Thank you!

I think the problem is that you are trying to delete the root element(Author), in which a update delete query may not work.

try:
1) getting the inoid of the Author/document you are trying to delete.

2) Create an TXMLObject from :
(e.g: String select = “for $a in input()/Author[AuthorName=’”+AuthorName+"’]":wink:

Doucment doc = …(from TResponse by parsing above query) …
TXMLObject xmlobject = TXMLObject.newInstance(doc);
xmlobject.setId(ino);

3) Delete:
accessor = connection.newXMLObjectAccessor(TAccessLocation.newInstance(collection), TDOMObjectModel.getInstance());

TResponse response = accessor.delete(xmlobject );

this should work.

tnx Bern!
your help is gold for me! :slight_smile:
i’ve found a different way…i use query instead of xquery:

  String q="/Author[AuthorName='"+AuthorName+"']";

        TQuery query = TQuery.newInstance(q);
		TXMLObjectAccessor xmlObjectAccessor = dataSource.getTXMLObjectAccessor();

		try {
       		TResponse response = xmlObjectAccessor.delete( query );
...................



your help had cleared to me some wrong ideas about Dom and xmlObject!
so…
do u think it’s better my way or your? and why?

tnx again!

Hello Tobia and Bern.

It is also possible to delete a document via an XQuery update expression, but the key/tricky thing is to specify the document - not the root element.
(Deleting the root element would leave an empty document hanging around, so you need to delete the document node.)

Tobia, to modify the update delete expression in your first posting so that it deletes documents, just add “/…” to the end of it.
At the moment it attempts to delete the root element, but the expression should select the parent of the root - the document…

Please also see this posting for more information.

I hope that helps,
Trevor.

Tnx Trevor!
i’m sorry…i’ve made an “overloaded” topic! :slight_smile:
So i try to add some ideas to this post! :slight_smile:
Well…your solution -of course- works! :slight_smile:
If i write:

 declare namespace tf='http://namespaces.softwareag.com/tamino/TaminoFunction' 
update for $i in input()/Author[AuthorName='xxxxx'] 
do delete $i/.. 



taking away the “where” expression i expected an error like “attempt to delete the root element” or “ino:id required” or something like this!
But it seems to work well! I think depends from the “$i/…” expression that returns the document itself (as you wrote in the other topic)!
My question is…is this query correct? or it works becouse i’ve few instance in my db?

Hi Tobia!

Your query (as presented in the previous posting) is correct. The example “delete query” that I posted in the other thread was based upon Michelle’s original query, that is the only reason that the ino:id attribute was used.

In your case, the root element “Author” is selected (where AuthorName=‘xxxxx’), and then the parent of “Author” is deleted. The parent of author is the document, so the effect is that the document is deleted.

You could also drop the filter condition / where clause - [AuthorName=‘xxxxx’] - to delete all the Author documents, and this will remain a valid / working expression.
If you get into a large number of documents, it is conceivable that attempting to delete all of them will take longer than the “maximum query duration” (or maximum transaction) setting on the database…

One last thing: you don’t need to declare the “tf” namespace in your query - that was only required for the tf:getInoId() function.

Cheers,
Trevor.

Tnx a lot for your explanation Trevor,
i’ll follow your advice!