473,441 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

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 4765
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

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

Similar topics

13
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.