Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xmldom getting elements by id 1

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
I need to get the contents of several nodes based on their parent nodes id attribute.

XML looks like this

<?xml version='1.0' ?>
<data>
<page id="1">
....other elements
</page>
<page id="2">
....other elements
</page>
</data>

I need to use asp to get all values within elements that have a parent node with a specific id. For, example, getting all of the elements within the "page" node that has an id of 1

I did find an xmldom resource which listed things like "nodeByID" but I couldn't get that to work.

Thanks in advance for any help on this
 
Thanks for that but my eyes are bleeding from looking through such resources for the last 8 hours. If anyone could provide an actual working example that would be great.
 
did you try this simple example from
Code:
<script type="text/vbscript">

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")

for each x in xmlDoc.documentElement.childNodes
  document.write("<b>" & x.nodename & ":</b> ")
  document.write(x.text)
  document.write("<br />")
next

</script>

-DNG
 
>for each x in xmlDoc.documentElement.childNodes
To answer op's question, it's this.
[tt]for each x in xmlDoc.selectNodes("/data/page[@id='2']/*")[/tt]
 
that example doesn't select the nodes within an element based on a specific id.

In my example I need to select everything between

<page id="1">
....other elements
</page>

That means all of the child nodes of the page node that has an id of 1
 
thanks tsuji, that sounds a bit more like it. By the way my last reply was to the post before yours
 
Whilst we are at it, can you tell me how to get the actual "id" value for all of the "page" nodes in my example xml code.
 
Do this to get to the essential.
[tt]
for each x in xmlDoc.selectNodes("/data/page[not(@id = ./preceding-sibling::*/@id)]")
response.write x.getAttribute("id") & "<br />"
next
[/tt]
 
getting this error

Expected token ')' found ':'. /data/page[not(@id = ./preceding-sibling-->:<--:*/@id)]
 
Is it a cut-and-paste that produces that error? There is no line break and no spurious space in the path except surrounding "=".
 
I did cut and paste but there is no line break and the only spaces are around the "=
 
I would check for other reason for the error to turn up. I make a test case and it works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top