473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SelectNodes not working like I thought it would

I've been trying to teach myself XML. I have an XML file with contents like
this (from a play):
<SPEECH>
<SPEAKER>SLY</SPEAKER>
<LINE>I'll pheeze you, in faith.</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>Hostess</SPEAKER>
<LINE>A pair of stocks, you rogue!</LINE>
</SPEECH>

I've written the following code:
Dim nodeSpeeches As XmlNodeList
Dim node As XmlNode
nodeSpeeches = xmlDoc.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(0).Value
aIndex = GetSpeakerIndex(TempStr)
... Load data based on index
Next
It doesn't work. The value of SPEAKER is always the first occurrence in the
entire XML document instead of the current SPEECH node. The LINE count is
always the number of LINE elements in the entire document instead of the
current SPEECH.
It was my understanding that
For Each node In nodeSpeeches
would render node into an XML fragment holding only one <SPEECH>...</SPEECH>
for each iteration.
Why doesn't this work?

Oct 24 '08 #1
2 2636
Bev Kaufman wrote:
I've been trying to teach myself XML. I have an XML file with contents like
this (from a play):
<SPEECH>
<SPEAKER>SLY</SPEAKER>
<LINE>I'll pheeze you, in faith.</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>Hostess</SPEAKER>
<LINE>A pair of stocks, you rogue!</LINE>
</SPEECH>

I've written the following code:
Dim nodeSpeeches As XmlNodeList
Dim node As XmlNode
nodeSpeeches = xmlDoc.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(0).Value
You want a relative XPath expression selecting the SPEAKER child element
e.g.
TempStr = node.SelectSingleNode("SPEAKER").InnerText
If you use //SPEAKER then you always select down from the root node (the
document node).
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 24 '08 #2
Thank you. It works now. On to the next bug.

"Martin Honnen" wrote:
Bev Kaufman wrote:
I've been trying to teach myself XML. I have an XML file with contents like
this (from a play):
<SPEECH>
<SPEAKER>SLY</SPEAKER>
<LINE>I'll pheeze you, in faith.</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>Hostess</SPEAKER>
<LINE>A pair of stocks, you rogue!</LINE>
</SPEECH>

I've written the following code:
Dim nodeSpeeches As XmlNodeList
Dim node As XmlNode
nodeSpeeches = xmlDoc.SelectNodes("//SPEECH")
For Each node In nodeSpeeches
TempStr = node.SelectSingleNode("//SPEAKER").ChildNodes(0).Value

You want a relative XPath expression selecting the SPEAKER child element
e.g.
TempStr = node.SelectSingleNode("SPEAKER").InnerText
If you use //SPEAKER then you always select down from the root node (the
document node).
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 24 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Mark Bram | last post by:
Hi All! In the code below, I am reading in an xhtml document and attempting to use selectNodes to find a <p id="rmb"> node.. But the result is: 2 - */* 0 - */p Can anyone suggest what I...
1
by: MacDk | last post by:
Hi, Given this: http://rss.asdf.dk/theothermusic.rss how do you use selectnodes/Xpath to return all title-items (//title is not working). I think it a problem with namespace but i can't...
4
by: Andrew Parsons | last post by:
Hey all, I have the following code to get a set of nodes containing a certain search criteria: theSearchTermsXML.selectNodes("/Table/Row/Cell") This works fine to a point - it finds all...
8
by: e-mid | last post by:
i want to get childs of specific xml node. normally i use xmlNodeList fooList = myNode.SelectNodes("foo"); but in compactframework , there is no selectNodes() method or selectSingleNode()...
2
by: Linda Boumarafi | last post by:
Hi, This is a newbie question to XMLNS. I just finished the article on "XML Namespaces and How They Affect XPath and XSLT" by Dare Obasanjo and I'm pretty sure I understand the problem. I just...
2
by: Joe | last post by:
Hello All: Does anyone know the differnce between the GetElementsByTagName method and the SelectNodes method? I know that they take different arguments. They also both return a NodeList. I'm...
1
by: Steve | last post by:
I thought I knew XML but this just doesn't make sense. I have a dataset ds. I load the data from the dataset into an XML document and try to SelectNodes. Nothing. Here is the code
3
by: John Smith | last post by:
I'm trying to make some Javascript X-browser. foo is a HTML DOM. I have foo.documentElement.selectNodes("xpath"); in the source. I see later down there is also a foo.transformNode. Yuck. ...
3
by: Alan Mailer | last post by:
Ok, I've looked for an answer for this, because I'm sure it's been asked a thousand times... but no luck... so here goes. Imagine I want to create a NodeList based on an XPath statement. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.