Question

If I store lots of XML documents in riak, and then want to query it using MapReduce, how can I use XPath in my map function?

The examples show JSON:

var data = JSON.parse(v.values[0].data);

But I want to do something like:

var data = XML.selectNode("/root/XPath/to/my/node");

Is this possible, or is there a workaround (other than create a JSON duplicate copy of my XML objects) ?

Était-ce utile?

La solution

Riak's JS virtual machine doesn't have support for XML natively, but Erlang functions do via the standard library's "xmerl" application.

map_xpath(Object, _KeyData, XPath) ->
    %% Get the value out of the riak object
    {ok, Body} = riak_object:get_value(Object),
    %% Parse the XML contents into data structures
    {Doc, _} = xmerl_scan:string(unicode:characters_to_list(Body)),
    %% Extract the fragment via XPath
    xmerl_xpath:string(XPath, Doc).

This is pretty generic, you might want something more specific. Since the XPath query can, in some cases, return a scalar value instead of a list of nodes, one might want to wrap the return value in a list, or otherwise associate something with the query result and wrap that in a list (e.g. get the bucket/key off the object).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top