[jQuery] Help with node navigation
Greetings,
I am learning jquery and have found it a great time saver so far. I
am building a thesaurus application but am having trouble with Xpath
expressions used to parse an XML document returned by an ajax query.
The XML that gets returned is in this format (truncated):
<response>
<hierarchy direction="narrower" max-levels="2">
<node>
<term>_top_</term>
<node>
<term>Agriculture</term>
<node><term>Agricultural diversification</term></node>
<node><term>Agricultural economics</term></node>
<node><term>Agricultural education</term></node>
</node>
<node><term>Boundaries</term></node>
<node><term>Buildings</term></node>
</node>
</hierarchy>
</response>
A few questions:
1) Is there a direct way to get the value which is "_top_" above? I
currently have this:
var BaseTerm;
$(xml).find('term:first').each(function(){
BaseTerm = $(this).text();
});
It works, but I don't see why I should have to use 'each'. Can I
address this node without iterating through a set of one element?
2) I would like to loop through a set of just the terms at the second
node deep level (i.e. "Agriculture","Boundaries" and "Buildings"). I
have tried a variety of ways with no luck. Here is an example of a
failed attempt:
$(xml).find(//term).each(function(){
var NT= $(this).text(); // get the narrower terms
});
3) I would like to test for the presence of subterms. Based on the
sample response, I would only want to get 'Agriculture'. Ideally this
would next in the sample loop above such as:
$(xml).find(//term).each(function(){
var NT= $(this).text();
var HasNarrowerTerms = NT.children();
});
Any assistance would be greatly appreciated in helping me parse /
traverse this response.
Cheers,
Andrew Koebrick