0

In an xsd file I have this element base type :

<xs:complexType name="event" abstract="true" >
    <xs:attribute name="move" type="aos:move_ref" use="required" />
    <xs:attribute name="type" type="aos:event_type" use="required" />
</xs:complexType>

And I want to define the value of the type attribute in the children types, so I tried this :

<xs:complexType name="signal" >
    <xs:complexContent>
      <xs:extension base="aos:event">
        <xs:attribute name="type" type="aos:event_type" fixed="signal" />
        <xs:attribute name="source" type="aos:signal_source" use="required" />
      </xs:extension>
    </xs:complexContent>
 </xs:complexType>

Visual Studio don't seem to bother but CodeSynthesis C++ code generator don't seem to agree :

error: attribute 'type' is already defined in base

How should I write this? I just want the value of the type attribute to be specific to each different child type.

edit ----

To make the question more clear, I'll write the same thing I want to do but in C++.

Here is the base class :

class Event
{
public:

   std::string name() const { return m_name; }

protected:

   // we need the child class to set the name
   Event( const std::string& name ) : m_name( name ) {} 

   // it's a base class
   virtual ~Event(){}

private:

   std::string m_name;

};

Now, one of the children could be implemented like this :

class Signal : public Event
{
public:

   Signal() : Event( "signal" ){}

};

As you can see, the child class define the values of attributes that are defined by the base class. Is it even possible to express in xsd?

6
  • can you get us a sample XML please ? Nov 18, 2010 at 11:43
  • Is it really necessary? I can but I don't see how it can help. You want some kind of use-case?
    – Klaim
    Nov 18, 2010 at 11:45
  • nope, I just want to be clarified, not clear with ur statement like I just want the value of the type attribute to be specific to each different child type, if you want to define an attribute differently under same child/entity then its not possible, where as if you want to define same attribute differently for different entity/children then quite possible. Nov 18, 2010 at 12:14
  • I want the same attribute to have different values defined by the child types. If there is really no way to set a value to an attribute that was defined in the base type can't be set in the child type, then please make it an answer?
    – Klaim
    Nov 18, 2010 at 12:31
  • In fact, in the xml, this will not be apparent. It will only be apparent if you try to get the "type" attribute from different element types, here <signal> for example. Maybe that's not useful in the end, I'm asking myself if I can have the information another way.
    – Klaim
    Nov 18, 2010 at 12:32

1 Answer 1

2
+50

To derive a type and fix a value, use a restriction:

<xs:complexType name="signal" >
    <xs:complexContent>
      <xs:restriction base="aos:event">
        <xs:attribute name="type" type="aos:event_type" fixed="signal" use="required" />
        <xs:attribute name="source" type="aos:signal_source" use="required" />
      </xs:restriction>
    </xs:complexContent>
 </xs:complexType>

From reading the spec, I would have expected that you couldn't add attributes in a restriction unless the base type had an attribute wildcard, but the W3C XSD validator accepts the above. If you run into problems, you can break up the definition into a restriction and an extension:

<xs:complexType name="fixedSignalEvent">
  <xs:complexContent>
    <xs:restriction base="aos:event">
      <xs:attribute name="type" type="aos:event_type" fixed="signal" use="required" />
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="signal" >
  <xs:complexContent>
    <xs:extension base="aos:fixedSignalEvent">
      <xs:attribute name="source" type="aos:signal_source" use="required" />
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Another fix would be to add an attribute wildcard to the base type.

<xs:complexType name="event" abstract="true" >
    <xs:attribute name="move" type="aos:move_ref" use="required" />
    <xs:attribute name="type" type="aos:event_type" use="required" />
    <xs:anyAttribute />
</xs:complexType>

This isn't an equivalent solution, as it allows an event to have anything for an attribute (which may be undesirable, generally speaking, but perhaps not for code generation), and it doesn't add an additional type (which is desirable).

Note that any particles (elements, groups or wildcards) in the base must be repeated in the restriction, else they won't be allowed in the element. If a restricted attribute is required on the base, it must also be required in the restriction. There are numerous other properties a restriction must fulfill to be a valid derivation or particle. The spec isn't that readable, but you can usually stumble through it.

See also: "how to use restrictions and extensions in XSD simultanously".

11
  • It seems that this parser still don't like the fact that I'm repeating the declaration of the attribute even using restriction over extension (with a simple child type that only tries to set the value of this attribute) ... too bad :/
    – Klaim
    Nov 21, 2010 at 4:58
  • Wait, I did a mistake, it seems to works! I wasn't trying to parse the right file, sorry! I'll have to do more checks and do the trick for more complex children class.
    – Klaim
    Nov 21, 2010 at 5:00
  • Ok so with complex children (that extend the type) I can't find how to "break up the definition into a restriction and an extension". Can I do this in the same complex type? Can you add an example?
    – Klaim
    Nov 21, 2010 at 5:05
  • Does the additional example clarify things? Note you can't nest restrictions within extensions (or vice versa), so you'll need two separate complex types.
    – outis
    Nov 21, 2010 at 6:18
  • Yes that's clear. That's what I guessed but hoped their was a more intuitive way. The separation is their only to set values, that's not good design... but if it's the only way then I'll do this. Thanks!
    – Klaim
    Nov 21, 2010 at 13:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.