PDA

View Full Version : DomQuery expression...



jeffiel
31 Mar 2007, 1:07 AM
DomQuery can be frustrating since it's not quite xpath... here's my problem:

In the grid, each row is defined by an element, say "Element". Then another column is defined by "Name" directly under Element. How can I retreive Name elements directly under Element, from within the context of the Element (as the store will interpret it).

With an expath function query(q, context), it would be simply query("Name", contextNode)... however, DomQuery gets all Name elements under the context, even if they're buried deep in the tree.

Things I've tried are:

* "> Name" ... actually does the opposite of what I want... it selects all Name elments that are grandchildren
* "/Name" ... selects all nodes
* "../Name"... throws

I've played around with implementing "./" in DomQuery, so that the query would be "./Name", but the code's not documented yet, so it's tricky.

Any thoughts? Am I missing something obvious?

jheid
14 May 2007, 2:52 PM
I just ran into the same problem.

Is there any solution or some workaround code for it?

jheid
14 May 2007, 3:10 PM
Okay, I just found a way:



Ext.DomQuery.pseudos['parent'] = function (c, name) {
var result = [];
for (var i = 0; i < c.length; i++)
if (c[i].parentNode.nodeName == name)
result[result.length] = c[i];
return result;
}


for



DomQuery.select ('Name:parent(name-of-the-parent-node)')


Is there a better way?
[/PHP]

Animal
15 May 2007, 5:22 AM
You can root the query string at a certain ID.



Ext.DomQuery.selectNode("#fromElement #name");


Gets the element id "fromElement", and returns a descendant node of that with an id of "name"

jheid
15 May 2007, 7:45 AM
You can root the query string at a certain ID.



Ext.DomQuery.selectNode("#fromElement #name");


Gets the element id "fromElement", and returns a descendant node of that with an id of "name"

There isn't such an ID as I used it with the XmlReader. The XML document looks like



<a>
<b>
<c>
<Name>X</Name>
</c>
<Name>Y</Name>
</b>
</a>


If you use DomQuery.select ('Name', c) you'll get X instead of Y. As the XML is dynamically generated I can't use :last or something like that. If I'm using XPath I would use ./Name or child::Name for that.

Animal
16 May 2007, 3:35 AM
Couldn't you use DomQuery.select("b > Name", root) in the case above?

jeffiel
17 May 2007, 12:21 AM
It's been a while since I wrote this, but here's my solution:



Ext.DomQuery.proot = function(n, root){

if(n.nodeType || n.documentElement)
n=[n];

var r = [], c;

for(var i=0, len = n.length; i<len; i++) {

if(n[i].parentNode == root)
r[r.length] = n[i];
}
return r;
}

Ext.DomQuery.matchers.unshift({re: /^\.\/([\w-]+)/, select: 'n = byTag(Ext.DomQuery.proot(n, root), "{1}");'});


This adds the ./ selector. I use this extensively within my row selectors, since I'm not always sure if the node-name may be in use elsewhere within the xml objects.