Discussion:
DOM getElementsByAttribute
dilvie
2003-03-03 12:45:45 UTC
Permalink
I frequently encounter circumstances where it would be convenient to
have a getElementsByAttribute method.

getElementsByAttribute(attribute, value)
would return a (possibly empty) list of elements with the attribute
matching value.

I get several questions per day from DHTML developers wishing they could
do this. Is there a good alternative in this DOM draft?
--
dilvie's websites:
http://www.minutejs.com/ - generate custom javascript
http://www.minutejs.com/forum/ - DHTML discussions
http://www.dilvie.com - listen to my original music
Joseph Kesselman
2003-03-24 22:42:07 UTC
Permalink
getElementsByAttribute[Value] has been considered in the past, but never
survived into the spec since there appeared to be adequate (and less
argumentative) alternatives:

In DOM Level 1: Write a tree-walker that searches for those attributes.
There are many existing tree-walkers you can borrow code from.

In DOM Level 2, if Traversal is supported: Implement a NodeFilter which
tests for the presence/value of the attribute and plug that into a
TreeWalker or NodeIterator. Or prescan the document, build a lookup table,
and implement your own retrieval method using that. Or, if you can't
depend on Traversal but can validate the document against a DTD, use
Document's getElementByID method.

In DOM Level 3, if the XPath module is supported: Write a suitable XPath
and evaluate that.


______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"may'ron DaroQbe'chugh vaj bIrIQbej" ("Put down the squeezebox and nobody
gets hurt.")
Joris Huizer
2003-04-08 20:23:12 UTC
Permalink
Hello DOM group,

I'm trying to write a small and understandable cpp
program to parse a XML doc and give a DOM Document
class back. The prog just uses Node and stuff as real
classes (to make things as simple as they could be)
Currently I'm just messing a bit with DOM 1

I have this question: should read-only stuff be done
using #define , or can I just say length becomes
getLength() or length() (on nodelists, for examples)

Thanks for the input - sorry if this is really
off-topic

Joris

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com
Joseph Kesselman
2003-04-08 20:46:45 UTC
Permalink
Depends on the value. If it's a manefest constant, #define or a static
final value is probably the right answer; if it's a value that varies from
node to node, you want an exposed field or a getter method or something of
that sort. (I'd suggest the getter, personally.)

Note that some C++ implementations already exist; even if you're writing
your own, you might want to look at their APIs as examples. See
http://www.w3.org/DOM/Bindings for some of them. Note that in C++ you have
to make a difficult decision about memory management, which the reference
bindings were able to dodge since they were written in garbage-collected
languages.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"may'ron DaroQbe'chugh vaj bIrIQbej" ("Put down the squeezebox and nobody
gets hurt.")
Joris Huizer
2003-04-09 07:43:15 UTC
Permalink
Hello,

Thank you Joseph Kesselman for you're reply.
Is my understanding correct that the suggested
structure from
http://xml.apache.org/xerces-c/ApacheDOMC++BindingL2.html
can be freely used ?
It's not like I'm going to finish something soon but,
you never know - some parts of the coding on that site
is somewhat like I wrote too, but other things are
better than what I thought of ofcourse. I see they
indeed use getLength() where needed - that answers my
initial question allright [currently I have a
getLength() method + a #define length getLength() but
that limits the use of the variable length;]
Post by Joseph Kesselman
Depends on the value. If it's a manefest constant,
#define or a static
final value is probably the right answer; if it's a
value that varies from
node to node, you want an exposed field or a getter
method or something of
that sort. (I'd suggest the getter, personally.)
Note that some C++ implementations already exist;
even if you're writing
your own, you might want to look at their APIs as
examples. See
http://www.w3.org/DOM/Bindings for some of them.
Note that in C++ you have
to make a difficult decision about memory
management, which the reference
bindings were able to dodge since they were written
in garbage-collected
languages.
______________________________________
XML, XSL and more.
"may'ron DaroQbe'chugh vaj bIrIQbej" ("Put down the
squeezebox and nobody
gets hurt.")
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com
Joseph Kesselman
2003-04-09 13:30:18 UTC
Permalink
Is my understanding correct that the suggested structure from
http://xml.apache.org/xerces-c/ApacheDOMC++BindingL2.html
can be freely used ?
See Apache's license for details. I Am Not A Lawyer so my advice isn't
worth anything, but my reading of that text is that it basically says
you're free to use any Apache code however you want to use it as long as
you give Apache appropriate credit.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"may'ron DaroQbe'chugh vaj bIrIQbej" ("Put down the squeezebox and nobody
gets hurt.")

Loading...