1

Are there any Java libraries that allow schemas to be combined, when possible?

I'm creating an application where a user can link multiple "Generators" to a user defined ( Probably XML ) data set, each "Generator" will have a schema ( Probably XSD ), then the user can input data that is valid to that schema, it would be great if the XSDs can easibly be combined to one - so that the entire data set would match the combined XSD, matching every Generator's schema.

I understand XSDs can be in conflict, but this isn't a problem, but if this is completely impossible, what would be other solutions?

Philipp Gayret
  • 4,870
  • 1
  • 28
  • 34
  • I don't fully understand what you're trying to do, but you can combine schemas using `import`or `include`: http://stackoverflow.com/q/2357943/1374678 – rolve Nov 01 '12 at 09:32
  • With a special accent on **chameleon schemas**. These tend to be overlooked even in some major XSD projects, with disastrous consequences. – Marko Topolnik Nov 01 '12 at 09:36

1 Answers1

1

You can indeed have separate XSDs that are "imported" to other schemas. It is very simple, in the "composite" schema you simply need to use the "import:schemaLocation". See the following example from a code I have used in a project:

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test/interactions/AddToContext"
    xmlns:tns="http://test/interactions/AddToContext" xmlns:basictypes="http://test/interactions/BasicTypes" elementFormDefault="qualified">

    <import schemaLocation="BasicTypes.xsd"
        namespace="http://test/interactions/BasicTypes">
    </import>

    <!-- Request Message -->
    <element name="AddToContextRequest">
        <complexType>
            <sequence>
                <choice>
                    <element name="ServiceIdToAdd" type="string" maxOccurs="unbounded" />
                    <element name="ValueToAdd" type="basictypes:ContextValue" maxOccurs="unbounded" />
                </choice>
            </sequence>
        </complexType>
    </element>

    <!-- Response message -->
    <element name="AddToContextResponse">
        <complexType>
            <sequence>
                <element name="Result" type="string" />
            </sequence>
        </complexType>
    </element>
</schema>

You can see that I have imported the "BasicTypes" schema to this schema...

If you want to use this schema as a reference to generate Java code (Classes) to manipulate these objects, you can use the "xjc" (XML Binding Compiler)... It is very useful!

I hope this helps! Cheers!


Combining the basic types in a new XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test/interactions/AddToContext"
    xmlns:tns="http://test/interactions/AddToContext" elementFormDefault="qualified">

    <complexType name="ContextValue">
        <sequence>
            <element name="ValueID" type="string" />
            <choice>
                <sequence>
                    <element name="Value" type="string" />
                    <element name="ID" type="string" />
                </sequence>
                <element name="InfoValue" type="string" />
            </choice>
        </sequence> 
    </complexType>

    <!-- Request Message -->
    <element name="AddToContextRequest">
        <complexType>
            <sequence>
                <choice>
                    <element name="ServiceIdToAdd" type="string" maxOccurs="unbounded" />
                    <element name="ValueToAdd" type="tns:ContextValue" maxOccurs="unbounded" />
                </choice>
            </sequence>
        </complexType>
    </element>

    <!-- Response message -->
    <element name="AddToContextResponse">
        <complexType>
            <sequence>
                <element name="Result" type="string" />
            </sequence>
        </complexType>
    </element>
</schema>
emgsilva
  • 3,047
  • 1
  • 17
  • 16
  • Will this work without namespaces, by combining the structures of the xsds? See comment in the question. – Philipp Gayret Nov 01 '12 at 09:56
  • If I understand correctly, You can do that... See the edited answer (with combined XSD instead of import), where you simply copy the "basicTypes:ContextValue" ComplexType to the new XSD containing all the basic types you want to put together... – emgsilva Nov 01 '12 at 10:17