複数のスキーマファイル

<xs:include> and <xs:import>

XML Schema では、<xs:include> を用いることで、一つの対象名前空間のためのスキーマを複数のスキーマ定義ドキュメントを組み合わせて定義することができる。例えば、MathML 3 のスキーマの XML Schema 版は以下を含む:

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

これは、mathml3-strict-content.xsd をあたかもスキーマの一部であるかのように取り込む。

また、XML Schema では、スキーマ定義ドキュメントは <xs:import> を用いることで他の名前空間のスキーマを参照することができ、つまり他のスキーマ定義ドキュメントを参照することができる。例えば、SAML Assertion のスキーマは二つの外部スキーマ定義をインポートする:

<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"/>

これは、スキーマ定義ドキュメントが指定された場所にある二つの対象名前空間を参照する。

複数のスキーマのコンパイル

<xs:include><xs:import> を含むスキーマドキュメントからデータバインディングを生成するには、参照されるスキーマも何らかの方法で一緒にコンパイルしなければならない。全てのスキーマドキュメントを一つのディレクトリにダウンロードして以下のように scalaxb を走らせるという方法を推奨する:

$ scalaxb file1 file2 -p somepackagename

sbt-scalaxb を用いる場合は、xsd ファイルを src/main/xsd 内に、wsdl ファイルを src/main/wsdl 入れ、以下を build.sbt に追加する:

packageName in scalaxb in Compile := "somepackagename"

パッケージ名の指定

もし、異なる対象名前空間ごとに case class を別の Scala パッケージに生成したい場合は、以下のようにする:

$ 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

これは、http://www.w3.org/2000/09/xmldsig# 内のコンポーネントは xmldsig パッケージに生成される。二つの異なるスキーマが同名の複合型を定義し、そのままの名前を使いたい場合などは名前空間ごとにパッケージ名を指定する必要がある。指定されなかった名前空間は、-p オプションで指定されたデフォルトのパッケージ名が使われる。sbt-scalaxb を使う場合は:

packageName in scalaxb in Compile := "somepackagename"

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

見つからないスキーマの検索

参照されたスキーマドキュメントがコンパイルから見つからない場合は、scalaxb は警告を表示する:

$ 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.

解決されない場合は、参照される要素や型が見つからずコンパイルエラーになる可能性が高い。

ユーザの補助として、scalaxb は見つからない参照を現ディレクトリ内から検索しようとする。ただし、 schemaLocation で指定された URI や相対パスに関わらず、現ディレクトリしか検索しないことに注意。(この機能は 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.