You are here: Reference > JavaScript > client-side > HTML DOM > methods > createNSResolver (document, XMLDocument)

createNSResolver method (document, XMLDocument)

Browser support:
Creates an XPathNSResolver object from the specified node that can be used to get namespace URIs from namespace prefixes in the scope of the 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.

Syntax:

object.createNSResolver (node);
You can find the related objects in the Supported by objects section below.

Parameters:

node
Required. A reference to the element that specifies the scope of the required namespaces.

Return value:

Returns the newly created XPathNSResolver object.

Example HTML code 1:

This example illustrates the use of the createNSResolver and evaluate methods:
Code
ajax.js
ns.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="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";
            }
            return null;
        }

        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>
    <button onclick="SendRequest ()">Get param tags with 'dotto' namespaces</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

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>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="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";
            }
            return null;
        }

        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>
    <button onclick="SendRequest ()">Get param tags with 'dotto' namespaces</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content