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

"reference to undefined XML name" in E4X queries

127 views
Skip to first unread message

Massimiliano Mirra

unread,
Jul 3, 2007, 2:25:41 PM7/3/07
to
I've run into a major obstacle while trying to query mildly
heterogeneous XML datasets with E4X. The following works:

> var dataset = <dataset>
<entry type="normal"><foo/></entry>
<entry><bar/></entry>
</dataset>;
> dataset.entry[0].@type == 'normal'
true
> dataset.entry[1].@type == 'normal'
false

So, I could loop over the dataset using "for each", and
collect children having @type == "normal". But that seems silly,
since E4X has query capabilities, so I try:

> dataset.entry(@type == 'normal')
typein:19: ReferenceError: reference to undefined XML name @type

Hmmm.

Turns out that the latter behaviour is expected, more or less [1],
although I can't help but find it inconsistent with the former.

There is a workaround:

> dataset.entry(function::attribute('type') == 'normal')

But it quickly becomes unwieldy. For example, if you introduce
namespaces, instead of having:

> const myns = 'http://my.namespace.org'
> dataset.entry(@type == 'normal' && @myns::otherattr ==
'otherval')

You have:

> const myns = 'http://my.namespace.org'
> dataset.entry(function::attribute('type') == 'normal' &&
function::attribute(new QName(myns, 'otherattr))
== 'otherval')

At which point, switching to DOM and XPath suddendly starts looking
attractive...

The only relevant entry in bugzilla I found is
https://bugzilla.mozilla.org/show_bug.cgi?id=246441#c14. I'd be
grateful for opinions before possibly filing this by itself.


Massimiliano

[1] http://groups.google.com/group/netscape.public.mozilla.jseng/msg/1e756e711720e1c5

0 new messages