473,888 Members | 1,501 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 4817
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.getAttr ibute("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.getAttr ibute("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

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

Similar topics

13
1919
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 this is because in the table i only have the 'standIn' listed by integer and i want to return the 'standIn' by name. I hope this is clear enough.
1
10081
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 select statement , if i try i ll get error i.e all column of destination table (i.e in which i want to insert data) should match with all columns in values column some thing like this. plz give me some helpful suggetion on this
4
1847
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> <BBB id="222">
2
1364
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
3332
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 in a Database Table with a Primary Key for each Record (see xml sample below) and a flat file structure. I would use a class to manipulate this type of data structure extensivly if I had one. Does anyone know of such an animal?
2
10706
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 attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
5
2182
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</option> <option value="1" selected >Connected Unit 2</option> </select> <span onclick="NewWindow('pluginpage.html?EditUnitGUID='+this.form.PlantUnitID.options.value+'&DoPostBack=1','namexx',200,310);"
1
6658
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 list. Note that docTag* are ignored and there could be any number of tag*.
5
3691
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 name="BlackRed" />
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11180
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7148
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
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 we have to send another system
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.