Cookies improve the way our website works, by using this website you are agreeing to our use of cookies. For more information see our privacy policy.
OK
Can be used to get namespace URIs from namespace prefices in the scope of a node.
When an XPath expression specified for the evaluate method contains namespaces, then an XPathNSResolver object also needs to be set for this method.
For further details, see the page for the evaluate method.
Retrieves the namespace URI associated with the specified namespace prefix in the scope of the current node.
Example HTML code 1:
This example illustrates the use of the XPathNSResolver object:
Code
ajax.js
ns.xml
<head><scripttype="text/javascript"src="ajax.js"></script><scripttype="text/javascript">var httpRequest = null;
function SendRequest () {
if (!httpRequest) {
httpRequest = CreateHTTPRequestObject (); // defined in ajax.js
}
if (httpRequest) {
// The requested file must be in the same domain that the page is served from.
var url = "ns.xml";
httpRequest.open ("GET", url, true); // async
httpRequest.onreadystatechange = OnStateChange;
httpRequest.send (null);
}
}
function OnStateChange () {
if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js
GetParamTags ();
}
else {
alert ("Operation failed.");
}
}
}
function NSResolver (nsPrefix) {
if (nsPrefix == "dotto") {
return"http://help.dottoro.com/NS";
}
returnnull;
}
function GetParamTags () {
var xmlDoc = ParseHTTPResponse (httpRequest); // defined in ajax.js
if (!xmlDoc)
return;
if (xmlDoc.createNSResolver) {
var nsResolver = xmlDoc.createNSResolver (xmlDoc.documentElement);
var params = xmlDoc.evaluate("//dotto:param", xmlDoc.documentElement, nsResolver, XPathResult.ANY_TYPE, null);
var param = params.iterateNext();
while (param) {
var paramName = param.getAttribute ("name");
var paramValue = param.getAttribute ("value");
alert ("<dotto:param name='" + paramName + "' value='" + paramValue + "' />");
param = params.iterateNext();
}
}
else {
alert ("Your browser doesn't support the createNSResolver method!");
}
}
</script></head><body><buttononclick="SendRequest ()">Get param tags with 'dotto' namespaces</button></body>
function CreateHTTPRequestObject () {
// although IE supports the XMLHttpRequest object, but it does not work on local files.
var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
if (window.XMLHttpRequest && !forceActiveX) {
returnnewXMLHttpRequest();
}
else {
try {
returnnewActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
alert ("Your browser doesn't support XML handling!");
returnnull;
}
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
returnnewActiveXObject(progIDs[i]);
} catch(e) {};
}
}
returnnull;
}
function CreateXMLDocumentObject (rootName) {
if (!rootName) {
rootName = "";
}
var xmlDoc = CreateMSXMLDocumentObject ();
if (xmlDoc) {
if (rootName) {
var rootNode = xmlDoc.createElement (rootName);
xmlDoc.appendChild (rootNode);
}
}
else {
if (document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument ("", rootName, null);
}
}
return xmlDoc;
}
function ParseHTTPResponse (httpRequest) {
var xmlDoc = httpRequest.responseXML;
// if responseXML is not valid, try to create the XML document from the responseText property
if (!xmlDoc || !xmlDoc.documentElement) {
if (window.DOMParser) {
var parser = newDOMParser();
try {
xmlDoc = parser.parseFromString (httpRequest.responseText, "text/xml");
} catch (e) {
alert ("XML parsing error");
returnnull;
};
}
else {
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
returnnull;
}
xmlDoc.loadXML (httpRequest.responseText);
}
}
// if there was an error while parsing the XML document
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
}
if (errorMsg) {
alert (errorMsg);
returnnull;
}
// ok, the XML document is valid
return xmlDoc;
}
// returns whether the HTTP request was successful
function IsRequestSuccessful (httpRequest) {
// IE: sometimes 1223 instead of 204
var success = (httpRequest.status == 0 ||
(httpRequest.status >= 200 && httpRequest.status < 300) ||
httpRequest.status == 304 || httpRequest.status == 1223);
return success;
}
<?xmlversion="1.0"encoding="utf-8"?><nsxmlns="http://help.dottoro.com/defaultNS"xmlns:dotto="http://help.dottoro.com/NS"><itemdotto:color="red"><dotto:title>Namespace example</dotto:title><dotto:description>Describes how to use namespaces with tags and attributes.</dotto:description><dotto:pubDate>Wed, 17 Aug 2009 13:58:41 +0200</dotto:pubDate><dotto:link>http://help.dottoro.com</dotto:link></item><order><dotto:paramname="sortBy"value="title"/><dotto:paramname="sortDir"value="ascending"/></order></ns>
This example is equivalent to the previous one, but it implements a namespace resolver function for the evaluate method:
Code
ajax.js
ns.xml
<head><scripttype="text/javascript"src="ajax.js"></script><scripttype="text/javascript">var httpRequest = null;
function SendRequest () {
if (!httpRequest) {
httpRequest = CreateHTTPRequestObject (); // defined in ajax.js
}
if (httpRequest) {
// The requested file must be in the same domain that the page is served from.
var url = "ns.xml";
httpRequest.open ("GET", url, true); // async
httpRequest.onreadystatechange = OnStateChange;
httpRequest.send (null);
}
}
function OnStateChange () {
if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js
GetParamTags ();
}
else {
alert ("Operation failed.");
}
}
}
function NSResolver (nsPrefix) {
if (nsPrefix == "dotto") {
return"http://help.dottoro.com/NS";
}
returnnull;
}
function GetParamTags () {
var xmlDoc = ParseHTTPResponse (httpRequest); // defined in ajax.js
if (!xmlDoc)
return;
if (xmlDoc.createNSResolver) {
var params = xmlDoc.evaluate("//dotto:param", xmlDoc.documentElement, NSResolver, XPathResult.ANY_TYPE, null);
var param = params.iterateNext();
while (param) {
var paramName = param.getAttribute ("name");
var paramValue = param.getAttribute ("value");
alert ("<dotto:param name='" + paramName + "' value='" + paramValue + "' />");
param = params.iterateNext();
}
}
else {
alert ("Your browser doesn't support the createNSResolver method!");
}
}
</script></head><body><buttononclick="SendRequest ()">Get param tags with 'dotto' namespaces</button></body>
function CreateHTTPRequestObject () {
// although IE supports the XMLHttpRequest object, but it does not work on local files.
var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
if (window.XMLHttpRequest && !forceActiveX) {
returnnewXMLHttpRequest();
}
else {
try {
returnnewActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
alert ("Your browser doesn't support XML handling!");
returnnull;
}
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
returnnewActiveXObject(progIDs[i]);
} catch(e) {};
}
}
returnnull;
}
function CreateXMLDocumentObject (rootName) {
if (!rootName) {
rootName = "";
}
var xmlDoc = CreateMSXMLDocumentObject ();
if (xmlDoc) {
if (rootName) {
var rootNode = xmlDoc.createElement (rootName);
xmlDoc.appendChild (rootNode);
}
}
else {
if (document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument ("", rootName, null);
}
}
return xmlDoc;
}
function ParseHTTPResponse (httpRequest) {
var xmlDoc = httpRequest.responseXML;
// if responseXML is not valid, try to create the XML document from the responseText property
if (!xmlDoc || !xmlDoc.documentElement) {
if (window.DOMParser) {
var parser = newDOMParser();
try {
xmlDoc = parser.parseFromString (httpRequest.responseText, "text/xml");
} catch (e) {
alert ("XML parsing error");
returnnull;
};
}
else {
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
returnnull;
}
xmlDoc.loadXML (httpRequest.responseText);
}
}
// if there was an error while parsing the XML document
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
}
if (errorMsg) {
alert (errorMsg);
returnnull;
}
// ok, the XML document is valid
return xmlDoc;
}
// returns whether the HTTP request was successful
function IsRequestSuccessful (httpRequest) {
// IE: sometimes 1223 instead of 204
var success = (httpRequest.status == 0 ||
(httpRequest.status >= 200 && httpRequest.status < 300) ||
httpRequest.status == 304 || httpRequest.status == 1223);
return success;
}
<?xmlversion="1.0"encoding="utf-8"?><nsxmlns="http://help.dottoro.com/defaultNS"xmlns:dotto="http://help.dottoro.com/NS"><itemdotto:color="red"><dotto:title>Namespace example</dotto:title><dotto:description>Describes how to use namespaces with tags and attributes.</dotto:description><dotto:pubDate>Wed, 17 Aug 2009 13:58:41 +0200</dotto:pubDate><dotto:link>http://help.dottoro.com</dotto:link></item><order><dotto:paramname="sortBy"value="title"/><dotto:paramname="sortDir"value="ascending"/></order></ns>