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;

};

これで、子の1つを次のように実装できます。

class Signal : public Event
{
public:

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

};

ご覧のとおり、子クラスは基本クラスによって定義される属性の値を定義します。xsdで表現することさえ可能ですか?

4

1 に答える 1

2

タイプを導出して値を修正するには、制限を使用します。

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

仕様を読んだことから、基本タイプに属性ワイルドカードがない限り、制限に属性を追加できないと予想していましたが、W3CXSDバリデーターは上記を受け入れます。問題が発生した場合は、定義を制限と拡張に分割できます。

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

もう1つの修正は、基本タイプに属性ワイルドカードを追加することです。

<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で制限と拡張機能を同時に使用する方法」も参照してください。

于 2010-11-21T00:54:13.790 に答える