<photos>
<photo big="images/lilypads.jpg" thumb="images/lilypads_t.jpg">
<name>Lily Pads</name>
<description>Saigon: I'd lie to spend more time there.</description>
<category>travel, flora</category>
</photo>
<photo big="images/buddha.jpg" thumb="images/buddha_t.jpg">
<name>Sleeping Buddha</name>
<description>Nha Tran: I liked it because it had big feet</description>
<category>travel, buddha</category>
</photo>
</photos>
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
Last edited by marilynn.fowler; 05-23-2012 at 02:28 AM..
Reason: typos
that is going to be problematic. first I’d say you have un-normalised data in the <category> element, so you would actually need a script to find your matches. I would recommend to use a <category> tag for each category so you can match more easily. but without the use of XPath I see no simple way to achieve that without an explicit text search.
and that XML is not well-formed ...
__________________
please post your code wrapped in [CODE] [/CODE] tags
if I understand correctly what you are trying to do, maybe the below will help - of course you will have to reconstruct whatever you want to be shown from the nodes in the xml, but this example shows the text from the name node if the term in the search box matches part or all of the category.
You have one major problem with your xml - you are self-closing your photo tags:
Thank you, xelawho. I had tried code very similar to that and while it gave me a subset of the thumbnails, if I used previous/next buttons, it still cycled through all of the photo elements. I guess my real question is whether it is possible to modify the initial value of x variable so that it only gets the array of photo elements where the category contains insects?
var x = xmlDoc.getElementsByTagName("photo");
Can I initially set x to be xmlDoc.getElementsByTagName("photo[category='insect']") or something along those lines?
And my actual XML document is well formed, I am just pitiful at retyping on an iPad
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx
I guess my real question is whether it is possible to modify the initial value of x variable so that it only gets the array of photo elements where the category contains insects?
why not just push everything that matches your criteria onto a separate array, or am I missing something fundamental here?
Code:
<body>
<input type="text" id="inp"/><input type="button" value="search" onclick="searchPics()"/>
<div id="results"></div>
<input type="button" value="<" onclick="showPics('down')"/><input type="button" value=">" onclick="showPics('up')"/>
<script type="text/javascript">
var xml;
var tmp;
function downloadUrl(callback) {
url="gallery.xml"
var xmlFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
xmlFile.onreadystatechange = function() {
if (xmlFile.readyState == 4) {
callback(xmlFile, xmlFile.status);
}
};
xmlFile.open("GET", url, true);
xmlFile.send(null);
}
downloadUrl(function(data) {
xml = data.responseXML;
});
function searchPics(){
tmp=[];
document.getElementById("results").innerHTML=""
var term=document.getElementById("inp").value
var pics = xml.getElementsByTagName("photo");
for (var i = 0; i < pics.length; i++) {
var cat =pics[i].getElementsByTagName("category")[0]
if (cat.firstChild.nodeValue.match(term)){
tmp.push(pics[i].getElementsByTagName("name")[0].firstChild.nodeValue)
}
}
document.getElementById("results").innerHTML=tmp[0]
}
function showPics(dir){
if (dir=="up"){
tmp.push(tmp.shift())
} else{
tmp.unshift(tmp.pop())
}
document.getElementById("results").innerHTML=tmp[0]
}
</script>
</body>
Thank you for replying, xelawho. I'm going to go through it line by line to see if I understand how it works. I got sidetracked by other projects, so I know I'm going to have questions, but I appreciate the time you took giving me an answer.
__________________
Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx