How to use the xpath.evaluate function in xpath

To help you get started, we’ve selected a few xpath examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DotJoshJohnson / vscode-xml / src / services / XPathEvaluator.ts View on Github external
static evaluate(query: string, xml: string, ignoreDefaultNamespace: boolean): EvaluatorResult {
        if (ignoreDefaultNamespace) {
            xml = xml.replace(/xmlns=".+"/g, (match: string) => {
                return match.replace(/xmlns/g, 'xmlns:default');
            });
        }
        
        let nodes: Node[] = new Array();
        
        let xdoc: Document = new DOMParser().parseFromString(xml, 'text/xml');
        
        let resolver: xpath.XPathNSResolver = xpath.createNSResolver(xdoc);
        let result: xpath.XPathResult = xpath.evaluate(
            query,            // xpathExpression
            xdoc,                        // contextNode
            resolver,                       // namespaceResolver
            xpath.XPathResult.ANY_TYPE, // resultType
            null                        // result
        )

        let evalResult = new EvaluatorResult();
        evalResult.type = EvaluatorResultType.SCALAR_TYPE;
        
        switch(result.resultType) {
            case xpath.XPathResult.NUMBER_TYPE:
                evalResult.result = result.numberValue;
                break; 
            case xpath.XPathResult.STRING_TYPE:
                evalResult.result = result.stringValue;
github DotJoshJohnson / vscode-xml / src / xpath / xpath-evaluator.ts View on Github external
static evaluate(query: string, xml: string, ignoreDefaultNamespace: boolean): EvaluatorResult {
        if (ignoreDefaultNamespace) {
            xml = xml.replace(/xmlns=".+"/g, (match: string) => {
                return match.replace(/xmlns/g, "xmlns:default");
            });
        }

        const nodes = new Array();
        const xdoc: Document = new DOMParser().parseFromString(xml, "text/xml");
        const resolver = (xpath as any).createNSResolver(xdoc);
        const xPathResult = xpath.evaluate(query, xdoc, resolver, 0, null);

        const evaluatorResult = new EvaluatorResult();
        evaluatorResult.type = EvaluatorResultType.SCALAR_TYPE;

        switch (xPathResult.resultType) {
            case XPathResultTypes.NUMBER_TYPE:
                evaluatorResult.result = xPathResult.numberValue;
                break;
            case XPathResultTypes.STRING_TYPE:
                evaluatorResult.result = xPathResult.stringValue;
                break;
            case XPathResultTypes.BOOLEAN_TYPE:
                evaluatorResult.result = xPathResult.booleanValue;
                break;
            case XPathResultTypes.UNORDERED_NODE_ITERATOR_TYPE:
            case XPathResultTypes.ORDERED_NODE_ITERATOR_TYPE:

xpath

DOM 3 XPath implemention and helper for node.js and the web

MIT
Latest version published 5 months ago

Package Health Score

84 / 100
Full package analysis