select dom text nodes

A

avsyam

Hi,

I want to select all the text nodes of an xml document using msxml
dom. I tried using the selectNodes method with "//#text" argument. But
it fails. Can anybody help please?

Thank you.
 
P

p.lepin

I want to select all the text nodes of an xml document
using msxml dom. I tried using the selectNodes method
with "//#text" argument.

I don't believe there's a selectNodes() method in the W3C
DOM specs. Assuming it accepts an XPath expression as an
argument, what you need is '//text()'. #text is a nodeName,
and is fairly meaningless in the context. Consider reading
some sort of XPath tutorial, because invoking magic without
understanding how it works is a recipe for disaster, as I
can attest from painful personal experiences.
 
A

avsyam

Hey.. Thank you very much. That worked!!

I m using MSXML and there is a selectNodes method.

Yes, I agree that reading throughly before attempting a task is always
the best way to work on something. Once in a while we all come across
tasks where we dont have enough time to be thoroughly trained before
attempting something.

To be fair to me :), I did go thru the material in MSDN on XPath.
Most of it is given in the context of XSLT. That partly obstructed my
learning. Inspite of that, I didnt find any reference to selecting all
text nodes.

After you pointed out, I did check the w3schools tutorial where I
found a similar point documented, thanks to you again. Btw, do you
seriously think XPath is complex enough that we should master it
before using it?
 
J

Joe Kesselman

In XPath, the pattern that matches text nodes is "text()".

Note that XPath's idea of the document tree is not exactly the same as
the DOM's model, so using XPath over a DOM involves a bit of translation
and magic. The DOM Level 3 recommendation defines an API for using XPath
with DOM that includes the necessary additional functions/behaviors to
make that work, including hiding entity-reference boundaries and
treating adjacent TEXT nodes as a single text() node.
 
P

p.lepin

Once in a while we all come across tasks where we dont
have enough time to be thoroughly trained before
attempting something.

When something like that happens I go to the management and
tell them something along the lines of 'You do realize this
is just asking for trouble? You've been warned.' This often
works. YMMV.
To be fair to me :), I did go thru the material in MSDN
on XPath. Most of it is given in the context of XSLT.
That partly obstructed my learning. Inspite of that, I
didnt find any reference to selecting all text nodes.

If that is the case, the materials in question are of
rather poor quality; however, I find that a bit hard to
believe. Whatever you say about Microsoft, their references
are usually up to snuff IME. It's more likely you weren't
looking at them the right way.
After you pointed out, I did check the w3schools tutorial
where I found a similar point documented, thanks to you
again.

W3schools has a somewhat mixed reputation. Some people
swear by them, but they've been caught red-handed at
posting almost blatantly ignorant advice in some of their
tutorials.

Joseph Kesselman usually refers people to IBM's collection
of materials on XML, which is what I'm gonna do:

http://www.ibm.com/xml/

It's even easier for me, because unlike Joseph I'm not
affiliated with IBM in any way (apart from having used an
IBM G96 19" display several years ago, which was an almost
orgastic experience).

Note that the place is a treasure trove of useful
information, which means it might seem a bit overwhelming.
Btw, do you seriously think XPath is complex enough that
we should master it before using it?

I think you should at least attempt getting hang of
*anything* you intend to use before trying to actually use
it. This applies to anything up to and including
chopsticks. Anyway, yes, I think XPath1 is complex enough
if we start messing around with predicates, contexts and
node identities, and I don't even mention XPath2. Note that
while you seem to think XPath is easy you failed to solve a
simple problem without asking for advice.
 
J

Joseph Kesselman

seriously think XPath is complex enough that we should master it
before using it?

Master? Maybe not.

Spend time learning the basics? Yes, as with any tool. You can get away
with just-in-time learning... but you do have to allow time for that
learning. If management is in too much of a panic to let you do a good
job, it may be time for them to hire a consultant to help you over the
hump. Asking Usenet for advice is fine, but folks get irritated when it
looks like you haven't done your homework first... and this really is a
question that any semi-decent XPath tutorial should have answered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top