I think that interface{} type would be useful in such case var xml []interface{}
You can make convenience accessor functions like func Names() []Names {} On Sunday, March 29, 2020 at 9:33:49 PM UTC+2, DrGo wrote: > > Hi, > wondering how to unmarshall the xml below (also available here > https://play.golang.org/p/QtZwKmrn8HM) into a tree. The challenge is that > each element can nest 0 or more *ordered* child elements which can be of > different types (eg text, group, choose, macro etc). I tried unmarshalling > into a struct which embed the following type > > type RenderingElement struct { > Names []Names `xml:"names"` > Date []Date `xml:"date"` > Label []Label `xml:"label"` > Text []Text `xml:"text"` > Number []Number `xml:"number"` > Choose []Choose `xml:"choose"` > Group []Group `xml:"group"` > } > > This is simple but the problem is that if an element has children of > different types eg text and group I have no way of telling their order and > the order is important (essentially the xml codes for a formatting program). > > Any tips or example code on how to unmarshall this xml while retaining its > nested ordered structure? > > Thanks, > > const src = `<layout suffix="."> > <text variable="citation-number" suffix=". "/> > <group delimiter=", "> > <text macro="author"/> > <choose> > <if type="thesis"> > <group delimiter=" "> > <group delimiter=", "> > <group> > <!-- Always print, even if no university given --> > <text value="thesis"/> > </group> > <text macro="publisher"/> > </group> > <text macro="issued" prefix="(" suffix=")"/> > </group> > </if> > <else-if type="bill book graphic legal_case legislation motion_picture > song chapter paper-conference" match="any"> > <group delimiter=" "> > <choose> > <if type="chapter paper-conference" match="any"> > <group delimiter=", "> > <group delimiter=" "> > <text term="in"/> > <text variable="container-title" font-style="italic"/> > </group> > <text macro="editor"/> > </group> > </if> > <else> > <text macro="title"/> > </else> > </choose> > <group prefix="(" suffix=")" delimiter="; "> > <group delimiter=", "> > <text macro="publisher"/> > <text macro="edition"/> > <text macro="issued"/> > </group> > <text variable="URL"/> > </group> > </group> > <group delimiter=" of "> > <group> > <label variable="volume" form="short" suffix=" "/> > <number variable="volume"/> > </group> > <text variable="collection-title" font-style="italic"/> > </group> > <choose> > <if type="chapter paper-conference" match="any"> > <text macro="pages"/> > </if> > </choose> > </else-if> > <else-if type="article-journal"> > <choose> > <if variable="page"> > <choose> > <if is-numeric="page" match="none"> > <group> > <group delimiter=", "> > <text variable="container-title" form="short" font-style="italic"/> > <group> > <text term="in press"/> > </group> > </group> > <text macro="access"> hello </text> > </group> > </if> > <else> > <text macro="article-details"/> > </else> > </choose> > </if> > <else> > <text macro="article-details"/> > </else> > </choose> > </else-if> > <else-if type="report"> > <group> > <group delimiter=", "> > <text variable="title" quotes="true"/> > <text variable="collection-title" font-style="italic"/> > </group> > <group prefix=" (" suffix=")" delimiter=", "> > <group delimiter=" "> > <text variable="genre" form="short"/> > <number variable="number"/> > </group> > <text variable="publisher"/> > <text variable="publisher-place"/> > <text macro="issued"/> > </group> > </group> > <text macro="pages"/> > <text macro="access"/> > </else-if> > <else> > <group> > <group delimiter=", "> > <text macro="editor"/> > <group delimiter=". "> > <text macro="title"/> > <text form="short" variable="container-title" font-style="italic"/> > <text variable="volume" font-weight="bold"/> > </group> > </group> > <text macro="issued" prefix=" (" suffix=")"/> > </group> > <text macro="pages"/> > <text macro="access"/> > </else> > </choose> > </group> > </layout> > ` > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6897e30d-e293-45c0-b968-313a54d99e0c%40googlegroups.com.