1

我正在使用 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

4

0 回答 0