SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Find whether a Tag is present in between two tags or not

    Suppose I have two tags <Date> and <Time> as below:
    Code HTML4Strict:
    <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?

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getElementsByTagName('date'), loop through all the Date elements and then inside of that check for getElementsByTagName('testtag') (length property)

  3. #3
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TestTag is not present in Date Tag...

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should work:

    Code JavaScript:
    var TestTag = document.getElementsByTagName('TestTag');
    for ( var i = 0 ; i < TestTag.length ; ++i ) {
        var next = (TestTag[i].nextSibling.nodeName=='#text') ? TestTag[i].nextSibling.nextSibling : TestTag[i].nextSibling;
        var previous = (TestTag[i].previousSibling.nodeName=='#text') ? TestTag[i].previousSibling.previousSibling : TestTag[i].previousSibling;
        if(next.nodeName=='Time'&&previous.nodeName=='Date') {
            alert('TestTag is present between date and time')
        }
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    But TestTag is not the only tag that exists between Date and Time tags. in this case, what should I do?

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm... You'd have to take a walk through the DOM in both directions. Tricky...

    OR you could get the whole DOM into a string and then test it somehow to determine whether your pattern is there.

    Getting the whole DOM:

    Code JavaScript:
    function showDOM(){
    var s = document.getElementsByTagName('*');
    var str = '';
    for(var i=0;i<s.length;i++)
    str+=s[i].nodeName+'<br>';
    return;
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If they are all in the same nest level just loop through the parent element, save the number of each, then check if the number of testtag is bigger than the number of Date and smaller than Time.

    edit; example:

    Code javascript:
    var i = 0
     ,els = document.getElementsByTagName('parent').getElementsByTagName('*')
     ,l = els.length
     ,nodeN
     ,date
     ,testtag
     ,time;
     
    for ( ; i < l; i++){
    	nodeN = els[i].nodeName
    	if (nodeN == "Date")
    		date = i;
    	if (nodeN == "TestTag")
    		testtag = i;
    	if (nodeN == "Time")
    		time = i;
    }
    if (testtag > date && testtag < time)
    	//do the do
    mmj

  8. #8
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any easy way using jQuery?

  9. #9
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ArunB View Post
    Is there any easy way using jQuery?
    Its all JavaScript, all jQuery would do is facilitate the DOM retriaval.

    There might be a way to check this with XPath, however I could easily be wrong as XPath is yet to be explored by me.
    mmj


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •