469,315 Members | 1,510 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

IF statement to select nodes w/certain attribute values

57
Hi,
I'm trying to add an IF statement to this javascript that will select/display only those xml nodes either with a certain attribute or attribute value.

Here's my xml code:

Expand|Select|Wrap|Line Numbers
  1. <stations>
  2.     <station timezone="PT" PT="">
  3.         <city>Seattle</city>
  4.         <freq>93.1 FM</freq>
  5.     </station>
  6.     <station timezone="MT" MT="">
  7.         <city>Phoenix</city>
  8.         <freq>95.7 FM</freq>
  9.     </station>
  10.     <station timezone="CT" CT="">
  11.         <city>Nashville</city>
  12.         <freq>99.9 FM</freq>
  13.     </station>
  14.     <station timezone="ET" ET="">
  15.         <city>Atlanta</city>
  16.         <freq>107.9 FM</freq>
  17.     </station>
  18. </stations>
  19.  
I've got two different ways to denote the timezone because I've gotten one IF statement to work in Firefox but not IE6, and a different script (using xpath) that works in IE6 but not Firefox (it appears the xpath isn't working in FF).

Here's the one that works in FF (but not IE6):

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     xmlDoc=loadXMLDoc("stations.xml");
  3.     x=xmlDoc.getElementsByTagName("city");
  4.     for (i=0;i<x.length;i++)
  5.         {
  6.         if (x[i].parentNode.hasAttribute("MT"))
  7.             {
  8.             document.write(x[i].childNodes[0].nodeValue)
  9.             document.write("<br />")
  10.             }
  11.         }
  12. </script>
  13.  
And here's the one that works in IE6 (but not FF):

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     xmlDoc=loadXMLDoc("stations.xml");
  3.     xmlDoc.setProperty("SelectionLanguage", "XPath");
  4.     x=xmlDoc.selectNodes("//station[@tmzn='MT']/city");
  5.     for (i=0;i<x.length;i++)
  6.         {
  7.         document.write(x[i].childNodes[0].nodeValue)
  8.         document.write("<br />")
  9.         }
  10. </script>
  11.  
And for further reference, here's my external javascript file I'm using to help load the xml:

Expand|Select|Wrap|Line Numbers
  1. function loadXMLDoc(dname)
  2. {
  3. var xmlDoc;
  4. // code for IE
  5. if (window.ActiveXObject)
  6. {
  7. xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  8. }
  9. // code for Mozilla, Firefox, Opera, etc.
  10. else if (document.implementation && document.implementation.createDocument)
  11. {
  12. xmlDoc=document.implementation.createDocument("","",null);
  13. }
  14. else
  15. {
  16. alert('Your browser cannot handle this script');
  17. }
  18. xmlDoc.async=false;
  19. xmlDoc.load(dname);
  20. return(xmlDoc);
  21. }
  22.  
Is there an IF statement that will work in both (maybe even in Safari, too?) or is there a way to use xpath so it will work in FF? I'm new to this, so it's kinda confusing.

Thanks!
May 19 '07 #1
11 4612
acoder
16,027 Expert Mod 8TB
I don't know if this will do what you want, but you could work with this example.
May 21 '07 #2
npm
57
Not exactly...
Sorry I should have been more clear on what I'm trying to get this script to do. I was hoping to have it go through the xml file and display only those cities (or frequencies) that are in a certain timezone.

Do I have the xml set up wrong? Should I put the timezone as another child element of <station> instead as an attribute of <station>? Would that make it easier to come up with a javascript that can sort that out?

Thanks, every little bit helps since I'm new to xml and still fairly new to javascript.
May 21 '07 #3
acoder
16,027 Expert Mod 8TB
Not exactly...
Sorry I should have been more clear on what I'm trying to get this script to do. I was hoping to have it go through the xml file and display only those cities (or frequencies) that are in a certain timezone.

Do I have the xml set up wrong? Should I put the timezone as another child element of <station> instead as an attribute of <station>? Would that make it easier to come up with a javascript that can sort that out?

Thanks, every little bit helps since I'm new to xml and still fairly new to javascript.
Yes, perhaps that should solve your problem. Is the xml just an example or do you expect that multiple stations will have the same timezone? Perhaps you could ask about the XML element of this problem in the XML forum?
May 21 '07 #4
iam_clint
1,208 Expert 1GB
you can use an attribute


element.getAttribute("timezone");
May 21 '07 #5
npm
57
Yes, I will have multiple stations in multiple states, but I also want to list the stations in pages corresponding to their timezone. The problem arises when one state has stations in more than one timezone. I haven't found/come up with a javascript that will display a city based on what timezone it's in, at least a script that will work in Firefox & IE or other browsers as well.

Thanks!
May 21 '07 #6
npm
57
you can use an attribute


element.getAttribute("timezone");
Where would I put that in a script?
Thanks!
May 23 '07 #7
npm
57
I'm not sure if this is the correct place to put the if statement, but I tyring to find an if statement to display only those <city> elements whose parent node <station> has a specific attribute value (in this case either Central or Eastern timezone).

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3.     xmlDoc=loadXMLDoc("stations.xml");
  4.     x=xmlDoc.getElementsByTagName("city");
  5.     for (i=0;i<x.length;i++)
  6.         {
  7.         if (x[i].parentNode.WHAT_GOES_HERE?)
  8.             {
  9.             document.write(x[i].childNodes[0].nodeValue)
  10.             document.write("<br />")
  11.             }
  12.         }
  13. //-->
  14. </script>
  15.  
Here's a sample xml file it would be selecting from:
Expand|Select|Wrap|Line Numbers
  1. <stations>
  2.     <station timezone="Eastern">
  3.         <city>Chattanooga</city>
  4.         <freq>99.9 FM</freq>
  5.     </station>
  6.     <station timezone="Eastern">
  7.         <city>Knoxville</city>
  8.         <freq>107.9 FM</freq>
  9.     </station>
  10.     <station timezone="Central">
  11.         <city>Memphis</city>
  12.         <freq>93.1 FM</freq>
  13.     </station>
  14.     <station timezone="Central">
  15.         <city>Nashville</city>
  16.         <freq>95.7 FM</freq>
  17.     </station>
  18. </stations>
  19.  
May 23 '07 #8
gits
5,390 Expert Mod 4TB
hi ...

it's the right place :) and WHAT_GOES_HERE has to be:

Expand|Select|Wrap|Line Numbers
  1. // when you want to get the EASTERN-zone
  2. x[i].parentNode.getAttribute('timezone') == 'Eastern'
  3.  
kind regards ...
May 23 '07 #9
gits
5,390 Expert Mod 4TB
Where would I put that in a script?
Thanks!
have a look at your other posting that concerns the same issue ...

kind regards ...
May 23 '07 #10
acoder
16,027 Expert Mod 8TB
have a look at your other posting that concerns the same issue ...

kind regards ...
No longer "other posting" because I've merged the threads!
May 23 '07 #11
npm
57
hi ...

it's the right place :) and WHAT_GOES_HERE has to be:

Expand|Select|Wrap|Line Numbers
  1. // when you want to get the EASTERN-zone
  2. x[i].parentNode.getAttribute('timezone') == 'Eastern'
  3.  
kind regards ...

Oh man...
Thank you! I knew it was some simple line of code that would pick out the ones I needed. It works just fine in both FF and IE6, hopefully now it works in others as well.

THANKS AGAIN!
May 24 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by kieran | last post: by
5 posts views Thread by GTi | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.