XSD:如何在子元素类型中设置属性值?

在xsd文件中,我有这个元素基类型:
<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>
我想在子类型中定义
type
属性的值,所以我尝试了这个:
<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似乎没有打扰,但CodeSynthesis C ++代码生成器似乎不同意:   错误:属性'type'已经存在   以基础定义 我该怎么写呢?我只想让
type
属性的值特定于每个不同的子类型。 编辑---- 为了使问题更清楚,我将在C ++中编写我想要做的同样的事情。 这是基类:
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;

};
现在,其中一个孩子可以像这样实现:
class Signal : public Event
{
public:

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

};
如您所见,子类定义由基类定义的属性的值。是否有可能在xsd中表达?     
已邀请:
要派生类型并修复值,请使用限制:
<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>
从阅读规范,我原本预计你不能在限制中添加属性,除非基类型具有属性通配符,但W3C XSD验证器接受上述内容。如果遇到问题,可以将定义分解为限制和扩展:
<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>
另一个修复方法是将属性通配符添加到基本类型。
<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>
这不是一个等价的解决方案,因为它允许事件为属性提供任何内容(一般来说,这可能是不受欢迎的,但可能不是代码生成),并且它不添加其他类型(这是可取的) 。 请注意,必须在限制中重复基础中的任何粒子(元素,组或通配符),否则将不允许在元素中使用它们。如果基础上需要受限属性,则限制中也必须使用该属性。限制必须满足许多其他属性才能成为有效的派生或粒子。规范不是那么可读,但你通常会偶然发现它。 另请参阅:“如何在XSD中同时使用限制和扩展”。     

要回复问题请先登录注册