
September 27th, 2001, 05:47 AM
|
|
Junior Member
|
|
Join Date: Jul 2001
Location: Madrid, Spain
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Javascript and XML attributes
Hello,
I am trying to do something with JavaScript that would be quite
easy to do with XSL, but for reasons that would take to long to
explain, I need to do it in JavaScript.
Basically, If I have the following simple XML:
<Questionaire>
<Section title="section1">
<Question>
.
.
.
</Question>
</Section>
<Section title="section2">
<Question>
</Question>
</Section>
</Questionaire>
How do I extract/get the section attributes?
I have the following Javascript to process the file:
<HTML>
<head><title>XML File output</title></head>
<XML ID="xml_file" SRC="questionaire.xml"></XML>
<BODY>
<SCRIPT LANGUAGE="JavaScript1.1">
function parse_xml( node )
{
section_list = xml_file.XMLDocument.selectNodes( "Questionaire/Section" );
//Loop through the sections
for( i = 0; i < section_list.length; i++ )
{
// I want to get the attribute here
section_array = new section( section_list[i] );
question_list = section_list[i].selectNodes( "Question" );
// Now loop through each question in the section
for( j = 0; j < question_list.length; j++ )
{
question_array= new question( question_list[j] );
}
}
}
</SCRIPT>
</BODY>
</HTML>
I know that the logical solution would be to change the XML section
definition to the following:
<Section>
<SectionTitle>section1</SectionTitle>
<Question>
</Question>
</Section>
But the other way is still standard XML and you should be able
to get to the attribute with JavaScript.
I tried different combinations of the following with no success
selectSingleNode( Section[@title])
Any Ideas???
Brady
Last edited by BJohnson : September 27th, 2001 at 05:53 AM.
|