Arun Kumar wrote:
Suppose I have two tags <Date> and <Time> as below:
<Date>
<Value>2008-07-07</Value>
</Date>
<TestTag>
TestTag Text Node
</TestTag>
<Time>
<Value>20:15:45</Value>
</Time>
How can I find whether TestTag is present in between the <Date> and
<Time> tags or not?
The <TestTag> may present any where in the document. But I want to
know whether it is present between <Date> and <Time> tags or not?
TestTag is not the only tag that exists between Date and Time tags.
If Date, Time, and TestTag are all siblings, this might do it:
$(document).ready(function() {
$("Date").each(function() {
var $time = $(this).nextAll("Time:first");
var $date = $time.prevAll("Date:first");
if ($date[0] != this) return;
$date.nextAll("TestTag").each(function() {
if ($(this).nextAll("Time:first")[0] == $time[0]) {
process(this);
}
});
});
function process(tag) {
// Tag is a TestTag between a Date and Time...
}
});
If they are not, it might get tricky. There is a little added
complexity here to cover the possiblity of
<Date/>
<Date/>
<TestTag/>
<Time/>
in which the TestTag would be processed twice if you used the simpler
$(document).ready(function() {
$("Date").each(function() {
var $time = $(this).nextAll("Time:first");
$(this).nextAll("TestTag").each(function() {
if ($(this).nextAll("Time:first")[0] == $time[0]) {
process(this);
}
});
});
function process(tag) {
// Tag is a TestTag between a Date and Time...
}
});
But if you can guarantee that Dates and Times always come in matched
pairs, the latter would probably be better.
Cheers,
-- Scott