You are here: Reference > JavaScript > client-side > xml handling > objects > XPathNSResolver

XPathNSResolver object

Browser support:
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.

Syntax:

Methods that return the object:
object.createNSResolver (node)
Related objects:
The base interface, through which you can add new functionalities to the XPathNSResolver object, is the XPathNSResolver interface.

Possible members:

Methods:
lookupNamespaceURI
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>
    <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

External links:

User Contributed Comments

Post Content

Post Content