You are here: Reference > JavaScript > client-side > xml handling > methods > selectSingleNode (XMLDocument)

selectSingleNode method (XMLDocument)

Browser support:
Returns the first node that matches the specified XPath expression on the current node.
In Firefox, Google Chrome and Safari (and optionally in Opera), use the evaluate method instead.

Syntax:

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

Parameters:

xpathExpression
Required. String that specifies the XPath expression to evaluate. For more information, please visit the Location Paths (W3C) page.

Return value:

Returns the first matching node or null.

Example HTML code 1:

This example illustrates the use of the selectSingleNode method:
Code
ajax.js
news.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 = "news.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
                    Test_SelectSingleNode ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

        function Test_SelectSingleNode () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;

            if ('selectSingleNode' in xmlDoc) {
                var xmlNode = xmlDoc.selectSingleNode ("news/channel/item/title");
                if ('textContent' in xmlNode)
                    alert (xmlNode.textContent);
                else
                    alert (xmlNode.text);
            }
            else {
                alert ("Your browser doesn't support the selectSingleNode method!");
            }
        }
    </script>
</head>
<body>
    <button onclick="SendRequest ()">Test selectSingleNode method</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content