我正在使用DataHub来协调Entity
数据。
我正在尝试获取元素StatusHistory
的子元素。但是,使用下面的当前代码,仅显示该孩子的一个实例。 StatusHistory
当前每个孩子至少有5条以上的记录。
目标是添加/显示StatusHistory
下的所有子项。
下面是我当前在content.sjs中的代码
function createContent(id, options) {
let doc = cts.doc(id);
let root = doc.root.toObject();
let source;
// for xml we need to use xpath
if (root && xdmp.nodeKind(root) === 'element') {
source = root.xpath('/*:envelope/*:instance/node()');
}
// for json we need to return the instance
else if (root && root.envelope && root.envelope.instance) {
source = root.envelope.instance;
}
// for everything else
else {
source = doc;
}
return extractInstanceWorkOrder(source);
}
/**
* Creates an object instance from some source document.
* @param source A document or node that contains
* data for populating a Entity
* @return An object with extracted data and
* metadata about the instance.
*/
function extractInstanceWorkOrder(source) {
// the original source documents
let attachments = source;
obj = {
'$type': 'Entity',
'$version': '0.0.1',
'$attachments': null
}
obj.EntityID = fn.normalizeSpace(hl.elementText(source, "ID", true));
obj.Location = fn.normalizeSpace(hl.elementText(source, "Location", true));
obj.StatusHistory = getStatusHistory(obj.EntityID);
// return the instance object
return obj;
};
function getStatusHistory(entityId) {
let statusHistory = cts.search(cts.andQuery([
cts.collectionQuery("Collection1"),
cts.collectionQuery("Collection2"),
cts.elementWordQuery(xs.QName("DocumentID"), entityId),
]))
if(fn.count(statusHistory) <= 0) return "";
for (const item of statusHistory) {
let stat = {
'$type': 'StatusRecord',
'$version': '0.0.1',
}
stat.StatusID = fn.normalizeSpace(hl.elementText(item, "StatusID", true));
stat.BeginDate = fn.replace(fn.normalizeSpace(hl.elementText(item, "BeginDate", true)),"/","-");
stat.StatusFrom = fn.normalizeSpace(hl.elementText(item, "StatusFrom", true));
stat.StatusTo = fn.normalizeSpace(hl.elementText(item, "StatusTo", true));
stat.EndDate = fn.replace(fn.normalizeSpace(hl.elementText(item, "EndDate", true)),"/","-");
stat.DateChanged = fn.normalizeSpace(hl.elementText(item, "DateChanged", true));
}
return stat;
}
我还尝试了对getStatusHistory
函数的调用进行迭代并使用xdmp.nodeInsertChild
函数,但是它仍然无法正常工作:
function createContent(id, options) {
let doc = cts.doc(id);
let root = doc.root.toObject();
let source;
// for xml we need to use xpath
if (root && xdmp.nodeKind(root) === 'element') {
source = root.xpath('/*:envelope/*:instance/node()');
}
// for json we need to return the instance
else if (root && root.envelope && root.envelope.instance) {
source = root.envelope.instance;
}
// for everything else
else {
source = doc;
}
return extractInstanceWorkOrder(source);
}
/**
* Creates an object instance from some source document.
* @param source A document or node that contains
* data for populating a Entity
* @return An object with extracted data and
* metadata about the instance.
*/
function extractInstanceWorkOrder(source) {
// the original source documents
let attachments = source;
obj = {
'$type': 'Entity',
'$version': '0.0.1',
'$attachments': null
}
obj.EntityID = fn.normalizeSpace(hl.elementText(source, "ID", true));
obj.Location = fn.normalizeSpace(hl.elementText(source, "Location", true));
obj.StatusHistory = hl.elementText(source, "null", true);
let statusHistory = cts.search(cts.andQuery([
cts.collectionQuery("Collection1"),
cts.collectionQuery("Collection2"),
cts.elementWordQuery(xs.QName("DocumentID"), obj.EntityID),
]))
if(fn.count(statusHistory) <= 0) return "";
for (const item of statusHistory) {
xdmp.nodeInsertChild(obj.xpath("/*:Entity/*:Activities"),xdmp.unquote(getStatusHistory(item)));
}
// return the instance object
return obj;
};
function getStatusHistory(item) {
let stat = {
'$type': 'StatusRecord',
'$version': '0.0.1',
}
stat.StatusID = fn.normalizeSpace(hl.elementText(item, "StatusID", true));
stat.BeginDate = fn.replace(fn.normalizeSpace(hl.elementText(item, "BeginDate", true)),"/","-");
stat.StatusFrom = fn.normalizeSpace(hl.elementText(item, "StatusFrom", true));
stat.StatusTo = fn.normalizeSpace(hl.elementText(item, "StatusTo", true));
stat.EndDate = fn.replace(fn.normalizeSpace(hl.elementText(item, "EndDate", true)),"/","-");
stat.DateChanged = fn.normalizeSpace(hl.elementText(item, "DateChanged", true));
return stat;
}
如何为StatusHistory
显示元素Entity
的所有子元素?