npm 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: -
<stations>
-
<station timezone="PT" PT="">
-
<city>Seattle</city>
-
<freq>93.1 FM</freq>
-
</station>
-
<station timezone="MT" MT="">
-
<city>Phoenix</city>
-
<freq>95.7 FM</freq>
-
</station>
-
<station timezone="CT" CT="">
-
<city>Nashville</city>
-
<freq>99.9 FM</freq>
-
</station>
-
<station timezone="ET" ET="">
-
<city>Atlanta</city>
-
<freq>107.9 FM</freq>
-
</station>
-
</stations>
-
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): -
<script type="text/javascript">
-
xmlDoc=loadXMLDoc("stations.xml");
-
x=xmlDoc.getElementsByTagName("city");
-
for (i=0;i<x.length;i++)
-
{
-
if (x[i].parentNode.hasAttribute("MT"))
-
{
-
document.write(x[i].childNodes[0].nodeValue)
-
document.write("<br />")
-
}
-
}
-
</script>
-
And here's the one that works in IE6 (but not FF): -
<script type="text/javascript">
-
xmlDoc=loadXMLDoc("stations.xml");
-
xmlDoc.setProperty("SelectionLanguage", "XPath");
-
x=xmlDoc.selectNodes("//station[@tmzn='MT']/city");
-
for (i=0;i<x.length;i++)
-
{
-
document.write(x[i].childNodes[0].nodeValue)
-
document.write("<br />")
-
}
-
</script>
-
And for further reference, here's my external javascript file I'm using to help load the xml: -
function loadXMLDoc(dname)
-
{
-
var xmlDoc;
-
// code for IE
-
if (window.ActiveXObject)
-
{
-
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
-
}
-
// code for Mozilla, Firefox, Opera, etc.
-
else if (document.implementation && document.implementation.createDocument)
-
{
-
xmlDoc=document.implementation.createDocument("","",null);
-
}
-
else
-
{
-
alert('Your browser cannot handle this script');
-
}
-
xmlDoc.async=false;
-
xmlDoc.load(dname);
-
return(xmlDoc);
-
}
-
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!
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.
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.
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?
iam_clint 1,208
Recognized Expert Top Contributor
you can use an attribute
element.getAttr ibute("timezone ");
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!
npm 57
New Member
you can use an attribute
element.getAttr ibute("timezone ");
Where would I put that in a script?
Thanks!
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). -
<script type="text/javascript">
-
<!--
-
xmlDoc=loadXMLDoc("stations.xml");
-
x=xmlDoc.getElementsByTagName("city");
-
for (i=0;i<x.length;i++)
-
{
-
if (x[i].parentNode.WHAT_GOES_HERE?)
-
{
-
document.write(x[i].childNodes[0].nodeValue)
-
document.write("<br />")
-
}
-
}
-
//-->
-
</script>
-
Here's a sample xml file it would be selecting from: -
<stations>
-
<station timezone="Eastern">
-
<city>Chattanooga</city>
-
<freq>99.9 FM</freq>
-
</station>
-
<station timezone="Eastern">
-
<city>Knoxville</city>
-
<freq>107.9 FM</freq>
-
</station>
-
<station timezone="Central">
-
<city>Memphis</city>
-
<freq>93.1 FM</freq>
-
</station>
-
<station timezone="Central">
-
<city>Nashville</city>
-
<freq>95.7 FM</freq>
-
</station>
-
</stations>
-
gits 5,390
Recognized Expert Moderator Expert
hi ...
it's the right place :) and WHAT_GOES_HERE has to be: -
// when you want to get the EASTERN-zone
-
x[i].parentNode.getAttribute('timezone') == 'Eastern'
-
kind regards ...
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 ...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
|
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">
|
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>
|
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?
| |
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...
|
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);"
|
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*.
|
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" />
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |