Hi guys,

I have a xml file that contains all the articles with few authors for each of them.
something like this;

<firstname>Karen L.</firstname>

I can display them in HTML well the problem is, the way I want to display is if authors more than 1 then there will be 'and' that show which is the last author, something like BIBLIOGRAPHY structure;

ex: MiddleTon, Karen L. and Valrie, Chambers ...

so I have js file that go into the xml file and display them. So far I can display each of them but not in the structure I want it.

This is my js code;

function displayClasses(doc) {
var journalNode = doc.getElementsByTagName('journals')[0];
    for (i=0; i<journalNode.childNodes.length; i++) {
        var journal = journalNode.childNodes[i];
		if (journal.tagName == 'journal')
			var text = '';
                       var authorNode = journal.getElementsByTagName('author');

			if (authorNode.length > 1) {
			for(ctr=0;ctr<authorNode.length;ctr++) {
				var author = authorNode[ctr];
				var firstNameNode = author.getElementsByTagName('firstname')[0];
				var firstName = firstNameNode.firstChild.nodeValue;
				var surname = author.getElementsByTagName('surname')
				var surnameValue = surname[0].firstChild.nodeValue;
				var surname2 = surname[0].lastChild.nodeValue;
				var myEl = document.createElement('p');
			         myEl.innerHTML = text
				text += surnameValue + ', ' + firstName + ' and ' + surname2 + '<br />';

Can anyone point me a right direction to solve this?
I am a noob in AJAX

Thanks in advanced

Edited by phingko: n/a

8 Years
Discussion Span
Last Post by rajarajan2017


for(ctr=0;ctr<authorNode.length;ctr++) {
var add='';
if (ctr==authorNode.length-2)  add='and'
text += add + surnameValue + ', ' + firstName + '<br />';

Within your loop create a empty string variable add, and check a condition before reach last node. and use the text like below

text += add + surnameValue + ', ' + firstName + '<br />';

Hope you understand the logic

Edited by rajarajan2017: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.