[xsd-users] Cross file references

Boris Kolpackov boris at codesynthesis.com
Thu Aug 4 09:10:42 EDT 2011


Hi Till,

Till Steinbach <till.steinbach at informatik.haw-hamburg.de> writes:

> i have a question concerning references. I have a huge XSD schema of my meta
> model that was generated from an Ecore model. My instances are divided in
> multiple xml files each with its own root. There are references between the
> files.
> 
> <foo>
> 	<bar name="foobar"/>
> </foo>
> 
> <model>
> 	<referencing element refBar="foo.xml#//foo/bar[@name='foobar']">
> </model>

The "<referencing element" part above is not legal XML.


> Is there a way to use links to elements in other files with xsd? 
> I currently use xcerces as underlaying xml parser.

There are generally two approaches to handling this which can be
called "before schema" and "after schema".

With the "before schema" approach your XML Schema (and the resulting
C++ object model) doesn't know anything about references and expects
XML as a single, complete file (or stream). The way to handle this
with XSD is to first parse the XML file into DOM and then pre-process
it (by resolving all the references) before handing it off to the
object model parser.

The major drawback of this approach is that you cannot use XML Schema
validation since that happens during the XML-to-DOM parsing stage and
since your schema doesn't know anything about references, validation
will fail.

The "after schema" approach assumes that your schemas are aware of the
referencing mechanism. For example, the schema can look like this:

  <complexType name="bar">
    <sequence minOccurs="0">
      <element name="data" type="string"/>
    </sequence>
    <attribute name="refBar" type="string"/>
    <attribute name="name" type="string"/>
  </complexType>

  <complexType name="model">
    <sequence>
      <element name="bar" type="bar"/>
    </sequence>
  </complexType>

  <element name="model" type="model"/>

Which allows the instance to look like this:

  <model>
    <bar name="foobar">
      <data>ddd</data>
    </bar>
  </model>

Or like this:

  <model>
    <bar refBar="..."/>
  </model>

With this approach you can have XML Schema validation enabled. You can
also resolve the references in the DOM document before handing it off
to the object model or you can do it in the object model (since the
object model will contain the references). You can even customize the
generated object model (the model type in the above example) to load
the referenced elements automatically or even lazily.

Which approach to choose for reference resolution will probably be
influenced by the number of types that can have such references. If
the number of great then it may be too tedious to customize every
type and the generic DOM approach may be an easier solution.

Also your references look a lot like XPath so you can probably use
the XPath processor (either the one that comes with Xerces-C++ or
XQilla) to resolve them.

Boris




More information about the xsd-users mailing list