W3C home > Mailing lists > Public > xmlschema-dev@w3.org > January 2009

Re: A schema that uses attribute values to enforce content structure rules?

From: Boris Kolpackov <boris@codesynthesis.com>
Date: Mon, 19 Jan 2009 08:24:18 +0200
To: Nathan Potter <ndp@coas.oregonstate.edu>
Cc: xmlschema-dev@w3.org
Message-ID: <boris.20090119081407@codesynthesis.com>

Hi Nathan,

Nathan Potter <ndp@coas.oregonstate.edu> writes:

> I am trying to express the semantics of our content model in an XML  
> schema. I have a type whose allowed content is function of the values  
> of an attribute. Here's a simplified "sketch" of the problem:
> An element <A> has a required attribute "type", if A@type=="xml" then  
> <A> may contain any (other namespace) XML:
>     <A type="xml">
>         <foo:B />
>         <bar:C />
>     </A>
> If A@type=="Container" then <A> may any number of other <A> elements:
>     <A type="Container">
> 	<A type="xml">
>             <foo:B />
>         </A>
> 	<A type="Container">
> 	    <A type="xml">
>                 <bar:C />
>             </A>
>         </A>
>     </A>
> If A@type=="values" then <A> must contain one or more <value> elements:
>     <A type="values">
>         <value>1234</value>
>     </A>

You can do this but you will need to use the special xsi:type attribute
instead of just type. You will need to define an abstract base type for 
element A:

<complexType name="base" abstract="true"/>

Then you would define three types, xml, container, and values, that 
derive from this type and define the corresponding content models.

You may also want to consider embedding the type information directly
into the element name, for example:

    <foo:B />
      <bar:C />

This gives you a much cleaner vocabulary. You can do this in an
extensible manner using substitution groups.


Boris Kolpackov, Code Synthesis Tools   http://codesynthesis.com/~boris/blog
Open source XML data binding for C++:   http://codesynthesis.com/products/xsd
Mobile/embedded validating XML parsing: http://codesynthesis.com/products/xsde
Received on Monday, 19 January 2009 06:25:51 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:50 UTC