Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Import / Included schemas for code Generation #68

Open
markfarnan opened this issue Aug 11, 2018 · 10 comments
Open

Use Import / Included schemas for code Generation #68

markfarnan opened this issue Aug 11, 2018 · 10 comments

Comments

@markfarnan
Copy link

Currently it appears the XSDGEN tool does not reference and included/imported schema files.

There is an 'Import' function in the Parser code, but unclear how to use it.

Is there any documentation on how to Code-Gen an XSD Schema that uses extensive external imports ?
My use case is an open standard domain specific XML Schema, which has an extensive data model and uses about 20 different included files (With nested includes/imports).

I can share the entire schema if required as it is public.

@droyo
Copy link
Owner

droyo commented Aug 17, 2018

The current handling of imports is a bit of a hack. The xsdgen tool requires that you provide paths to all required files on the command line, including files imported by an XSD, and imports of imports and so on. #35 is open to have a utility to retrieve those files recursively.

So as far as I can tell, you should be able to generate code for your schema, you will just have to go through the extremely tedious process of acquiring and referencing all of the relevant XSD files. If that is feasible, you can try that and let me know if it doesn't work. If it does work we can mark this issue as a duplicate of #35.

@kortschak
Copy link
Contributor

kortschak commented Oct 9, 2018

I have a case where all the files necessary are available in a single directory. Handing xsdgen the paths to all of the xsd files fails to result in a successful codegen; doing schema surgery and manually inlining the includes in the root does give a successful result.

$ xsdgen -v *.xsd
complexType parent: could not find type id-type in namespace http://www.gexf.net/1.2draft for attribute for

Am I missing something here? or is this not expected to work?

@droyo
Copy link
Owner

droyo commented Oct 13, 2018

This is expected to work. Looking at your example I see "id-type" defined in gexf.xsd, under targetNamespace "http://www.gexf.net/1.2draft", defined here. It's then used in data.xsd.

I believe what is happening is that the same targetNamespace is split across multiple files. The way xsd.Parse works, it expects for a targetNamespace to be fully defined within a single file. In short, I'm not properly handling the <include> statement: https://www.w3.org/TR/xmlschema-1/#compound-schema

There is a step during the xsd parsing process, resolvePartialTypes, that looks up types like id-type. I think the way forward is to pull this step out of the per-document parsing, and defer it to the very end, after all documents have been parsed.

@aaronmmanzano
Copy link

I believe I've handled the splitting of namespaces across files in my PR. I'm sure it needs work, and you may prefer it be handled in another manner else where within the logic, but it may be a start.

@davidalpert
Copy link
Contributor

this is an older issue but I ran into the same problem. I took a crack at resolving it on a local branch by:

  • added an -f flag to the xsdgen CLI app to toggle on following schemas;
  • updated the read file logic so that when the follow schema flag is set it will call xsd.Imports(..) on each file as it's read, recursively reading in those files (and their imports) so that all the imported schema files are all in memory;
  • added a MergeTypes() method to the xsd.Schema type to merge types so long as the target namespaces are equivalent;
  • updated the parse logic to merge schema types by target namespace rather than overwrite them, allowing all the imported types from a given target namespace to collect into a single xsd.Schema object;

this seems to work, as it satisfies the assumptions of the resolvePartialTypes method, that all type resolution can happen within a single xsd.Schema object.

if this approach seems reasonable I am happy to offer these changes as a pull request for consideration.

@davidalpert
Copy link
Contributor

I didn't find the PR that @aaronmmanzano mentioned until this morning, I assume that #72 is it? I'm going to have a look through to catch up on what I missed in the conversation and see how I can help.

@davidalpert
Copy link
Contributor

PR #72 has some merge conflicts with master in parse.go which are complex enough that I don't want to get it wrong.

In the meantime, here are a pair of PRs, separated as I think they serve different purposes, for consideration:

@droyo
Copy link
Owner

droyo commented Mar 13, 2021

I merged #131 but I am doing some research and apparently it is valid to have import cycles. Or, at least, it is not explicitly invalid. I have not worked with a wide range of XSD files; does anyone have an idea how common circular imports are? #131 will have an infinite loop on such input.

@nikolawannabe
Copy link

I also ran into this issue when generating code for Onix 3.0.

For me, I had to manually merge the 3 files for the generation to succeed.

@dalcorta
Copy link

dalcorta commented Jan 28, 2024

I suggest using fixtrading.org XSD since it is very complex.
I downloaded it from https://www.fixtrading.org/standards/fixml/
fixmlschema_FIX.Latest_EP286.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants