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

发布于 2024-10-02 18:58:16 字数 1513 浏览 3 评论 0原文

在 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来表达吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-10-09 18:58:16

,请使用限制

<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”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文