javascript cross browser xpath function

//**************************************
//     
// Name: javascript cross browser xpath 
//     function
// Description:For use with AJAX web app
//     lications, allows xpath expressions on X
//     ML data objects.
// By: Charles Toepfer
//
// Inputs:XML DOM element, xPath string
//
// Returns:resulting XML DOM
//
// Assumes:Here are some sample xpath st
//     atements:
Select nodes where the attribute name is Bob Mickles: //*[@name='Bob Mickles']
Select nodes that contain bob in the text: //nodes/node/*[contains(.,'Bob')];
or .. //nodes/node/node[contains(.,'Bob')];
Simple path select: //nodes/node
Select nodes that contain bob, retrieve child 'node': //nodes/node[node='bob']/node
Select nodes that nodes that contain bob in nodeA and mickles in nodeB: //nodes/node[nodeA='bob' and nodeB='mickles']
Select nodes where the name node is unique: //nodes/node[not(name = preceding-sibling::node/name)]
//
//This code is copyrighted and has
// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=5314&lngWId=2
//for details.
//**************************************
//     

function xPath(oNodes, sXPath)


    {
    /*
    xPath function by Charles Toepfer: toepfer_c@hotmail.com 
    use: 'resulting xml dom' = xPath('xml dom object', 'xpath string');
    */
    if(oNodes)


        {
        if(window.XMLHttpRequest)


            { 
            try 


                {
                var oXpe = new XPathEvaluator();
                var oNsResolver = oXpe.createNSResolver(oNodes.ownerDocument == null ? oNodes.documentElement : oNodes.ownerDocument.documentElement);
                var oResult = oXpe.evaluate(sXPath, oNodes, oNsResolver, 0, null);
                var aFound = [];
                var oRes;
                while (oRes = oResult.iterateNext())


                    {
                    aFound.push(oRes);
                }

                return aFound;
            }

            catch (e) 


                {
                alert(e.description);
            }

        }

        else


            {
            try


                {
                oNodes.setProperty ("SelectionLanguage", "XPath")
                var oSelectedNode = oNodes.documentElement.selectNodes(sXPath);
                return oSelectedNode;
            }

            catch (e) 


                {
                alert(e.description);
            }

        }

    }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: