You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by Thorsten Scherler <th...@juntadeandalucia.es> on 2008/09/22 14:54:07 UTC

[AXIOM] Xpath always returns null

Hi all,

having following code I always get null which I do not understand:
OMFactory factory = OMAbstractFactory.getOMFactory();
StAXOMBuilder builder = new StAXOMBuilder();
...
 String[] tokenizer = rootPath.split("/");
    // create all nodes that are in the root path
    LinkedHashSet<OMElement> path = new LinkedHashSet<OMElement>();
    int i = 0;
    for (String pathPart : tokenizer) {
      if (!pathPart.equals("")) {
        OMElement element = factory.createOMElement(pathPart, null);
        path.add(element);
      }
    }
    // now we have all nodes created but with no hierarchy 
    OMElement[] elements = new OMElement[0];
    elements= path.toArray(new OMElement[path.size()]);
    // create the hierarchy by adding the last node to the 
    // previous one till we reached the first one.
    for (int j = elements.length-1; j > 0; j--) {
      elements[j-1].addChild(elements[j]); 
    }
    // Get rid of the overhead and expose just one node
    OMElement root = elements[0];
...
     AXIOMXPath xpath = new AXIOMXPath("/html/body");
     OMElement injectionPoint = (OMElement)xpath.selectSingleNode(root);
...

When I debug the code I get for root: 
root	OMElementImpl  (id=61)	
	attributes	null	
	builder	null	
	done	true	
	factory	OMLinkedListImplFactory  (id=50)	
	firstChild	OMElementImpl  (id=80)	
	lastChild	OMElementImpl  (id=80)	
	lineNumber	0	
	localName	"html" (id=84)	
	namespaces	null	
	nextSibling	null	
	nodeType	0	
	noPrefixNamespaceCounter	0	
	ns	null	
	parent	null	
	previousSibling	null	

but then for xpath.selectSingleNode(root) it always returns null. 

I am doing obvious something wrong, but I am not sure what. Is there
some obvious problem?

salu2
-- 
Thorsten Scherler                                 thorsten.at.apache.org
Open Source Java                      consulting, training and solutions


Re: [AXIOM] Xpath always returns null

Posted by Thorsten Scherler <th...@juntadeandalucia.es>.
On Wed, 2008-09-24 at 09:15 +0530, Saliya Ekanayake wrote:
> Hi Thorsten (now I know how to greet in Spanish :) ),

hehe, glad to hear. ;) 

> This is interesting ;) 

Isn't it. 

> Theoretically XPath should work no matter how the OM
> tree is built. I will look into this and reply to you ASAP.

I opened an issue to keep track of this issue.
https://issues.apache.org/jira/browse/WSCOMMONS-388

Thanks for looking into this.

salu2

> Regards,
> Saliya
> 
> On Tue, Sep 23, 2008 at 4:14 PM, Thorsten Scherler <
> thorsten.scherler.ext@juntadeandalucia.es> wrote:
> 
> > On Tue, 2008-09-23 at 11:53 +0200, Thorsten Scherler wrote:
> > > I wonder - reading your other answers - whether AXIOMXPath is only
> > > working with OM's that a builder has generated (parsing a file) and
> > > not
> > > like I do an OMElement that I created from the factory (without
> > > parsing
> > > a file), hence builder = null.
> >
> > I did a small test where I created a file and used it instead the former
> > approach to create the OMElement via the factory and this revealed that
> > above suspicion is right. The objects are identically besides the
> > builder which seems to make all the difference.
> >
> > If you create a model directly from a factory and not a builder
> > AXIOMXPath will not return you any node.
> >
> > Is this a bug?
> >
> > salu2
> > --
> > Thorsten Scherler                                 thorsten.at.apache.org
> > Open Source Java                      consulting, training and solutions
> >
> >
> 
> 
-- 
Thorsten Scherler                                 thorsten.at.apache.org
Open Source Java                      consulting, training and solutions


Re: [AXIOM] Xpath always returns null

Posted by Saliya Ekanayake <es...@gmail.com>.
Hi Thorsten (now I know how to greet in Spanish :) ),

This is interesting ;) Theoretically XPath should work no matter how the OM
tree is built. I will look into this and reply to you ASAP.

Regards,
Saliya

On Tue, Sep 23, 2008 at 4:14 PM, Thorsten Scherler <
thorsten.scherler.ext@juntadeandalucia.es> wrote:

> On Tue, 2008-09-23 at 11:53 +0200, Thorsten Scherler wrote:
> > I wonder - reading your other answers - whether AXIOMXPath is only
> > working with OM's that a builder has generated (parsing a file) and
> > not
> > like I do an OMElement that I created from the factory (without
> > parsing
> > a file), hence builder = null.
>
> I did a small test where I created a file and used it instead the former
> approach to create the OMElement via the factory and this revealed that
> above suspicion is right. The objects are identically besides the
> builder which seems to make all the difference.
>
> If you create a model directly from a factory and not a builder
> AXIOMXPath will not return you any node.
>
> Is this a bug?
>
> salu2
> --
> Thorsten Scherler                                 thorsten.at.apache.org
> Open Source Java                      consulting, training and solutions
>
>


-- 
Saliya Ekanayake
http://www.esaliya.blogspot.com
http://www.esaliya.wordpress.com

Re: [AXIOM] Xpath always returns null

Posted by Thorsten Scherler <th...@juntadeandalucia.es>.
On Tue, 2008-09-23 at 11:53 +0200, Thorsten Scherler wrote:
> I wonder - reading your other answers - whether AXIOMXPath is only
> working with OM's that a builder has generated (parsing a file) and
> not
> like I do an OMElement that I created from the factory (without
> parsing
> a file), hence builder = null.

I did a small test where I created a file and used it instead the former
approach to create the OMElement via the factory and this revealed that
above suspicion is right. The objects are identically besides the
builder which seems to make all the difference. 

If you create a model directly from a factory and not a builder
AXIOMXPath will not return you any node.

Is this a bug?

salu2
-- 
Thorsten Scherler                                 thorsten.at.apache.org
Open Source Java                      consulting, training and solutions


Re: [AXIOM] Xpath always returns null

Posted by Thorsten Scherler <th...@juntadeandalucia.es>.
On Mon, 2008-09-22 at 21:44 +0530, Saliya Ekanayake wrote:
> Hi Salu

hehe, it is Thorsten. 

The "salu2" stands for "saludos" which is Spanish for greetings. ;)

> 
> Could you please post the rootPath string, which you use to create the names
> of the elements? 

That can variate from one format to the other. However in html out put
that mostly likely will be just one 

String rootPath="/html";

> Additionally according to your XPath the "body" element
> should be the first element of the root element. 

Yes.

> So please debug and see if
> that's the first element present in your OM tree.

Yeah, it is. 

firstChild	OMElementImpl  (id=113)	
	attributes	null	
	builder	null	
	done	true	
	factory	OMLinkedListImplFactory  (id=80)	
	firstChild	null	
	lastChild	null	
	lineNumber	0	
	localName	"body" (id=120)	
	namespaces	null	
	nextSibling	null	
	nodeType	0	
	noPrefixNamespaceCounter	0	
	ns	null	
	parent	OMElementImpl  (id=93)	
	previousSibling	null	



> 
> Just FYI, you can write much more clean code to accomplish your task. 

My ears are width open. :)

> If you
> post or explain the structure of the rootPath string I will be able to
> provide you with a much better code sample to suit your work.

The rootPath can be any string that is the common denominator for all
paths of a structurer [http://markmail.org/message/mwg5le6slrikjyak].

In html it is the root element "html" in fo it will be "fo:root". ...
but it could be basically everything like "html/body" if no injection to
"html/head" are made. The rootPath is the longest common path for all
injections.

I wonder - reading your other answers - whether AXIOMXPath is only
working with OM's that a builder has generated (parsing a file) and not
like I do an OMElement that I created from the factory (without parsing
a file), hence builder = null.

Thanks for your feedback Saliya.

salu2

> 
> Thanks,
> Saliya
> 
> On Mon, Sep 22, 2008 at 6:24 PM, Thorsten Scherler <
> thorsten.scherler.ext@juntadeandalucia.es> wrote:
> 
> > Hi all,
> >
> > having following code I always get null which I do not understand:
> > OMFactory factory = OMAbstractFactory.getOMFactory();
> > StAXOMBuilder builder = new StAXOMBuilder();
> > ...
> >  String[] tokenizer = rootPath.split("/");
> >    // create all nodes that are in the root path
> >    LinkedHashSet<OMElement> path = new LinkedHashSet<OMElement>();
> >    int i = 0;
> >    for (String pathPart : tokenizer) {
> >      if (!pathPart.equals("")) {
> >        OMElement element = factory.createOMElement(pathPart, null);
> >        path.add(element);
> >      }
> >    }
> >    // now we have all nodes created but with no hierarchy
> >    OMElement[] elements = new OMElement[0];
> >    elements= path.toArray(new OMElement[path.size()]);
> >    // create the hierarchy by adding the last node to the
> >    // previous one till we reached the first one.
> >    for (int j = elements.length-1; j > 0; j--) {
> >      elements[j-1].addChild(elements[j]);
> >    }
> >    // Get rid of the overhead and expose just one node
> >    OMElement root = elements[0];
> > ...
> >     AXIOMXPath xpath = new AXIOMXPath("/html/body");
> >     OMElement injectionPoint = (OMElement)xpath.selectSingleNode(root);
> > ...
> >
> > When I debug the code I get for root:
> > root    OMElementImpl  (id=61)
> >        attributes      null
> >        builder null
> >        done    true
> >        factory OMLinkedListImplFactory  (id=50)
> >        firstChild      OMElementImpl  (id=80)
> >        lastChild       OMElementImpl  (id=80)
> >        lineNumber      0
> >        localName       "html" (id=84)
> >        namespaces      null
> >        nextSibling     null
> >        nodeType        0
> >        noPrefixNamespaceCounter        0
> >        ns      null
> >        parent  null
> >        previousSibling null
> >
> > but then for xpath.selectSingleNode(root) it always returns null.
> >
> > I am doing obvious something wrong, but I am not sure what. Is there
> > some obvious problem?
> >
> > salu2
> > --
> > Thorsten Scherler                                 thorsten.at.apache.org
> > Open Source Java                      consulting, training and solutions
> >
> >
> 
> 
-- 
Thorsten Scherler                                 thorsten.at.apache.org
Open Source Java                      consulting, training and solutions


Re: [AXIOM] Xpath always returns null

Posted by Saliya Ekanayake <es...@gmail.com>.
Hi Salu

Could you please post the rootPath string, which you use to create the names
of the elements? Additionally according to your XPath the "body" element
should be the first element of the root element. So please debug and see if
that's the first element present in your OM tree.

Just FYI, you can write much more clean code to accomplish your task. If you
post or explain the structure of the rootPath string I will be able to
provide you with a much better code sample to suit your work.

Thanks,
Saliya

On Mon, Sep 22, 2008 at 6:24 PM, Thorsten Scherler <
thorsten.scherler.ext@juntadeandalucia.es> wrote:

> Hi all,
>
> having following code I always get null which I do not understand:
> OMFactory factory = OMAbstractFactory.getOMFactory();
> StAXOMBuilder builder = new StAXOMBuilder();
> ...
>  String[] tokenizer = rootPath.split("/");
>    // create all nodes that are in the root path
>    LinkedHashSet<OMElement> path = new LinkedHashSet<OMElement>();
>    int i = 0;
>    for (String pathPart : tokenizer) {
>      if (!pathPart.equals("")) {
>        OMElement element = factory.createOMElement(pathPart, null);
>        path.add(element);
>      }
>    }
>    // now we have all nodes created but with no hierarchy
>    OMElement[] elements = new OMElement[0];
>    elements= path.toArray(new OMElement[path.size()]);
>    // create the hierarchy by adding the last node to the
>    // previous one till we reached the first one.
>    for (int j = elements.length-1; j > 0; j--) {
>      elements[j-1].addChild(elements[j]);
>    }
>    // Get rid of the overhead and expose just one node
>    OMElement root = elements[0];
> ...
>     AXIOMXPath xpath = new AXIOMXPath("/html/body");
>     OMElement injectionPoint = (OMElement)xpath.selectSingleNode(root);
> ...
>
> When I debug the code I get for root:
> root    OMElementImpl  (id=61)
>        attributes      null
>        builder null
>        done    true
>        factory OMLinkedListImplFactory  (id=50)
>        firstChild      OMElementImpl  (id=80)
>        lastChild       OMElementImpl  (id=80)
>        lineNumber      0
>        localName       "html" (id=84)
>        namespaces      null
>        nextSibling     null
>        nodeType        0
>        noPrefixNamespaceCounter        0
>        ns      null
>        parent  null
>        previousSibling null
>
> but then for xpath.selectSingleNode(root) it always returns null.
>
> I am doing obvious something wrong, but I am not sure what. Is there
> some obvious problem?
>
> salu2
> --
> Thorsten Scherler                                 thorsten.at.apache.org
> Open Source Java                      consulting, training and solutions
>
>


-- 
Saliya Ekanayake
http://www.esaliya.blogspot.com
http://www.esaliya.wordpress.com