Question
· Dec 3, 2024

Project objects to XML - flatten a property collection

I'm attempting to build a fairly complicated object graph, with nested objects and collections, in order to create a FHIR bundle.

In its most basic form there is a <bundle></bundle> element that represents the root, and there can be a number of nested <entry></entry> child elements.

I'm attempting to represent this as such:

class Bundle (%RegisteredObject, %XML.Adaptor)
{
    Property Entries As List Of Entry;
}

The problem is when this is projected it nests each Entry object within an Entries container, which makes sense but I don't want this. Is there a way to "flatten" or ignore the container? I can't find anything in the docs relating to this, nor any parameter values that would achieve it.

The XML should look like this instead:

<Bundle>
    <Entry>
        ...
    </Entry>
    <Entry>
        ...
    </Entry>
    <Entry>
        ...
    </Entry>
</Bundle>
Product version: HealthShare 2017.2
Discussion (7)3
Log in or sign up to continue
Class DC.Bundle Extends (%RegisteredObject, %XML.Adaptor)
{
Property Entries As list Of Entry(XMLNAME = "Entry", XMLPROJECTION = "ELEMENT");

ClassMethod Test()
{
	s bnd=..%New()
	s ent=##class(Entry).%New(),ent.Name="John",ent.Age=40 d bnd.Entries.Insert(ent) k ent
	s ent=##class(Entry).%New(),ent.Name="Paul",ent.Age=45 d bnd.Entries.Insert(ent) k ent
	
	d bnd.XMLExportToString(.xx) q xx
}
}

Class DC.Entry Extends (%RegisteredObject, %XML.Adaptor)
{
Property Name As %String;
Property Age As %Integer;
}

The test output is:


USER>set xml=##class(DC.Bundle).Test()

USER>write xml
<Bundle><Entry><Name>John</Name><Age>40</Age></Entry><Entry><Name>Paul</Name><Age>45</Age></Entry></Bundle>
USER>

USER>zzxs xml
<Bundle>
    <Entry>
        <Name>John</Name>
        <Age>40</Age>
    </Entry>
    <Entry>
        <Name>Paul</Name>
        <Age>45</Age>
    </Entry>
</Bundle>

USER>