Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating C# classes from FHIR schemas

Tags:

c#

xsd

hl7-fhir

I am attempting to generate c# classes from schemas provided by the FHIR project: http://hl7.org/implement/standards/fhir/ I have downloaded the schemas: http://hl7.org/documentcenter/public/standards/FHIR/fhir-all-xsd.zip I have "Unblocked" the zip file and unzipped the xsd files into a folder. While attempting to use xsd.exe to create c# classes, I keep getting errors that indicate an issue with the schemas. Consistently getting xhtml:div element is not declared in addition to others. The file fhir-all.xsd seems to list the top level objects. I was able to get the simple schema tombstone.xsd to work with xsd.exe, but a more complex item like valueset.xsd or alert.xsd fails miserably. I can't see what is wrong with these files. Any help on how to fix these schemas will be appreciated.

like image 455
jlo-gmail Avatar asked Jan 11 '23 04:01

jlo-gmail


2 Answers

Generating POCO's from the XSD's will give less-then-optimal classes however. Since FHIR's serialization avoids the use of polymorhism, elements that present a choice (e.g. Observation.value) will be represented in the XSD as sets of elements with identical names (valueNumber, valueString, valueCodeableConcept etc. etc).

As well, it's pretty hard to use the same POCO's for json serialization.

In the .NET NuGet package for FHIR, you'll find a set of generated classes for the FHIR Resources, which are as light-weight as I could make them. In addition, there are Validation attributes to validate their contents, the package contains serializers and parsers for json and xsd and a REST client to invoke the rest operations on a server.

If you need to integrate the parsers and serializers with WebAPI, I have posted about that here: HL7 FHIR serialisation to json in asp.net web api

like image 156
Ewout Kramer Avatar answered Jan 21 '23 20:01

Ewout Kramer


So far I have been able to generate classes and de-serialize many of the patient*.xml samples provided to both raw classes generated as described, and classes generated from the raw classes by a SOAP service.

Editing xhtml1-strict.xsd to fix this issue is not that simple. I used xsd.exe to attempt to create classes from the file, then used the error messages as starting points. After some experimentation, I cam up with this file. It addresses the problem with the div element, as long the HTML contained is kept simple. I am sharing the difference report for others to make use of. The numbers represent line-number. (I am just sharing the changes because of size limitations, I tried to share the whole file).

XSD\xhtml1-strict.xsd(413):      <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(441):      <!--<xs:element ref="pre"/>-->
XSD\xhtml1-strict.xsd(443):      <!--<xs:element ref="blockquote"/>-->
XSD\xhtml1-strict.xsd(462):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(519):      <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(520):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(539):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1349):        <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1351):        <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(1352):        <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1450):          <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1452):          <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1718):          <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1720):          <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(1721):          <!--<xs:group ref="misc"/>-->

I am also sharing my notes so far regarding manual edits required, to fix issues in the classes generated.

Generate entities with Xsd2Code add-in from www.codeplex.com\Xsd2Code

Use fhir-atom-single.xsd as the source XSD
Use Parms:
    Serilization.GenerateXMLAttributes = true
    Code.Namespace = Hl7.Fhir.Validation.SchematronOutput
    Collection.CollectionObjectType=Array

!!! Do not open Schema in Designer, or classes will change.

Manual updates:

    public partial class boolean : Element
    ...
        [System.Xml.Serialization.XmlAttributeAttribute("value")]
        public bool Value
        {


    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false, Namespace = "http://www.w3.org/1999/xhtml")]
    public partial class div : Flow


    Refactor:
    public partial class FeedType
    to
    public partial class feed
like image 22
jlo-gmail Avatar answered Jan 21 '23 20:01

jlo-gmail