[英]How to replace a node in MarkLogic?
I need to replace the existing node with the new name using MarkLogic of an XML sheet. 我需要使用XML工作表的MarkLogic用新名称替换现有节点。
I don't use Query Console and I write all my code in .sjs file. 我不使用查询控制台,而是将所有代码写在.sjs文件中。 When I have gone through the steps given in MarkLogic docs, it is throwing some errors.
当我完成MarkLogic文档中给出的步骤后,它会抛出一些错误。
Sample code: 样例代码:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
I have to change tag "from" and replace it as "sender", ie. 我必须更改标签“ from”,并将其替换为“ sender”,即。 expected output:
预期输出:
<note>
<to>Tove</to>
<sender>Jani</sender>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Java Code: Java代码:
uploading sjs transform from java 从Java上传sjs转换
DatabaseClient client = DatabaseClientFactory.newClient(IP, 8000,
DATABASE_NAME, USERNAME, PWD, Authentication.DIGEST);
// get transform mgr
TransformExtensionsManager transMgr = client.newServerConfigManager()
.newTransformExtensionsManager();
FileInputStream transStream = null;
try {
transStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamHandle ipStreamHandle = new InputStreamHandle(transStream);
transMgr.writeJavascriptTransform(JS_TRANSFORM_NAME, ipStreamHandle);
client.release();
Applying the transform before reading the data from MarkLogic. 在从MarkLogic读取数据之前应用转换。 The actual data in database won't be updated
数据库中的实际数据不会被更新
ServerTransform transform = new ServerTransform(transformName);
DatabaseClient client = DatabaseClientFactory.newClient(IP, 8000,
DATABASE_NAME, USERNAME, PWD, Authentication.DIGEST);
JSONDocumentManager docMgr = clientNew.newJSONDocumentManager();
InputStreamHandle handle = new InputStreamHandle();
docMgr.read("/" + uri + JSON_EXT, handle, transform);
String document = handle.toString();
clientNew.release();
return document;
.sjs code: .sjs代码:
declareUpdate();
var n = new NodeBuilder();
node = n.addElement("sender", "Jani").toNode();
xdmp.nodeReplace(
cts.doc("/example.xml").xpath("/note/from"),
node
);
Error: 错误:
Operation not allowed on the currently executing transaction with identifier declareUpdate
First, let me note that you can use JavaScript in Query Console (just change the query type). 首先,请注意,您可以在查询控制台中使用JavaScript(只需更改查询类型)。 I assume you need to put your code into a file so that it will be part of your application, but running in Query Console is a good way to figure out what code you need.
我假设您需要将代码放入文件中,以便它将成为应用程序的一部分,但是在Query Console中运行是确定所需代码的好方法。
That said, the code you provided in the comment is a mix of XQuery and JavaScript. 也就是说,您在注释中提供的代码是XQuery和JavaScript的混合。 xdmp:document-insert() is XQuery -- you can tell by the case, JavaScript doesn't support identifiers with hyphens.
xdmp:document-insert()是XQuery -您可以看出这种情况,JavaScript不支持带连字符的标识符。 The xdmp:nodeReplace should be either xdmp:node-replace (XQuery) or xdmp.nodeReplace (JavaScript).
xdmp:nodeReplace应该是xdmp:node-replace(XQuery)或xdmp.nodeReplace(JavaScript)。
The problem you've run into is that XML is not valid content in a JavaScript file -- the < and > symbols are interpreted as less than and greater than. 您遇到的问题是XML不是JavaScript文件中的有效内容-<和>符号被解释为小于和大于。 You can accomplish what you're trying to do with code like this:
您可以使用以下代码完成您要尝试执行的操作:
declareUpdate();
var n = new NodeBuilder();
node = n.addElement("sender", "Jani").toNode();
xdmp.nodeReplace(
cts.doc("/example.xml").xpath("/note/from"),
node
);
Before going much further, I think you would benefit from reading the Server-side JavaScript introduction . 在继续之前,我认为您将受益于阅读服务器端JavaScript简介 。 That will give you a better foundation for what you're trying to accomplish.
这将为您要完成的工作提供更好的基础。
Edit: In light of additional information from the comments, the solution is that the JavaScript code you're using is intended to update the database, while you want to do a read transformation in memory ( Guidelines for Writing Transforms ). 编辑:根据注释中的其他信息,解决方案是您要使用JavaScript代码更新数据库,同时又要在内存中进行读取转换( 编写转换指南 )。 Based on Writing JavaScript Transformations , here's what you want instead:
根据编写JavaScript转换 ,下面是您想要的:
function fromToSender(context, params, content)
{
var doc = content.toObject();
delete doc.note.from;
doc.sender = 'Jani';
return doc;
};
exports.transform = fromToSender;
Note that this is a library module with a transform called "fromToSender". 请注意,这是一个带有名为“ fromToSender”的转换的库模块。 Adjust as needed.
根据需要进行调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.