Ryan
Ryan

Reputation: 175

How can I remove data type information from my xml in MarkLogic Javascript?

I have an XML data generated via Javascript in MarkLogic:

<Activities datatype="array">
	<Activity>
	  <ActivityCrewSize>10</ActivityCrewSize>
	  <ActivitySeqNo>1</ActivitySeqNo>
	  <ActivityDesc/>
	</Activity>
</Activities>

How can I remove the datatype information so it will just look like this:

<Activities>
	<Activity>
	  <ActivityCrewSize>10</ActivityCrewSize>
	  <ActivitySeqNo>1</ActivitySeqNo>
	  <ActivityDesc/>
	</Activity>
 </Activities>

obj.Activities = [];

let act = {
    '$type': 'Activity',
    '$version': '0.0.1',
     }	
	 
for (const item of activities) {

	act.ActivityCrewSize = fn.normalizeSpace(hl.elementText(item, "CrewSize", true)); 
	act.ActivitySeqNo = fn.normalizeSpace(hl.elementText(item, "SeqNo", true)); 
	act.ActivityDesc = hl.elementText(item, null, true);

	obj.Activities.push(act);	
}

return obj;

Upvotes: 1

Views: 52

Answers (3)

Ryan
Ryan

Reputation: 175

I was able to remove the data type information by converting the data using the Sequence.from function in MarkLogic. Hope this may help anyone in the future.

Upvotes: 0

Chris S.
Chris S.

Reputation: 1257

If you need it short and simple, this may satisfy your requirements.

var _xml = '<Activities datatype="array"><Activity><ActivityCrewSize>10</ActivityCrewSize<ActivitySeqNo>1</ActivitySeqNo><ActivityDesc/></Activity></Activities>';

var _test = _xml.replace(/\sdatatype\=\".+\"/g,'');
alert(_test);

This will wildcard-remove ANY occurance of ' datatype="(anything)"' from your XML-String. https://jsfiddle.net/53817ofj/

If you need it a bit more complex, you may want to take a look at https://www.w3schools.com/xml/xml_parser.asp

Upvotes: 0

NullPointer
NullPointer

Reputation: 7368

You can Remove an attribute by name.

xmlDoc.getElementsByTagName("Activities")[0].removeAttribute('datatype');
<Activities datatype="array">
	<Activity>
	  <ActivityCrewSize>10</ActivityCrewSize>
	  <ActivitySeqNo>1</ActivitySeqNo>
	  <ActivityDesc/>
	</Activity>
</Activities>

Upvotes: 1

Related Questions