XPathEval (XMLDocumentHandleOrXpathNodeHandle, XpathString [, index])

Result type:  An XPath node handle, or a string, or a number — depending on the XPath expression

Definition:  Evaluates an XPath in the context of an XML document that has been parsed with XMLParseFile or XMLParseString or relative to a node in that document that has been found with a previous call to XPathEval. When evaluating against the document, use an XPath anchored at the root node /. When evaluating against a previously found node, use an XPath anchored at the "current node" ./.

If the XPath expression identifies an element or set of elements, then the function result wil be a node handle that you can use with subsequent calls to XPathEval.

If the XPath expression explicitly requests a string or number (or boolean) result (e.g. using string(...), number(...) or .text()) then the return value will be a string or a number.

If the Xpath node handle you provide is actually a set of nodes, you should provide an index parameter to indicate which node in the set you want to be the anchor for the relative XPath. Otherwise the first node will be assumed. The index is 1-based.

As a special case you can use XPathEval(nodeSetHdl, "count") to get the number of nodes in a nodeset.


Getting indexed elements using the XPath [] notation.

    let doc = XmlParseFile("transactions.xml")    // filename only = use safe path (MoneyWorks Automation folder)
    let numTrans = XPathEval(doc, "count(/table/transaction)") // count nodes using XPath 
    foreach i in (1, numTrans)
        let t = XPathEval(doc, "/table/transaction["+i+"]") // return a singleton nodeset handle
        syslog(XPathEval(t, "string(./ourref)"))
        syslog(XPathEval(t, "string(./description)"))
        syslog(XPathEval(t, "number(./gross)"))

Getting indexed elements from a nodeset using an index parameter to XPathEval

    let doc = XmlParseFile("transactions.xml")    // filename only = use safe path (MoneyWorks Automation folder)
    let transSet = XPathEval(doc, "//table/transaction") // returns a nodeset with multiple elements
    let numTrans = XPathEval(transSet, "count") // special case to get nodeset count
    foreach i in (1, numTrans)
        syslog(XPathEval(transSet, "string(./ourref)", i))
        syslog(XPathEval(transSet, "string(./description)", i))
        syslog(XPathEval(transSet, "number(./gross)", i))

Availability:  MWScript scripts in MoneyWorks Gold v8.1.1 and later

Suite:  This function is part of the LibXML/Xpath XML parsing suite of functions.

See Also:

XMLFree: Free an XML document

XMLParseFile: Parse an XML file to an XML document

XMLParseString: Parse an XML string to an XML document