Attribute with fixed value in derived types

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Attribute with fixed value in derived types

Florent Georges-3
  Hi,

  I have a complex type with a required attribute:

    <xs:complexType name="type">
       ...
       <xs:attribute ref="my:attr" use="required"/>
    </xs:complexType>

  I then use it to define some elements with additional children:

    <xs:element name="elem">
       <xs:complexType>
      <xs:complexContent>
         <xs:extension base="my:type">
        <xs:sequence>
           ...
        </xs:sequence>
         </xs:extension>
      </xs:complexContent>
       </xs:complexType>
    </xs:element>

  For some of those elements, I'd like to set a fixed value for the
attribute.  How can I do that, with XML Schema 1.0?  In addition, is
it possible to set a fixed value in the complex type, then to override
it in some elements only?

  Regards,


--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/

Reply | Threaded
Open this post in threaded view
|

Re: Attribute with fixed value in derived types

Michael Kay
I suspect that you have to first define a restriction of the type in
which the attribute has a fixed value, and then define an extension of
the restricted type.

Or in XSD 1.1, as I discovered recently on this list, you can add an
assertion as part of the extension, so you could define the fixed value
by means of an assertion and thereby avoid the restriction step. (But
you wanted an XSD 1.0 solution...)

Michael Kay
Saxonica

On 27/06/2012 10:44, Florent Georges wrote:

>    Hi,
>
>    I have a complex type with a required attribute:
>
>      <xs:complexType name="type">
>         ...
>         <xs:attribute ref="my:attr" use="required"/>
>      </xs:complexType>
>
>    I then use it to define some elements with additional children:
>
>      <xs:element name="elem">
>         <xs:complexType>
>        <xs:complexContent>
>           <xs:extension base="my:type">
>          <xs:sequence>
>             ...
>          </xs:sequence>
>           </xs:extension>
>        </xs:complexContent>
>         </xs:complexType>
>      </xs:element>
>
>    For some of those elements, I'd like to set a fixed value for the
> attribute.  How can I do that, with XML Schema 1.0?  In addition, is
> it possible to set a fixed value in the complex type, then to override
> it in some elements only?
>
>    Regards,
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Attribute with fixed value in derived types

Florent Georges-3
Michael Kay wrote:

  Hi Mike,

  Thank you for your response.

> I suspect that you have to first define a restriction of the type in
> which the attribute has a fixed value, and then define an extension
> of the restricted type.

  I am afraid I don't see exactly what you mean.  How can I define an
extension of a simple type within the extension of a complex type
(within an attribute redefinition)?  And if I could, that would then
allow more values for the attribute, not set a fixed value, wouldn't
it?  But I'm sure I just missed something...

> Or in XSD 1.1, as I discovered recently on this list, you can add an
> assertion as part of the extension, so you could define the fixed
> value by means of an assertion and thereby avoid the restriction
> step.  (But you wanted an XSD 1.0 solution...)

  That's exactly why I asked for 1.0 ;-)

--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/

Reply | Threaded
Open this post in threaded view
|

Re: Attribute with fixed value in derived types

Michael Kay
Something like this:

     <xs:complexType name="type">
        ...
        <xs:attribute ref="my:attr" use="required"/>
     </xs:complexType>

     <xs:complexType name="restrictedType">
       ...

       <xs:attribute ref="my:attr" use="required" fixed="...."/>
     </xs:complexType>
 
    I then use it to define some elements with additional children:

     <xs:element name="elem">
        <xs:complexType>
       <xs:complexContent>
          <xs:extension base="my:restrictedType">
         <xs:sequence>
            ...
         </xs:sequence>
          </xs:extension>
       </xs:complexContent>
        </xs:complexType>
     </xs:element>

Michael Kay
Saxonica

Reply | Threaded
Open this post in threaded view
|

Re: Attribute with fixed value in derived types

Florent Georges-3
Michael Kay wrote:

> Something like this:

  Thank you!  I've been able to adapt it.  So if I am right, the key
point here is to define an intermediary, global restricted complex
type to set the fixed value, then use it as the base for the concrete
element (instead of the original base type).

  Thanks for the solution.

  Regards,

--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/