commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Yuri Ushakov" <yuri.usha...@gmail.com>
Subject Re: JXPath API
Date Thu, 24 May 2007 07:11:17 GMT
Hello,

Here is the sample code:

src/test/Main.java :
package test;

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.xml.DocumentContainer;
import org.w3c.dom.Element;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        JXPathContext context = JXPathContext.newContext(
                new DocumentContainer(new File("test.xml").toURI().toURL())
        );
        List abcs = context.selectNodes("//abc");
        for (Object o : abcs) {
            Element el = (Element) o;
            System.out.printf("1: %s a=%s%n", el.getNodeName(),
el.getAttribute("a"));
        }
        JXPathContext subcontext = JXPathContext.newContext(abcs);
        List detailedAbcs = subcontext.selectNodes("*");
        for (Object o : detailedAbcs) {
            Element el = (Element) o;
            System.out.printf("2: %s a=%s%n", el.getNodeName(),
el.getAttribute("a"));
        }
    }
}

test.xml:
<root>
    <abc a="1"/>
    <abc a="2"/>
    <abc a="3"/>
</root>

Result:
1: abc a=1
1: abc a=2
1: abc a=3

The second query "*" resulted in empty sequence. If I query for "/"
instead of "*", I get the three abc elements, but for "/*" result is
empty again. So... how do you think I could get all abc elements where
a=1 from subcontext ?

Thanks,
Yuri.

2007/5/24, Matt Benson <gudnabrsam@yahoo.com>:
> At first glance it appears this, or something similar,
> should be sufficient for what you want to do.  Please
> assemble some sample code and explain where it is
> failing.
>
> Thanks,
> Matt
>
> --- Yuri Ushakov <yuri.ushakov@gmail.com> wrote:
>
> > Hello.
> >
> > I can't find a way, yet I think it should be
> > possible to do something like this:
> >
> > JXPathContext subcontext =
> >
> JXPathContext.newContext(context.selectNodes("//abc"));
> >
> > So then subcontext would point to a new sequence and
> > I could do
> > selects on abc elements in it. When I try to do as
> > above, I'm simply
> > "browsing" a List implementation object... But I
> > need to be able to
> > execute a query like this:
> >
> > subcontext.selectNodes("*[@a=1][not(@b)]");
> >
> > Where "*" is abc elements selected from first query.
> > Any tips?
> >
> > Thanks,
> > Yuri.
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > commons-user-help@jakarta.apache.org
> >
> >
>
>
>
>
> ____________________________________________________________________________________Pinpoint
customers who are looking for what you sell.
> http://searchmarketing.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message