DesignLiquido / xslt-processor

A JavaScript XSLT processor without native library dependencies
GNU Lesser General Public License v3.0
102 stars 31 forks source link

Parsing XPath expression in Node.js #23

Closed mig82 closed 5 years ago

mig82 commented 5 years ago

Can't figure out how to use this to parse an XPath expression in Node.js. If I just do this:

var xsltProc = require("xslt-processor")
console.log(xsltProc)

This is all that comes out:

Object {xmlParse: function(), xsltProcess: function()}

Just an object with two functions of which none is the xpathParse function tested here. Can you please shed some light on how to do this? The description of the package states:

Because XSLT uses XPath, it also contains an implementation of XPath that can be used independently of XSLT

So I'm assuming it's possible.

mig82 commented 5 years ago

I'm afraid 9798f31 doesn't really address the issue. Notice I'm talking about using this in Node.js. I believe for this to work, you'd have to explicitly add exports.xpathParse = xpathParse to your dist/xslt-processor.js file. Is this a modification you'd be willing to do? I'd very much appreciate it and I'm sure other Node users would as well :)

johanneswilm commented 5 years ago

@mig82 Have you tried it? xpathParse is now as exposed as the other properties. dist/xslt-processor.js is merely the processed output file of what you can see here.

johanneswilm commented 5 years ago

Inspecting the out put dist/xslt-processor.js file it does indead end with

...;exports.xpathParse=xpathParse;

So I'm guessing you reported here without actually trying it out?

mig82 commented 5 years ago

Actually, I did have a look. And what it exports is xmlParse, not xpathParse.

$ tail node_modules/xslt-processor/dist/xslt-processor.js 
                }
            }
            node = node.parentNode;
        }
    }
    return ret;
}

exports.xsltProcess = xsltProcess;
exports.xmlParse = xmlParse;
mig82 commented 5 years ago

Sorry. Still using the older version. My bad.

mig82 commented 5 years ago

Ok, now xpathParse is exposed. The problem I find now is that I can't seem to find the proper way to use it. I've tried this:

var xml = `<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
    <actors>
        <actor id="1">Christian Bale</actor>
        <actor id="2">Liam Neeson</actor>
        <actor id="3">Michael Caine</actor>
    </actors>
    <foo:singers>
        <foo:singer id="4">Tom Waits</foo:singer>
        <foo:singer id="5">B.B. King</foo:singer>
        <foo:singer id="6">Ray Charles</foo:singer>
    </foo:singers>
</root>`;
var actor = xpathParse("//actor[1]/text()").evaluate(xml);

But I'm getting this error:

n.clone is not a function

So looking at your test suite I tried this instead -same declaration of the xml variable:

const ExprContext = require('xslt-processor').ExprContext;
...
var actor = xpathParse("//actor[1]/text()").evaluate(
    new ExprContext(xmlParse(xml))
);

But I'm afraid ExprContext is not exposed by the module to be imported like this. It would be great if you could add a small snippet to README.md to describe how to use xpathParse.

johanneswilm commented 5 years ago

It would be great if you could add a small snippet to README.md to describe how to use xpathParse.

It's probably better if you figure out exactly all the things you want/need exposed, create a ticket and I'll expose them for you then. Even better if you could create a PR with all the things you want changed. @mig82

mig82 commented 5 years ago

Point taken. I'll do that.