Re: How to define a complexType so that derived types can have simple content or complex content?

Hi Roger,
    I gave this problem some more thought, and here's something we
could possibly do better with XSD 1.1 assertions.

XML instance:

<book>
    <title>The Implementation of Functional Programming Languages</title>
    <author>
       <Person>
           <name>Simon L. Peyton Jones</name>
       </Person>
    </author>
</book>

XSD solution 1:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="book">
         <xs:complexType>
         <xs:sequence>
       <xs:element name="title" type="book_component"/>
       <xs:element name="author" type="book_component"/>
  </xs:sequence>
  </xs:complexType>
    </xs:element>

  <xs:complexType name="book_component" mixed="true">
         <xs:sequence>
  <xs:element name="Person" minOccurs="0">
       <xs:complexType>
           <xs:sequence>
          <xs:element name="name" type="xs:string"/>
    </xs:sequence>
        </xs:complexType>
        </xs:element>
 </xs:sequence>
  <xs:assert test="if (name() = 'title') then (not(*) and
(string-length(string()) lt 100))
                                else
 
(Person[normalize-space(string-join(text(),'')) = ''])"/>
    </xs:complexType>

</xs:schema>

XSD solution 2:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

     <xs:element name="book">
           <xs:complexType>
          <xs:sequence>
        <xs:element name="title" type="book_component"/>
        <xs:element name="author" type="book_component"/>
  </xs:sequence>
   </xs:complexType>
     </xs:element>

  <xs:complexType name="book_component" mixed="true">
         <xs:complexContent>
      <xs:extension base="property">
                   <xs:sequence>
           <xs:element name="Person" minOccurs="0">
                <xs:complexType>
                       <xs:sequence>
                     <xs:element name="name" type="xs:string"/>
                </xs:sequence>
                </xs:complexType>
         </xs:element>
           </xs:sequence>
    <xs:assert test="if (name() = 'title') then (not(*) and
(string-length(string()) lt 100))
                                       else
 
(Person[normalize-space(string-join(text(),'')) = ''])"/>
      </xs:extension>
  </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="property" abstract="true"/>

</xs:schema>

Both of these solutions possibly achieve something like what you
specified i.e we associate two elements ("title" or "author") with the
same complex type, that works both as simple content and complex
content depending on the context element ("title" or "author" in this
case). This is achievable by declaring the complexType as "mixed" and
controlling the content with assertions (by conditional checks to the
context element).

Here's some pros and cons I can think of for above two solutions,
1) The second solutions achieves the abstraction you further wanted.
The type "book_component" inherits from an abstract type "property".
So one can know that, type "book_component" in this case is some
property (and this is a useful domain specific type annotation I
believe) by looking at ancestor type hierarchy of "book_component".
2) The first solution doesn't make "book_component" inherit from a
user defined abstract type and it's a standalone type. This is
functionally same as the definition of "book_component" specified in
the second solution. This may be a pros or a con wrt point 1 above.

Other than this XSD 1.1 solution, the 1.0 solution you proposed would
certain work in XSD 1.0 environment (but it seems you were not happy
with that, since it allows extra content due to the "mixed" content
model).

On Thu, Nov 3, 2011 at 1:35 AM, Costello, Roger L. <costello@mitre.org> wrote:
> Hi Folks,
>
> I would like to associate this title element with a complexType named property:
>
> <title>The Implementation of Functional Programming Languages</title>
>
> Notice that this "property element" has simple content.
>
> I would like to associate this author element with the same property complexType:
>
>    <author>
>        <Person>
>            <name>Simon L. Peyton Jones</name>
>        </Person>
>    </author>
>
> Observe that this property element has complex content.
>
> Thus, there are property elements with simple content and property elements with complex content.
>
> How do I define a property complexType such that it can be restricted to simpleContent and extended to complexContent?
>
> The following works, but I am not happy with it because it uses mixed="true" which I do not want. The <author> element has mixed content. Is there a way to implement this without requiring some property elements to have mixed content?   /Roger
>
>    <xs:complexType name="property" abstract="true" mixed="true" />
>
>    <xs:element name="title" type="titleType" />
>
>    <xs:complexType name="titleType">
>        <xs:simpleContent>
>            <xs:restriction base="property">
>                <xs:simpleType>
>                    <xs:restriction base="xs:string">
>                        <xs:maxLength value="100" />
>                    </xs:restriction>
>                </xs:simpleType>
>            </xs:restriction>
>        </xs:simpleContent>
>    </xs:complexType>
>
>    <xs:element name="author" type="authorType" />
>
>    <xs:complexType name="authorType" mixed="true">
>        <xs:complexContent>
>            <xs:extension base="property">
>                <xs:sequence>
>                    <xs:element ref="Person" />
>                </xs:sequence>
>            </xs:extension>
>        </xs:complexContent>
>    </xs:complexType>





-- 
Regards,
Mukul Gandhi

Received on Friday, 4 November 2011 03:59:24 UTC