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!

how do I find a given node's index wrt doc root?

Status
Not open for further replies.

altsnd

Technical User
Dec 1, 2009
7
GB
Probably a dumb question, but I've run out of ways to ask google...

I'm doing a little flash app for vocabulary learning. The vocabulary items are <entry>s in an xml file, children of a given <chapter>. The user loops through the vocab entries, and has the option to see only entries from particular chapter(s) they select. What I need is, for the first vocab entry in every selected chapter, its index with respect to the root (so I can then pull a randomised list of vocab entries from all and only the selected chapters).

An abbreviated version of the xml follows:

Code:
<vocab>
<chapter>
   <chaptername>Numbers</chaptername>
   <entry>
      <tl>wahed</tl>
      <en>one</en>
   </entry>
   <entry>
      <tl>ithnaan</tl>
      <en>two</en>
   </entry>
</chapter>
<chapter>
   <chaptername>Pronouns</chaptername>
   <entry>
      <tl>ana</tl>
      <en>I, me</en>
   </entry>
</chapter>
</vocab>

The chapter selection is held in my arrChpts[] (array of checkboxes, indexed same as xml chapter indices), so I use a loop to test whether a checkbox was selected, count selected chapters ('nrChapters') and get a total of vocab entries to be included ('nrEntries'):

Code:
totChapters=dataXML.chapter.length();
totEntries=dataXML.chapter.entry.length();
if (arrChpts) {
   for (var i:int=0; i<totChapters; i++) {
      if (arrChpts[i].selected) {
         nrChapters++;
         nrEntries += dataXML.chapter[i].entry.length();
      }
   }
}

... and in that for-loop I'd like to store all the entry indices in that chapter but wrt root rather than the chapter. For example, with reference to above xml snippet, if a user selected the chapter "pronouns" but not "numbers", I'd want the index for "pronouns"'s first (and any subsequent) entries - in this case it would be '3' (third <entry> under root).

I've tried so many versions with childIndex and parentNode and whatnot, but none of it makes any sense (and unsurprisingly doesn't work). How do I do this?

The only alternative I can think of would be to store all the xml data of selected chapters in an array, but that seems so.. redundant!

Thanks for your help!
 
OK never mind - decided to just add things up...

Code:
if (arrChpts) {
   for (var h:int=0; h<totChapters; h++) {
      if (arrChpts[h].selected) {
         nrChapters++;
         var addNrEntries:int = dataXML.chapter[h].entry.length();
         nrEntries += addNrEntries;
         for (var k:int=0;k<addNrEntries;k++) {
            entries.push(cindex + k);
         }
      }
      //find current entry index (in absence of smart way..)
      cindex += dataXML.chapter[h].entry.length();
   }
}

Though if anybody knows of a less roundabout way, I'd appreciate it!
 
Are you interested in an XPath solution?

Tom Morrison
Micro Focus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top