You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Vanessa Williams <v....@acm.org> on 2002/06/10 18:33:53 UTC

Removing documents found by query

I want to query a collection and then remove the documents found by the
query. I have something that works, but I had to go about it in a strange
way. I just want to confirm that there isn't a better way to do it. Here's
what I would have expected to work:

    // spacedb is my Collecion and getQueryService is just a
    // convenience function
    ResourceSet resultSet = getQueryService(spacedb).query(xpath);
    ResourceIterator results = resultSet.getIterator();
    while (results.hasMoreResources()) {
        Resource res = results.nextResource();
        if (res instanceof XMLResource) {

            // do some stuff with it if you want,
            // then remove it...

            spacedb.removeResource(res);

            // this throws an exception!

        }


Instead, I have to do something like this:


    ResourceSet resultSet = getQueryService(spacedb).query(xpath);
    ResourceIterator results = resultSet.getIterator();
    while (results.hasMoreResources()) {
        Resource res = results.nextResource();
        if (res instanceof XMLResource) {

            // do some stuff with it if you want,

            // find the key (getId() doesn't work, apparently
            // because Resources from ResourceSets are "anonymous"?)
            try {
                Node xmldoc = res.getContentAsDOM().getFirstChild();
                NamedNodeMap attrs = xmldoc.getAttributes();
                Node keyNode = attrs.getNamedItemNS(
                    "http://xml.apache.org/xindice/Query",
                    "key");
                key = keyNode.getNodeValue();
            }
            catch (Exception e) {
                //deal with any DOM exceptions
            }

            // now that we have the key, remove resource from collection
            try {
                Resource doc = spacedb.getResource(key);
                if (doc != null) {
                    spacedb.removeResource(doc);
                }
            }
            catch (Exception e) {
                // etc.
            }
        }
    }


Is this the easiest way? Or am I missing something fundamental. It seems to
me it shouldn't be this hard, but it was the only way I could get it to
work. Any enlightenment appreciated.

Cheers,

Vanessa


Re: Removing documents found by query

Posted by "Mark J. Stang" <ma...@earthlink.net>.
Vanessa,
I don't know enough about the internals at this point to do it
either.   Also, I don't have the time right now.   So far it hasn't
been a big enough problem to try and sort it out.   I just
provide my users with a list so they can choose what they want
to delete.   I haven't run into a real need for the delete via
XPath...

Mark

Vanessa Williams wrote:

> Mark J. Stang wrote:
>
> > Vanessa,
> > I don't know of an easier way.   I do it the same way.   Maybe we
> > should write one to do it the other way.
> >
> > Mark
>
> Thanks, Mark. At least I'm not crazy or totally stupid.
>
> The first step would be to understand why it works the way it does. For
> instance, why are the Resources returned in a ResourceSet not usable in the
> expected ways? There is probably a good reason for this, but it's just not
> obvious.
>
> Unfortunately, I just don't know enough about the Xindice code to do
> anything to it at the moment. I also wanted to alter it to remove the extra
> attributes it adds to returned documents, but I haven't got the kind of time
> it would require to go through the code and try to figure out how it works.
> I'd no doubt just break something.
>
> If you have some clues to what's really going on under the hood, I'd love to
> hear them. Maybe there is something we can do...or maybe not.
>
> Thanks again,
>
> Vanessa
>
> > --
> > Mark J Stang
> > Architect
> > Cybershop Systems
> >
> >

--
Mark J Stang
Architect
Cybershop Systems


Re: Removing documents found by query

Posted by Vanessa Williams <v....@acm.org>.
Mark J. Stang wrote:

> Vanessa,
> I don't know of an easier way.   I do it the same way.   Maybe we
> should write one to do it the other way.
> 
> Mark

Thanks, Mark. At least I'm not crazy or totally stupid.

The first step would be to understand why it works the way it does. For
instance, why are the Resources returned in a ResourceSet not usable in the
expected ways? There is probably a good reason for this, but it's just not
obvious.

Unfortunately, I just don't know enough about the Xindice code to do
anything to it at the moment. I also wanted to alter it to remove the extra
attributes it adds to returned documents, but I haven't got the kind of time
it would require to go through the code and try to figure out how it works.
I'd no doubt just break something.

If you have some clues to what's really going on under the hood, I'd love to
hear them. Maybe there is something we can do...or maybe not.

Thanks again,

Vanessa

> --
> Mark J Stang
> Architect
> Cybershop Systems
> 
> 


Re: Removing documents found by query

Posted by "Mark J. Stang" <ma...@earthlink.net>.
Vanessa,
I don't know of an easier way.   I do it the same way.   Maybe we
should write one to do it the other way.

Mark

Vanessa Williams wrote:

> I want to query a collection and then remove the documents found by the
> query. I have something that works, but I had to go about it in a strange
> way. I just want to confirm that there isn't a better way to do it. Here's
> what I would have expected to work:
>
>     // spacedb is my Collecion and getQueryService is just a
>     // convenience function
>     ResourceSet resultSet = getQueryService(spacedb).query(xpath);
>     ResourceIterator results = resultSet.getIterator();
>     while (results.hasMoreResources()) {
>         Resource res = results.nextResource();
>         if (res instanceof XMLResource) {
>
>             // do some stuff with it if you want,
>             // then remove it...
>
>             spacedb.removeResource(res);
>
>             // this throws an exception!
>
>         }
>
> Instead, I have to do something like this:
>
>     ResourceSet resultSet = getQueryService(spacedb).query(xpath);
>     ResourceIterator results = resultSet.getIterator();
>     while (results.hasMoreResources()) {
>         Resource res = results.nextResource();
>         if (res instanceof XMLResource) {
>
>             // do some stuff with it if you want,
>
>             // find the key (getId() doesn't work, apparently
>             // because Resources from ResourceSets are "anonymous"?)
>             try {
>                 Node xmldoc = res.getContentAsDOM().getFirstChild();
>                 NamedNodeMap attrs = xmldoc.getAttributes();
>                 Node keyNode = attrs.getNamedItemNS(
>                     "http://xml.apache.org/xindice/Query",
>                     "key");
>                 key = keyNode.getNodeValue();
>             }
>             catch (Exception e) {
>                 //deal with any DOM exceptions
>             }
>
>             // now that we have the key, remove resource from collection
>             try {
>                 Resource doc = spacedb.getResource(key);
>                 if (doc != null) {
>                     spacedb.removeResource(doc);
>                 }
>             }
>             catch (Exception e) {
>                 // etc.
>             }
>         }
>     }
>
> Is this the easiest way? Or am I missing something fundamental. It seems to
> me it shouldn't be this hard, but it was the only way I could get it to
> work. Any enlightenment appreciated.
>
> Cheers,
>
> Vanessa

--
Mark J Stang
Architect
Cybershop Systems