Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

node in context, root, parent etc in filter and method ancestors() list

4 views
Skip to first unread message

bijuma...@yahoo.com

unread,
Dec 31, 2005, 1:45:01 PM12/31/05
to
I am looking for few (many) enhancement in E4X

1. context node, root, parent etc in filter :-

It would have been nice if we had simple accessors to the current node,
root
node (of original XML), parent node etc in the filter clause.

so that I can do like
c.dept.emp.(@id == @..@boss);
c.dept.emp.(@id == @/.@companypresident);


2. root() node, ancestors() list.
We have children(), child(), descendants() for a node
But we dont have a function for root() of the original XML object
also no method for to get all in the chain above a node like
a ancestors() list method

I have also filed an enhancement request at
https://bugzilla.mozilla.org/show_bug.cgi?id=322005

Thanks in advance
BijuGC

Martin Honnen

unread,
Dec 31, 2005, 2:00:57 PM12/31/05
to

bijuma...@yahoo.com wrote:

> I am looking for few (many) enhancement in E4X
>
> 1. context node, root, parent etc in filter :-
>
> It would have been nice if we had simple accessors to the current node,
> root
> node (of original XML), parent node etc in the filter clause.
>
> so that I can do like
> c.dept.emp.(@id == @..@boss);
> c.dept.emp.(@id == @/.@companypresident);

What is the root node in your understanding, what XPath defines as the
root node and the DOM calls the document node, or the root element node
(also called document element)?


--

Martin Honnen
http://JavaScript.FAQTs.com/

Martin Honnen

unread,
Dec 31, 2005, 2:43:38 PM12/31/05
to

bijuma...@yahoo.com wrote:

> I am looking for few (many) enhancement in E4X
>
> 1. context node, root, parent etc in filter :-


Note that Spidermonkey has an extension, a function namespace that
allows you to access the methods in the filter expression so you can
simply do e.g.
c.dept.emp.(@id == function::parent().@boss)
to access the parent XML object and its attributes.
See
<http://groups.google.com/group/netscape.public.mozilla.jseng/browse_frm/thread/b99b718927d06901/0803567b41f3e191?lnk=st&q=function+namespace+group%3Anetscape.public.mozilla.jseng+author%3Aeich&rnum=1&hl=en#0803567b41f3e191>
for Brendan's explanation.
So the "context node" is there in the filter expression, only as far as
E4X is currently standardized the methods are not found with the normal
property lookup and that function namespace fixes that.

biju

unread,
Dec 31, 2005, 10:38:32 PM12/31/05
to
Tnx...
"function::" solved my original issue.. ie.
a = <a><b id="1">hi</b><b>there</b></a>;
a.b.(id=='1');
was giving "id is not defined" error

a.b.(function::attribute('id')=='1')
worked..

function::attribute()
function::child()
function::parent()
are temp soln. for my prob.

but still feel a hidden attributes like
@. @.. @/ will be better
or @# @## @/ as .. has another meaning in E4X
and # is never used as attribute name

> What is the root node in your understanding,

either of them will be fine,
slight preference for document node

Martin Honnen

unread,
Jan 1, 2006, 7:49:26 AM1/1/06
to

biju wrote:


> "function::" solved my original issue.. ie.
> a = <a><b id="1">hi</b><b>there</b></a>;
> a.b.(id=='1');
> was giving "id is not defined" error

Well to access attributes as properties you need to use @attributeName
e.g. in Rhino an example is

js> var a = <a><b id="1">hi</b><b>there</b></a>;
js> a.b


<b id="1">hi</b>
<b>there</b>

js> a.b.(@id == 1)
hi

But it looks as Spidermonkey gives an error message on that example with
the attribute property access yielding to "reference to undefined XML
name @id", I will have to look why that happens and whether there is a
bug filed on that.

>>What is the root node in your understanding,
>
> either of them will be fine,
> slight preference for document node

But you understand that E4X is a standard and that it defines the
following node kind of different XML objects:
element, attribute, comment, processing-instruction, text
so there is nothing like the XPath root node or the DOM document node in
that data model E4X defines.

Martin Honnen

unread,
Jan 1, 2006, 9:20:50 AM1/1/06
to

Martin Honnen wrote:


> Well to access attributes as properties you need to use @attributeName
> e.g. in Rhino an example is
>
> js> var a = <a><b id="1">hi</b><b>there</b></a>;
> js> a.b
> <b id="1">hi</b>
> <b>there</b>
> js> a.b.(@id == 1)
> hi
>
> But it looks as Spidermonkey gives an error message on that example with
> the attribute property access yielding to "reference to undefined XML
> name @id", I will have to look why that happens and whether there is a
> bug filed on that.

Looking deeper into that I think that Rhino has a bug, it should give
the error too that Spidermonkey gives. The problem is that the second b
element in the XMLList object that a.b yields has no id attribute and
as the filtering predicate @id == 1 is applied with each object in the
list being added to the scope chain the reference error should occur.

What I am wondering if the usual typeof checks employed in client-side
script to safeguard against different environments should work so I have
tried
a.b.(typeof @id != 'undefined' && @id == '1')
but Spidermonkey still gives the "reference to undefined XML name @id"
error.

Brendan, I hope you are reading, how could one write a filter predicate
expression filtering out those XML objects with a certain attribute
value but not throwing any error for those XML objects that do not have
the attribute at all?

One way with Spidermonkey could be
a.b.(function::attribute('id').length() != 0 && @id == '1')
but that needs function:: which is not standardized.

biju

unread,
Jan 1, 2006, 12:12:21 PM1/1/06
to
>> you need to use @attributeName
oops... I mean a.b.(@id=='1');

>> One way with Spidermonkey could be
>> a.b.(function::attribute('id').length() != 0 && @id == '1')

following also works...
(still need function:: though)

a = <a><b id="1">hi</b><b>there</b></a>;

a.b.(function::attribute('id')== '1') ;

or for....

a = <a>
<b>
<text>hi</text>
<id><val>1</val><type>SSN</type></id>
</b>
<b>
<text>there</text>
</b>
</a>;
a.b.(function::child('id').val== '1').text

biju

unread,
Jan 1, 2006, 12:25:21 PM1/1/06
to
// other work arrounds

a = <a>
<b>
<text>hi</text>

<id><val>1</val><type><text>SSN</text></type></id>


</b>
<b>
<text>there</text>
</b>
</a>;

//method 1
id=null;
a.b.(id && id.val== '1').text

//method 2
id=null;
a.b.(id && id.type.text== 'SSN').text

//method 3
id={};
a.b.(id.val== '1').text

//method 4
id={type:{}};
a.b.(id && id.type.text== 'SSN').text

// and so on...

Martin Honnen

unread,
Jan 1, 2006, 12:42:28 PM1/1/06
to

biju wrote:


>>>One way with Spidermonkey could be
>>> a.b.(function::attribute('id').length() != 0 && @id == '1')


> following also works...

> a.b.(function::attribute('id')== '1') ;

Right, I was trying to protect @id == '1' from giving an error but
overlooked then that once function::attribute('id') has been used that
then the @id is no longer needed at all.

biju

unread,
Jan 1, 2006, 6:00:57 PM1/1/06
to
with these methods we can also create new attributes like @name on the
fly

dept = <dept>
<emp fn="Biju" ln="GC" />
<emp fn="Martin" ln="Honnen" />
<emp fn="Brendan" ln="Eich" />
</dept>

dept.emp.(@fn.parent().@name = @fn + ' ' + @ln);
dept;

0 new messages