W3C home > Mailing lists > Public > xmlschema-dev@w3.org > August 2001

Re: open schema

From: Eddie Robertsson <eddie@allette.com.au>
Date: Tue, 28 Aug 2001 10:38:50 +1000
Message-ID: <3B8AE81A.F4E4FF4D@allette.com.au>
To: estee <estee@isdn.net.il>
CC: xmlschema-dev@w3.org
Hi estee,

> I hope you get the way i am thinking (I know it night be a bit bizaar)
> However, what i want is simple:
>         I want to validate that a certain parent element contains some mandatory
> fields. I do not however care what else it might contain.
>         Additionally, I do not care what is the order of all the elements in the
> parent. (some "garbage" elements can occur mixed between mandatory elements)
>         I do not even know the quantity of child elements I expect to occur

<snip/>

> <?xml version="1.0" encoding="UTF-8"?>
> <!--W3C Schema generated by XML Spy v4.0 beta 2 build Jul 26 2001
> (http://www.xmlspy.com)-->
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified">
>         <xs:element name="a">
>                 <xs:complexType>
>                         <xs:choice maxOccurs="unbounded">
>                                 <xs:element name="o" type='xs:string' minOccurs='1'/>
>                                 <xs:element name="d" type='xs:string' minOccurs='1'/>
>                                 <xs:any namespace = "hey"/>
>                         </xs:choice>
>                 </xs:complexType>
>         </xs:element>
> </xs:schema>

As Tom already have pointed out what you want isn't possible to do with W3C XML Schema. I
have too suggestions to your problem:

1) Move to a different schema dialect (I'm not sure if RELAX-NG will support what you try
to do but from what I can understand it's good at these things)

2) Include a Schematron constraint in your XML Schema that will handle your requirement
that all your mandatory elements must appear in your instance document.

Example (keep you schema model and add a Schematron constraint):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
 <xs:element name="a">
  <xs:annotation>
   <xs:appinfo>
    <pattern xmlns="http://www.ascc.net/xml/schematron" name="MandatoryElements">
     <rule context="a">
      <!-- This rule asserts that all the mandatory elements must appear atleast once.
-->
      <assert test="count(o) > 0">You must have atleast one o element.</assert>
      <assert test="count(d) > 0">You must have atleast one d element.</assert>
     </rule>
    </pattern>
   </xs:appinfo>
  </xs:annotation>
  <xs:complexType>
   <xs:choice maxOccurs="unbounded">
    <xs:element name="o" type='xs:string' minOccurs='1'/>
    <xs:element name="d" type='xs:string' minOccurs='1'/>
    <xs:any namespace = "hey"/>
   </xs:choice>
  </xs:complexType>
 </xs:element>
</xs:schema>

This way the Schematron rule will assert that you have atleast one of the mandatory
elements present in your instance document.
For more information about how to integrate XML Schema with Schematron see [1]
(ExtendingSchemas). The Schematron Validator from Topologi [2] will let you validate XML
Schemas with embedded Schematron information.

Cheers,
/Eddie

[1] http://www.xfront.com/BestPracticesHomepage.html
[2] www.topologi.com




>
>
> -----Original Message-----
> From: Eddie Robertsson [mailto:eddie@allette.com.au]
> Sent: Monday, August 27, 2001 3:37 AM
> To: estee
> Cc: xmlschema-dev@w3.org
> Subject: Re: open schema
>
> Hi estee,
>
> > Is there any possibility to define (in an xml schema) a type,
> > containing only simple types, of which some are required, and all the
> > others unknown.In other words: define a type in which i know what
> > elements are supposed to be present, but do not know which "might" be
> > present. eg:the following type must contain 'a', 'b', and 'c', but
> > might also contain some other elements. It is also important to state
> > that the order af appearance is of no siginificance.
> > <letters>        <a/>        <hollat/>        <c/>
> > <club/>        <b/>        <smit/>        <pliq/>    </letters> I am
> > searching for a solution in which all elements are of same namespaces.
>
> What you need is to include the xs:any [1] declaration in your content
> model. The xs:any declaration is used when you want to include arbitrary
> elements in your content model. It lets you control in which namespace
> the elements should be defined (using the namespace attribute) and how
> the validation should be performed (using the processContents
> attribute).
> In your example the declaration of letter would probably be something
> like:
>
> <xs:complexType name="letterType">
>    <xs:choice minOccurs="0" maxOccurs="unbounded">
>       <xs:element name="a" type="xs:string"/>
>       <xs:element name="b" type="xs:string"/>
>       <xs:element name="c" type="xs:string"/>
>       <xs:any namespace="##targetNamespace" processContents="strict"/>
>    </xs:choice>
> </xs:complexType>
>
> By setting namespace="##targetNamespace" you specify that all the
> elements must come from the targetNamespace of your schema.
>
>  Cheers,
> /Eddie
>
> [1] http://www.w3.org/TR/xmlschema-0/#any
Received on Monday, 27 August 2001 20:38:45 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:23 GMT