﻿/**
 *  select content of node by xpath
 *  return '' no text node finded
 */

function SelectSingleNode(xmlDoc, elementPath)
{
    var sReturn = '';
    // if IE
    if (xmlDoc && elementPath)
    {
    	if(window.ActiveXObject)
        {
            if (xmlDoc.selectSingleNode(elementPath))
            {
                sReturn = xmlDoc.selectSingleNode(elementPath).text;
            }
        }
        //other
        else
        {
            try
            {
                var xpe = new XPathEvaluator();
                var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
                var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                if (results.singleNodeValue && results.singleNodeValue.firstChild)
                {
                    sReturn = results.singleNodeValue.firstChild.nodeValue;
                }
            }
            catch(e)
            {
                sReturn = 'failed opening : ' + elementPath;
            }
        }
    }
    return sReturn;
}

