multiple schema files

This page explains a single execution of scalaxb that involves multiple schema files. For multiple runs of scalaxb, see multiple configs.

<xs:include> and <xs:import>

In XML Schema, schema components for a single target namespace can be assembled from multiple schema definition documents using <xs:include>. For example XML Schema translation of MathML 3's mathml3-content.xsd contains the following:

<xs:include schemaLocation="mathml3-strict-content.xsd"/> 

The above includes the file mathml3-strict-content.xsd into the schema as if it is part of it.

Also, in XML Schema, a schema definition document can reference schema components from other namespaces, and thereby other schema definition documents, using <xs:import>. For example, SAML Assertion schema imports two other schema documents:

<import namespace="http://www.w3.org/2000/09/xmldsig#"
    schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>
<import namespace="http://www.w3.org/2001/04/xmlenc#"
    schemaLocation="http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd"/>

This references two external namespaces using the given location.

compiling multiple schema

In order to generate data binding for schema documents containing either <xs:include> or <xs:import>, the referenced schema must be compiled together either explicitly or implicitly. Download all schema documents into a directory, and run scalaxb as follows:

$ scalaxb file1 file2 -p somepackagename

Or using sbt-scalaxb, place your xsd files in src/main/xsd and wsdl files in src/main/wsdl and add the following to build.sbt:

packageName in scalaxb in Compile := "somepackagename"

specifying package names

Suppose you would like to generate case class for different target namespace into different Scala package, you would do the following:

$ scalaxb -p:http://www.w3.org/2000/09/xmldsig#=xmldsig -p:http://www.w3.org/2001/04/xmlenc#=xmlenc -p:urn:oasis:names:tc:SAML:2.0:assertion=saml2 -p:urn:oasis:names:tc:SAML:2.0:metadata=saml2.metadata saml-schema-metadata-2.0.xsd saml-schema-assertion-2.0.xsd xenc-schema.xsd xmldsig-core-schema.xs

This generates schema components from http://www.w3.org/2000/09/xmldsig# into xmldsig package, and so on. Assigning different packages may be necessary, if two schemas defined complex types with the same name and you would like the names to be intact. For namespaces that were not found, it will use the default package name specified with -p option. Using sbt-scalaxb:

packageName in scalaxb in Compile := "somepackagename"

packageNames in scalaxb in Compile := Map(uri("http://www.w3.org/2000/09/xmldsig#") -> "xmldsig", ...)

searching of missing schema

When referenced schema documents are missing from the compilation, scalaxb will print warning messages:

$ scalaxb saml-schema-assertion-2.0.xsd -p saml
Warning: saml-schema-assertion-2.0.xsd imports http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd but no schema with that name was compiled together.
Warning: saml-schema-assertion-2.0.xsd imports http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd but no schema with that name was compiled together.

If unresolved, this will likely result to a compilation error because the referenced elements and types will not be found in the schema.

To assist the user, scalaxb looks for missing references in the current directory, but only in the current directory regardless of the URI or relative path specified in schemaLocation. (This feature will be available in 0.5.2)

$ scalaxb saml-schema-assertion-2.0.xsd -p saml
Warning: saml-schema-assertion-2.0.xsd imports http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd but no schema with that name was compiled together.
Warning: saml-schema-assertion-2.0.xsd imports http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd but no schema with that name was compiled together.
Warning: added xmldsig-core-schema.xsd to compilation.
Warning: added xenc-schema.xsd to compilation.
generated ./saml-schema-assertion-2.0.scala.
generated ./xmldsig-core-schema.scala.
generated ./xenc-schema.scala.
generated ./xmlprotocol.scala.
generated ./scalaxb.scala.