I've got a Blog structure XML with namespaces. I don't have any problem printing all the posts title, date and content - Although it only works properly on Firefox (only tested on version 35).
My plan is to add a select tag so the user can filter the posts by language. I've tried many different things but none of them worked. Not sure if a browser limitation or wrong coding.
Here are the three main files:
Although you can see my source code here, here is the main JS method:
function LoadXMLWithXPath() { // load post titles and content using XPath
xmlDoc = loadXMLDoc(); // get XML content
var xml = xmlDoc.responseXML;
var path = "//p:title|//p:content|//p:date"; // get all titles and its content
var addContent = "";
if (typeof xml.evaluate !== 'undefined') { // if XML is not null. Enough for most browsers.
/*
* xml.evaluate ( xpathExpression, contextNode, namespaceResolver, resultType, result )
*/
var result = xml.evaluate(path, xml, // 'result' is a XPathResult object
function (prefix) {
switch (prefix) { // namespace resolver
case 'b': return 'http://ift.tt/1xqCQkv';
case 'a': return 'http://ift.tt/15nPYAf';
case 'p': return 'http://ift.tt/1xqCSZC';
default: null;
}
}, XPathResult.ANY_TYPE, null);
var nodes = xml.evaluate(path, xml,
function (prefix) {
switch (prefix) {
case 'b': return 'http://ift.tt/1xqCQkv';
case 'a': return 'http://ift.tt/15nPYAf';
case 'p': return 'http://ift.tt/1xqCSZC';
default: null;
}
}, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext(); // store the nodes content on the result object
title = true;
date = false;
content = false;
hr = 0;
testing = "";
while (result) {
if (hr % 3 == 0) {
addContent += '';
}
if (title) {
addContent += "<b>Title: </b>" + (result.childNodes[0].nodeValue) + "<br />";
title = false;
date = true;
} else if (date) {
addContent += "<b>Date: </b>" + (result.childNodes[0].nodeValue) + "<br />";
date = false;
content = true;
} else if (content) {
addContent += "<div class='article'><p><b>Content: </b>" + (result.childNodes[0].nodeValue) + "<p></div>";
content = false;
title = true;
}
hr++;
if (hr % 3 == 0) {
addContent += "<hr />";
}
result = nodes.iterateNext();
}
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined') {
// For IE only
xml.setProperty('SelectionLanguage', 'XPath');
xml.setProperty('SelectionNamespaces', 'xmlns:p="http://ift.tt/1xqCSZC"');
var nodes = xml.selectNodes(path);
var nodes = xml.selectNodes(path);
for (i = 0; i < nodes.length; i++) { addContent += nodes[i].childNodes[0].nodeValue + "<br><hr />"; }
}
document.getElementById("loadPathXML").innerHTML = addContent;
document.getElementById("testing").innerHTML = testing;
}
My most successful attempt so far was when printing only the posts title and filtering it by language:
var language = "es";
var path = "//p:title[@lang='" + language + "']";
But when using this method with the full code the website filters the titles but loads all dates and contents.
I've run out of ideas, so any ideas is very welcome. I'll test it and post the results here.
No comments:
Post a Comment