PDA

View Full Version : sort xml elements in asp/jscript - xml dom


avivit
12-07-2004, 08:09 PM
Hi,

I am pretty new to xml.
I have built my xml file, and displayed the needed details on asp page using xml dom+jscript.

two questions:
1. How can I sort xml elements in alphabetic list for displaying?
For example, if I got the list of all <subject> elements, how can I display in the right order?

2. When I gathert a list of <title> element values, in asp+xml dom+js, same name appears more than once as it is so in the xml file )same title element value is used for different items).
How can I remove duplication, in displaying in asp, so I can get each value once, thus getting a list of all titles?

If possible in xml dom or javascript, great.

Of course, it should be cross browser.


(I prefer not to use xsl).

Thanks

Alex Vincent
12-08-2004, 06:55 AM
Could we see some source code, please? :) Samples of your XML and other work related to the problem at hand would help us figure this one out.

avivit
12-08-2004, 08:28 AM
As for my first question:
I have out the Subject element one node above the item node as,
I have many items in each subject, and I did not want to write the same subject thousand times, and to mispell it .

Following a sample of the xml file and the "calling" page.
I would like to show a list of all subjects values, in a select box, in alphabetic order, and then when selecting an option, to dispaly all the titles of the selected subject.

So 2 questions here, and the firsi is more urgent to me.

1. How to dispaly an alphabetic order of the subjects in the combo box in that matter.

2. How to select the titles of the selected subject for dispaly?
I guess I should keep the subject item number in a variable, and use it for dispalying the titles???


Sample of the xml file:
*********************************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<BOOKS>
<Subject>subject1
<book>
<Title>title1</Title>
<Description>text</Description>
</book>
<book>
<Title>title2</Title>
<Description>text</Description>
</book>
</Subject>
<Subject>subject2
<book>
<Title>title1</Title>
<Description>text</Description>
</book>
</Subject>
</BOOKS>
***************************************************
In the asp page that "reads" data from this xml file and displays it,
I use javascript and xml dom:
++++++++++++++++++++++++++++++++++++++++++++++++
<body>
<head>
<script language="javascript">
function getItems()
{ var i=document.searchBySubj.subjList.selectedIndex;
selectedSubj=document.searchBySubj.subjList[i].value;
//alert(selectedSubj);
}
</script>
.....
<FORM name="searchBySubj" ...>
<SELECT NAME="subjList" onchange="getItems()">
<script language="javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load("xmlPage.xml")
var x=xmlDoc.documentElement.childNodes; //root="books" tag

for (i = 0; i<x.length;i++) //Fill in options value with all subjects values
{ secondLevel=x.item(i).childNodes;
subjectVar=secondLevel.item(0).text;
document.write("<OPTION value="+subjectVar+">"+subjectVar)
}
</script>
</SELECT>
</FORM>
...
</body>
++++++++++++++++++++++++++++++++++++++++++++++++

Thanks very much for your help