Hi guys,

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

<authors>
<author>
<surname>Middleton</surname>
<firstname>Karen L.</firstname>
</author>
<author>
<surname>Chambers</surname>
<firstname>Valrie</firstname>
</author>
</authors>

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

Member Avatar for rajarajan2017

Logic:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.