Restricting attribute use from optional to required

View: New views
9 Messages — Rating Filter:   Alert me  

Restricting attribute use from optional to required

by Jan Přidal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,
my question follows, I am using XML Schema 1.0.

I have an attributeGroup with an optional attribute. This
attributeGroup is used in declaration of complexType. Another
complexType is then an extension of that complexType. Please see my
sample code below.

I would like to declare the attribute 'label' as required for the
'derived' complexType. Is there any legal way to do it?

<xsd:attributeGroup name="attributes">
    ...
    <xsd:attribute name="label" type="xsd:string" />
</xsd:attributeGroup>

<xsd:complexType name="parent">
    <xsd:complexContent>
        ...
        <xsd:attributeGroup ref="attributes"/>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="derived">
    <xsd:complexContent>
        <xsd:extension base="parent">
            <xsd:sequence>
                <xsd:element ref="parameter" minOccurs="0"
maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Regards,
Jan Pridal



RE: Restricting attribute use from optional to required

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You can do this, but only in an <xs:restriction>, not in an <xs:extension>

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

> -----Original Message-----
> From: xmlschema-dev-request@...
> [mailto:xmlschema-dev-request@...] On Behalf Of Jan Pridal
> Sent: 14 August 2009 13:42
> To: xmlschema-dev@...
> Subject: Restricting attribute use from optional to required
>
> Hello all,
> my question follows, I am using XML Schema 1.0.
>
> I have an attributeGroup with an optional attribute. This
> attributeGroup is used in declaration of complexType. Another
> complexType is then an extension of that complexType. Please
> see my sample code below.
>
> I would like to declare the attribute 'label' as required for
> the 'derived' complexType. Is there any legal way to do it?
>
> <xsd:attributeGroup name="attributes">
>     ...
>     <xsd:attribute name="label" type="xsd:string" />
> </xsd:attributeGroup>
>
> <xsd:complexType name="parent">
>     <xsd:complexContent>
>         ...
>         <xsd:attributeGroup ref="attributes"/>
>     </xsd:complexContent>
> </xsd:complexType>
>
> <xsd:complexType name="derived">
>     <xsd:complexContent>
>         <xsd:extension base="parent">
>             <xsd:sequence>
>                 <xsd:element ref="parameter" minOccurs="0"
> maxOccurs="unbounded"/>
>             </xsd:sequence>
>             <xsd:attribute name="name" type="xsd:string"
> use="required"/>
>         </xsd:extension>
>     </xsd:complexContent>
> </xsd:complexType>
>
> Regards,
> Jan Pridal
>
>



Re: Restricting attribute use from optional to required

by Jan Přidal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I feared it was so :-)

But it is then more than a little bit awkward - as I have to add a new
complexType that restricts the complexType 'parent' and then within it
I have to repeat all the stuff declared the 'parent' and its parent
types (by extension) etc., and eventually I have to use the new
complexType as a base of 'derived' complextType. Also I do not like
the section in the new complexType declaration where I have to list
all attributes of the attributeGroup one by one.

<xsd:attributeGroup name="attributes">
  ...
  <xsd:attribute name="label" type="xsd:string" />
</xsd:attributeGroup>

<xsd:complexType name="parent">
  <xsd:complexContent>
      ...
      <xsd:attributeGroup ref="attributes"/>
  </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="parentWithRequiredLabelAttribute">
   <xsd:complexContent>
       <xsd:restriction base="parent">
           <xsd:sequence>
               ... list all elements of 'parent' type etc.
           </xsd:sequence>
           ...
           <xsd:attribute name="label" type="xsd:string" use="required"/>
           ...
           ... and list all remaining attributes from attributeGroup
called ''attributes"
           ...
       </xsd:restriction>
   </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="derived">
  <xsd:complexContent>
      <xsd:extension base="parentWithRequiredLabelAttribute">
          <xsd:sequence>
              <xsd:element ref="parameter" minOccurs="0" maxOccurs="unbounded"/>
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required"/>
      </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

Or am I missing something and it could be done better?

Thank you for all your help,
-Jan Pridal

2009/8/16 Michael Kay <mike@...>:

>
> You can do this, but only in an <xs:restriction>, not in an <xs:extension>
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay
>
>> -----Original Message-----
>> From: xmlschema-dev-request@...
>> [mailto:xmlschema-dev-request@...] On Behalf Of Jan Pridal
>> Sent: 14 August 2009 13:42
>> To: xmlschema-dev@...
>> Subject: Restricting attribute use from optional to required
>>
>> Hello all,
>> my question follows, I am using XML Schema 1.0.
>>
>> I have an attributeGroup with an optional attribute. This
>> attributeGroup is used in declaration of complexType. Another
>> complexType is then an extension of that complexType. Please
>> see my sample code below.
>>
>> I would like to declare the attribute 'label' as required for
>> the 'derived' complexType. Is there any legal way to do it?
>>
>> <xsd:attributeGroup name="attributes">
>>     ...
>>     <xsd:attribute name="label" type="xsd:string" />
>> </xsd:attributeGroup>
>>
>> <xsd:complexType name="parent">
>>     <xsd:complexContent>
>>         ...
>>         <xsd:attributeGroup ref="attributes"/>
>>     </xsd:complexContent>
>> </xsd:complexType>
>>
>> <xsd:complexType name="derived">
>>     <xsd:complexContent>
>>         <xsd:extension base="parent">
>>             <xsd:sequence>
>>                 <xsd:element ref="parameter" minOccurs="0"
>> maxOccurs="unbounded"/>
>>             </xsd:sequence>
>>             <xsd:attribute name="name" type="xsd:string"
>> use="required"/>
>>         </xsd:extension>
>>     </xsd:complexContent>
>> </xsd:complexType>
>>
>> Regards,
>> Jan Pridal
>>
>>
>
>



RE: Restricting attribute use from optional to required

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> But it is then more than a little bit awkward - as I have to
> add a new complexType that restricts the complexType 'parent'
> and then within it I have to repeat all the stuff declared
> the 'parent' and its parent types (by extension) etc.

Yes, that's a particularly nasty feature of derivation by restriction.

If you're only restricting the attributes, and not the content model, you
can get around it by having both the base type and the derived type
reference the same named model group.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 



Re: Restricting attribute use from optional to required

by Jan Přidal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OK, so let's say we get rid off this repeating of content model
declaration using named model group, see below.
There's still one place I'd like to fix - the enumeration of all
attributes from 'attributes' attributeGroup inside the
'parentWithReqLabelAttr' type declaration.

<xsd:attributeGroup name="attributes">
  <xsd:attribute name="label" type="xsd:string" />
  ...
</xsd:attributeGroup>

<xsd:group name="commonElements">
  <xsd:sequence>
      ...
  </xsd:sequence>
</xsd:group>

<xsd:complexType name="parent">
  <xsd:group ref="commonElements"/>
  <xsd:attributeGroup ref="attributes"/>
</xsd:complexType>

<xsd:complexType name="parentWithReqLabelAttr">
  <xsd:complexContent>
    <xsd:restriction base="parent">
      <xsd:group ref="commonElements"/>
      <xsd:attribute name="label" type="xsd:string" use="required"/>
      ... list all remaining attributes from 'attributes' group!!!
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="derivedWithOptLabelAttr">
  <xsd:complexContent>
    <xsd:extension base="parent">
      <xsd:sequence>
        <xsd:element ref="style"/>
      </xsd:sequence>
      <xsd:attribute name="icon" type="xsd:string" use="optional"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="derivedWithReqLabelAttr">
  <xsd:complexContent>
    <xsd:extension base="parentWithReqLabelAttr">
      <xsd:sequence>
        <xsd:element ref="parameter" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
      <xsd:attribute name="name" type="xsd:string" use="required"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

I'd like to have there something like a restriction of attributeGroup,
either local or a global inside the type 'parentWithReqLabelAttr':

<xsd:attributeGroup name="attributesWithReqLabelAttr">
  <xsd:attributeGroup ref="attributes">
    <xsd:attribute ref="label" use="required"/>
  </xsd:attributeGroup>
</xsd:attributeGroup>

If this can be achieved then I would be able to add a new attribute to
the 'attributes' attributeGroup without changing the
'parentWithReqLabelAttr' type. However as far as I know there's no
such mechanism.

Jan Pridal

2009/8/17 Michael Kay <mike@...>:

>> But it is then more than a little bit awkward - as I have to
>> add a new complexType that restricts the complexType 'parent'
>> and then within it I have to repeat all the stuff declared
>> the 'parent' and its parent types (by extension) etc.
>
> Yes, that's a particularly nasty feature of derivation by restriction.
>
> If you're only restricting the attributes, and not the content model, you
> can get around it by having both the base type and the derived type
> reference the same named model group.
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay
>
>


RE: Restricting attribute use from optional to required

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


There's an assymetry here between attributes and child elements. For the
content model (child elements), when you restrict a type you have to restate
all the parts of the content model that you want to inherit. For attributes,
you only have to list the things that have changed - any other attributes
are inherited automatically.

(No, I can't justify why it was designed that way. Like most things in XSD,
it was probably because there were too many bright people on the committee
and each of them got their way on one feature of the language.)

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


> -----Original Message-----
> From: xmlschema-dev-request@...
> [mailto:xmlschema-dev-request@...] On Behalf Of Jan Pridal
> Sent: 26 August 2009 08:23
> To: xmlschema-dev@...
> Subject: Re: Restricting attribute use from optional to required
>
> OK, so let's say we get rid off this repeating of content
> model declaration using named model group, see below.
> There's still one place I'd like to fix - the enumeration of
> all attributes from 'attributes' attributeGroup inside the
> 'parentWithReqLabelAttr' type declaration.
>
> <xsd:attributeGroup name="attributes">
>   <xsd:attribute name="label" type="xsd:string" />
>   ...
> </xsd:attributeGroup>
>
> <xsd:group name="commonElements">
>   <xsd:sequence>
>       ...
>   </xsd:sequence>
> </xsd:group>
>
> <xsd:complexType name="parent">
>   <xsd:group ref="commonElements"/>
>   <xsd:attributeGroup ref="attributes"/> </xsd:complexType>
>
> <xsd:complexType name="parentWithReqLabelAttr">
>   <xsd:complexContent>
>     <xsd:restriction base="parent">
>       <xsd:group ref="commonElements"/>
>       <xsd:attribute name="label" type="xsd:string" use="required"/>
>       ... list all remaining attributes from 'attributes' group!!!
>     </xsd:restriction>
>   </xsd:complexContent>
> </xsd:complexType>
>
> <xsd:complexType name="derivedWithOptLabelAttr">
>   <xsd:complexContent>
>     <xsd:extension base="parent">
>       <xsd:sequence>
>         <xsd:element ref="style"/>
>       </xsd:sequence>
>       <xsd:attribute name="icon" type="xsd:string" use="optional"/>
>     </xsd:extension>
>   </xsd:complexContent>
> </xsd:complexType>
>
> <xsd:complexType name="derivedWithReqLabelAttr">
>   <xsd:complexContent>
>     <xsd:extension base="parentWithReqLabelAttr">
>       <xsd:sequence>
>         <xsd:element ref="parameter" minOccurs="0"
> maxOccurs="unbounded"/>
>       </xsd:sequence>
>       <xsd:attribute name="name" type="xsd:string" use="required"/>
>     </xsd:extension>
>   </xsd:complexContent>
> </xsd:complexType>
>
> I'd like to have there something like a restriction of
> attributeGroup, either local or a global inside the type
> 'parentWithReqLabelAttr':
>
> <xsd:attributeGroup name="attributesWithReqLabelAttr">
>   <xsd:attributeGroup ref="attributes">
>     <xsd:attribute ref="label" use="required"/>
>   </xsd:attributeGroup>
> </xsd:attributeGroup>
>
> If this can be achieved then I would be able to add a new
> attribute to the 'attributes' attributeGroup without changing
> the 'parentWithReqLabelAttr' type. However as far as I know
> there's no such mechanism.
>
> Jan Pridal
>
> 2009/8/17 Michael Kay <mike@...>:
> >> But it is then more than a little bit awkward - as I have to add a
> >> new complexType that restricts the complexType 'parent'
> >> and then within it I have to repeat all the stuff declared the
> >> 'parent' and its parent types (by extension) etc.
> >
> > Yes, that's a particularly nasty feature of derivation by
> restriction.
> >
> > If you're only restricting the attributes, and not the
> content model,
> > you can get around it by having both the base type and the derived
> > type reference the same named model group.
> >
> > Regards,
> >
> > Michael Kay
> > http://www.saxonica.com/
> > http://twitter.com/michaelhkay
> >
> >
>



Re: Restricting attribute use from optional to required

by C. M. Sperberg-McQueen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 26 Aug 2009, at 02:06 , Michael Kay wrote:

>
> There's an assymetry here between attributes and child elements. For  
> the
> content model (child elements), when you restrict a type you have to  
> restate
> all the parts of the content model that you want to inherit. For  
> attributes,
> you only have to list the things that have changed - any other  
> attributes
> are inherited automatically.
>
> (No, I can't justify why it was designed that way. Like most things  
> in XSD,
> it was probably because there were too many bright people on the  
> committee
> and each of them got their way on one feature of the language.)

If memory serves, the asymmetry reflects the view (not
often challenged) that the less you have to re-specify,
the easier it is to define a restriction.  With
attributes, it's easy to devise a way to specify only
changes:  attributes are inherited unless there is
an explicit statement that they are prohibited, or
unless there is a local declaration.  For the content
model, there appears to be universal consensus that having
to repeat the content model is inconvenient, but there
don't seem to be a lot of plausible alternatives.

It would be simple to specify that the content model
in the restriction must be satisfied, and also the
content model of the base type (parallel to the rule
for restrictions by pattern in simple types), but a
significant portion of the WG feared that this would
confuse users and that it would be difficult to
implement.  Since the proposal failed to generate
consensus, it died.

If there were a convenient way to point into a content
model and say "change THAT bit there", that would be
simple, too.  But schema component designators were not
well defined at that time, and even if they had been
I am not sure users would find their use dramatically
more convenient than repeating the content model.  The
technical direction approved by the WG at that fateful
meeting was to require that the restricted content
model have essentially the same structure as the
base type's content model, so that checking that it
defined a sublanguage would be trivial.  The actual
rules got out of hand when the 1.0 editors became
ambitious about allowing alternative equivalent
formulations of a language.  Some WG members continue
to believe that it would have been best (and would
still be best) simply to take the restriction's
content model as an additional constraint that need not
restate the base type's content model constraints
(the so-called 'content-model intersection' rule).
But some WG members continue to insist that their
implementors are not smart enough to calculate
a finite state automaton which is the intersection of
two content models, and they have thus far had their
way in XSD.

The treatment of the content model and attributes could
have been made more parallel by making the handling of
attributes less convenient; would that have been better?

--
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com
* http://cmsmcq.com/mib
* http://balisage.net
****************************************************************






Re: Restricting attribute use from optional to required

by Jan Přidal :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So basically you're telling me that I don't have to repeat all
attributes from attributeGroup in restriction and it is sufficient to
just list the changed ones, that is the 'label' attribute in my case?

<xsd:complexType name="parentWithReqLabelAttr">
 <xsd:complexContent>
   <xsd:restriction base="parent">
     <xsd:group ref="commonElements"/>
     <xsd:attribute name="label" type="xsd:string" use="required"/>
     <!-- no need to list all remaining attributes from 'attributes' group -->
   </xsd:restriction>
 </xsd:complexContent>
</xsd:complexType>

-Jan Pridal


2009/8/26 Michael Kay <mike@...>:

>
> There's an assymetry here between attributes and child elements. For the
> content model (child elements), when you restrict a type you have to restate
> all the parts of the content model that you want to inherit. For attributes,
> you only have to list the things that have changed - any other attributes
> are inherited automatically.
>
> (No, I can't justify why it was designed that way. Like most things in XSD,
> it was probably because there were too many bright people on the committee
> and each of them got their way on one feature of the language.)
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay
>
>
>> -----Original Message-----
>> From: xmlschema-dev-request@...
>> [mailto:xmlschema-dev-request@...] On Behalf Of Jan Pridal
>> Sent: 26 August 2009 08:23
>> To: xmlschema-dev@...
>> Subject: Re: Restricting attribute use from optional to required
>>
>> OK, so let's say we get rid off this repeating of content
>> model declaration using named model group, see below.
>> There's still one place I'd like to fix - the enumeration of
>> all attributes from 'attributes' attributeGroup inside the
>> 'parentWithReqLabelAttr' type declaration.
>>
>> <xsd:attributeGroup name="attributes">
>>   <xsd:attribute name="label" type="xsd:string" />
>>   ...
>> </xsd:attributeGroup>
>>
>> <xsd:group name="commonElements">
>>   <xsd:sequence>
>>       ...
>>   </xsd:sequence>
>> </xsd:group>
>>
>> <xsd:complexType name="parent">
>>   <xsd:group ref="commonElements"/>
>>   <xsd:attributeGroup ref="attributes"/> </xsd:complexType>
>>
>> <xsd:complexType name="parentWithReqLabelAttr">
>>   <xsd:complexContent>
>>     <xsd:restriction base="parent">
>>       <xsd:group ref="commonElements"/>
>>       <xsd:attribute name="label" type="xsd:string" use="required"/>
>>       ... list all remaining attributes from 'attributes' group!!!
>>     </xsd:restriction>
>>   </xsd:complexContent>
>> </xsd:complexType>
>>
>> <xsd:complexType name="derivedWithOptLabelAttr">
>>   <xsd:complexContent>
>>     <xsd:extension base="parent">
>>       <xsd:sequence>
>>         <xsd:element ref="style"/>
>>       </xsd:sequence>
>>       <xsd:attribute name="icon" type="xsd:string" use="optional"/>
>>     </xsd:extension>
>>   </xsd:complexContent>
>> </xsd:complexType>
>>
>> <xsd:complexType name="derivedWithReqLabelAttr">
>>   <xsd:complexContent>
>>     <xsd:extension base="parentWithReqLabelAttr">
>>       <xsd:sequence>
>>         <xsd:element ref="parameter" minOccurs="0"
>> maxOccurs="unbounded"/>
>>       </xsd:sequence>
>>       <xsd:attribute name="name" type="xsd:string" use="required"/>
>>     </xsd:extension>
>>   </xsd:complexContent>
>> </xsd:complexType>
>>
>> I'd like to have there something like a restriction of
>> attributeGroup, either local or a global inside the type
>> 'parentWithReqLabelAttr':
>>
>> <xsd:attributeGroup name="attributesWithReqLabelAttr">
>>   <xsd:attributeGroup ref="attributes">
>>     <xsd:attribute ref="label" use="required"/>
>>   </xsd:attributeGroup>
>> </xsd:attributeGroup>
>>
>> If this can be achieved then I would be able to add a new
>> attribute to the 'attributes' attributeGroup without changing
>> the 'parentWithReqLabelAttr' type. However as far as I know
>> there's no such mechanism.
>>
>> Jan Pridal
>>
>> 2009/8/17 Michael Kay <mike@...>:
>> >> But it is then more than a little bit awkward - as I have to add a
>> >> new complexType that restricts the complexType 'parent'
>> >> and then within it I have to repeat all the stuff declared the
>> >> 'parent' and its parent types (by extension) etc.
>> >
>> > Yes, that's a particularly nasty feature of derivation by
>> restriction.
>> >
>> > If you're only restricting the attributes, and not the
>> content model,
>> > you can get around it by having both the base type and the derived
>> > type reference the same named model group.
>> >
>> > Regards,
>> >
>> > Michael Kay
>> > http://www.saxonica.com/
>> > http://twitter.com/michaelhkay
>> >
>> >
>>
>
>


RE: Restricting attribute use from optional to required

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> So basically you're telling me that I don't have to repeat
> all attributes from attributeGroup in restriction and it is
> sufficient to just list the changed ones, that is the 'label'
> attribute in my case?
>
> <xsd:complexType name="parentWithReqLabelAttr">  <xsd:complexContent>
>    <xsd:restriction base="parent">
>      <xsd:group ref="commonElements"/>
>      <xsd:attribute name="label" type="xsd:string" use="required"/>
>      <!-- no need to list all remaining attributes from
> 'attributes' group -->
>    </xsd:restriction>
>  </xsd:complexContent>
> </xsd:complexType>
>

Correct.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay