0

I have been fighting this for the past day and read countless other posts here on SO that all give the same advice for my problem. However, that advice is not working for me. I am using the xsd files for the CDA format from HL7 to try and generate my classes (unfortunately the files are too large to post here). Here are the relevant parts of the schema:

<xs:schema targetNamespace="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns="urn:hl7-org:v3" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="POCD_MT000040.ClinicalDocument" type="ClinicalDocument"/> <!-- error message points to the "<xs:element" on this line -->
<xs:complexType name="POCD_MT000040.ClinicalDocument">
    <xs:sequence>
        <xs:element name="realmCode" type="CS" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="typeId" type="POCD_MT000040.InfrastructureRoot.typeId"/>
        <xs:element name="templateId" type="II" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="id" type="II"/>
        <xs:element name="code" type="CE"/>
        <!-- many more elements follow -->
    </xs:sequence>
</xs:complexType>

I have found dozens of postings here on SO from people reporting similar messages. All of them say the problem is either that a default Namespace is missing or the complexType needs an accompanying "xs:element" declaration. As you can see I have both. So why is this happening?

I should add I have tried using XmlSpy to generate the classes from the xsd with it reporting the same error. However, my specific tools are from .NET. I have received the same error from Visual Studio, xsd.exe, and xml2code.

Any thoughts or ideas would be greatly appreciated!

Jeff Lehmer
  • 241
  • 1
  • 5
  • 17
  • 1
    Where is the `ClinicalDocument` type declared? – lexicore Jun 27 '18 at 15:08
  • 1
    Also there should normally be imports in the schema - otherwise unclear where the HL7 definitions should come from. – lexicore Jun 27 '18 at 15:09
  • As @lexicore indicates, we'll need to see a true [mcve] to help. – kjhughes Jun 27 '18 at 15:13
  • This is the declaration for ClinicalDocument. – Jeff Lehmer Jun 27 '18 at 15:27
  • The CS,II, and CE types are defined in another xsd using an include – Jeff Lehmer Jun 27 '18 at 15:29
  • As stated above, the files are way too big to post here. I am looking into another way I can provide them. – Jeff Lehmer Jun 27 '18 at 15:34
  • The top level file is CDA.xsd – Jeff Lehmer Jun 27 '18 at 15:51
  • I should add my ultimate goal is to generate C# classes from this description so I can (de)serialize xml files to objects. So I made one small change from the official HL7 version of these files. I moved the "cs" simple type from datatypes-base.xsd to voc.xsd to eliminate a circular reference. Most xml/xsd tools hate circular references. – Jeff Lehmer Jun 27 '18 at 15:55
  • 1
    You specifically should ***not*** post *all* of your files. **You have to prune your problem down to a *minimal* [mcve]** -- read that link for more information. Yes, it's a lot of work on your side, but it's your responsibility, not ours. Also, often OP's will find their problem on their own while simplifying/reducing it to a MCVE because reducing clutter improves their clarity too. – kjhughes Jun 27 '18 at 16:29
  • Post a sample of xml. Just need a couple of elements of each type. I've done a lot of work with both XML and HL7. You mention NET. Is this a c# project? HL7 should not have any circular types. Need to see the classes and xml. The error I believe is in the XML file and not the schema. Is the xsd tool producing the classes? Where did you xml file come from? Are you using the entire xml file? – jdweng Jun 27 '18 at 17:54
  • Using data binding technologies with a schema as large and complex as HL7 looks like a nightmare to me. Are you sure this is the right approach? – Michael Kay Jun 27 '18 at 18:01
  • @jdweng Yes it is a C# project. No there are no classes being generated yet. The error I am encountering is when I try to generate the classes from the .xsd. I do not have any .xml files yet. – Jeff Lehmer Jun 27 '18 at 18:19
  • @Michael Kay: Maybe that is the real problem here. I hope it is not true the tools are choking on this xsd. What other approach do I have other than a major effort to create my own classes? Using XmlSerializer would be a great "fast-forward" for our project. – Jeff Lehmer Jun 27 '18 at 18:23
  • @jdweng: What did you mean the "HL7 should not have any circular types"? Are you saying you have a version of the schema files that does not have a circular reference? We got our copy directly from HL7.org. – Jeff Lehmer Jun 27 '18 at 18:24
  • I believe I was able to satisfy @kjhughes request with the following. This is the absolute minimum amount of code to reproduce the problem. I'm in shock I was able to get it down this small. Hopefully this means the solution is simple. ` ` – Jeff Lehmer Jun 27 '18 at 20:10
  • @MarcusCole: Don't add markup in comments; [edit] your question and add it there, formatted as code. Be sure not to forfeit ***completeness*** for minimality. We should be able to verify the problem, so don't dangle type references (`ClinicalDocument` vs `POCD_MT000040.ClinicalDocument`: they're not the same.) – kjhughes Jun 27 '18 at 21:04
  • @MarcusCole Share your schemas in a GitHub project. – lexicore Jun 27 '18 at 21:14
  • @lexicore: No, please don't encourage that. It's against SO policy that Q's should stand on their own for the benefit of future readers. It also discourages creation of a MCVE, which benefits no one in the long run. – kjhughes Jun 27 '18 at 21:18
  • 1
    Thank you everyone for replying. I found the answer. @kjhughes suggestion of minifying down to the absolute minimum helped me eliminate all the noise and allowed me to focus on the real problem. I will edit the question with the solution. – Jeff Lehmer Jun 27 '18 at 21:18
  • 1
    @MarcusCole Don't edit your question to include the answer - just answer your question, it is fully OK. – lexicore Jun 27 '18 at 21:23
  • 1
    I got it to work. I downloaded from Hl7 the zip file cda_r2_normativewebedition2010.zip (the schema file). Then unzip. Open cmd.exe and went to folder : infrastructure\cda. Then ran : xsd /classes CDA.xsd – jdweng Jun 28 '18 at 03:55
  • 1
    I would be careful about making any changes to the schema. The schema is designed so you can either receive xml or generate xml. Making changes will not allow you to receive xml from other people. Yes there may be redundant structures in the schema but when serializing you have the option of which classes to fill. The empty ones (redundant) will not generate elements. You could display warning messages when unused classes contain data. – jdweng Jun 28 '18 at 10:05
  • I agree. I am loathe to modify any files I don't control. I also do not like the idea of modifying auto-generated files. I will look back at which folder from the HL7 zip file where I got the schemas that are causing me trouble. Thanks for your investigation. It is very valuable! – Jeff Lehmer Jun 29 '18 at 13:21

1 Answers1

3

SOLUTION: After cutting the code down to the absolute minimum (per request) it helped me to isolate the problem. The solution was to make the text in the 'type' on the xs:element declaration match the 'name' on the xs:complexType declaration. Making this change allowed the xsd.exe tool to generate the classes from the .xsd file.

<xs:element name="POCD_MT000040.ClinicalDocument" type="ClinicalDocument"/>
<xs:complexType name="ClinicalDocument"> <!-- took out "POCD_MT00040." -->
    <xs:sequence>
        <xs:element name="realmCode" type="CS" minOccurs="0" maxOccurs="unbounded"/>
        <!-- many more elements follow -->
    </xs:sequence>
</xs:complexType>
Jeff Lehmer
  • 241
  • 1
  • 5
  • 17