W3C home > Mailing lists > Public > xmlschema-dev@w3.org > May 2011

Re: Question about checking dependencies between fields in xml document via xsd

From: C. M. Sperberg-McQueen <cmsmcq@blackmesatech.com>
Date: Tue, 17 May 2011 09:11:40 -0600
Cc: "C. M. Sperberg-McQueen" <cmsmcq@blackmesatech.com>, <xmlschema-dev@w3.org>
Message-Id: <B0E2BA14-3CC8-49DB-BD66-5DA761582030@blackmesatech.com>
To: Ионов Дмитрий Петрович <Ionov@ndc.ru>
On May 12, 2011, at 12:49 PM, Ионов Дмитрий Петрович wrote:

> >On 11/05/2011 17:25, Ионов Дмитрий Петрович wrote:
> ...  doc_body can be in 3 variants
> 1. see upper (in xml document sample)

[i.e. group 1 and group 2 both present]

> 2.
> <doc_body>
> <some_field1>sf1</some_field1>
> <some_field2>sf2</some_field2>
> <some_field3>sf3</some_field3>
> <items>
> <item>
> <item_c>item_</item_c>
> <item_q>1</item_q>
> </item>
> </items>
>  
> <field1></field1>
> <field2></field2>
> <field3></field3>
>  
> </doc_body>

i.e. group 1 present, group 2 absent.

> 
> 3.
> <doc_body>
> <some_field1>sf1</some_field1>
> <some_field2>sf2</some_field2>
> <some_field3>sf3</some_field3>
> <items>
> <item>
> <item_c>item_</item_c>
> <item_q>1</item_q>
> </item>
> </items>
>  
> <field4></field4>
> <field5></field5>
>  
> </doc_body>

i.e. group 1 absent, group 2 present.
> 
>  
> How I can change XSD for check all variants of doc_body ?
>  

This is an instance of a general pattern:  for some A and B,
where A and B are elements, or sequences of elements, or
some other distinctive patterns, you want exactly one of:

  A
  B
  AB

The simplest way to say what you want would be to write it
as a three-way choice, as (A | B | AB), or in XSD notation

  <choice>
    <sequence> ... A ... </sequence>
    <sequence> ... B ... </sequence>
    <sequence> ... A, B ... </sequence>
  </choice>

But that violates the 'Unique Particle Attribution Constraint':  
when the validator sees an A, it won't know which of the two 
As in the content model is being matched, and XSD doesn't
allow that.

So notice first that (A | AB) accepts the same sequences as
(AB?):  one A followed by an optional B.  So (A | B | AB) =
(A | AB | B) = (AB? | B).  Or in XSD:

  <choice>
    <sequence>
      ... A ...
      <sequence minOccurs="0">
        ... B ... 
      </sequence>
    </sequence>
    <sequence>
        ... B ...
    </sequence>
  </choice>

Replacing A with a sequence of f1, f2, f3 and B with a 
sequence of 4 and f5, this gives:

  <choice>
      <sequence>
          <element name="f1/>
          <element name="f2/>
          <element name="f3/>
          <sequence minOccurs="0">
              <element name="f4/>
              <element name="f5/>
          </sequence>
      </sequence>
      <sequence>
          <element name="f4/>
          <element name="f5/>
      </sequence>
  </choice>

I hope this helps.

-- 
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com 
* http://cmsmcq.com/mib                 
* http://balisage.net
****************************************************************
Received on Tuesday, 17 May 2011 15:12:09 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 17 May 2011 15:12:09 GMT