473,499 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IF statement to select nodes w/certain attribute values

57 New Member
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 4769
acoder
16,027 Recognized Expert Moderator MVP
I don't know if this will do what you want, but you could work with this example.
May 21 '07 #2
npm
57 New Member
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 Recognized Expert Moderator MVP
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 Recognized Expert Top Contributor
you can use an attribute


element.getAttribute("timezone");
May 21 '07 #5
npm
57 New Member
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 New Member
you can use an attribute


element.getAttribute("timezone");
Where would I put that in a script?
Thanks!
May 23 '07 #7
npm
57 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator MVP
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 New Member
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

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

Similar topics

13
1885
by: kieran | last post by:
Hi, I have the following SQL statement which is pulling a few details from a database. As you can see, there is only the one table from which i am creating a temporary copy. The reason I do...
1
10059
by: avinash | last post by:
hi my self avi i want to copy data from one table to other table,by giving certain condition and i want o use insert statement .in this i want to pass some value directly and some value from...
4
1832
by: bearclaws | last post by:
I want to select certain nodes from an XML file whose parent nodes contain a particular attribute id. For instance, here's a sample XML file: <AAA> <BBB id="111">text <CCC>text</CCC> </BBB>...
2
1341
by: aman.coe | last post by:
hello all Consider this xml file: <root> <body> <ElementX id="X1" Xproperty="X_Default_Value" /> <ElementX id="X2" /> </body> </root>
3
3304
by: Earl Teigrob | last post by:
I am considering writing a Class that Selects, Adds, Updates and Deletes Nodes in an XML File but do not what to reinvent the wheel. (See XML file below) That data format would emulate records...
2
10662
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
5
2160
by: GTi | last post by:
Whats wring with this code? <select class=EditField size="1" name="PlantUnitID" title="Select line"> <option value="0" >Standalone Unit</option> <option value="1" selected >Connected Unit...
1
6568
by: Sharat Koya | last post by:
I have a the following xml node. <doc tag1="a" tag2="b" tag3="c" docTag1="d" docTag2="e"/> I would like to output "abc" I have the following XPATH2.0 so far //doc/@*] which returns a node...
5
3670
by: AdrianGawrys | last post by:
Hi Guys, I have an xml similar to this one: <?xml version="1.0" encoding="UTF-8"?> <SystemUpdate ForceUpdate="false" > <A ForceUpdate="false"> <Book name="Black" /> <Book...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.