【发布时间】:2010-11-16 21:29:52
【问题描述】:
在一个 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++ code generator 似乎不同意:
错误:属性“类型”已经存在 在base中定义
我该怎么写?我只希望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表达吗?
【问题讨论】:
-
你能给我们一个示例 XML 吗?
-
真的有必要吗?我可以,但我不明白它有什么帮助。你想要某种用例吗?
-
不,我只是想澄清一下,不清楚你的声明像
I just want the value of the type attribute to be specific to each different child type,如果你想在同一个子/实体下定义一个属性不同那么它不是可能,如果您想为不同的实体/子项以不同的方式定义相同的属性,那么很有可能。 -
我希望相同的属性具有由子类型定义的不同值。如果实在没有办法给在基类型中定义的属性设置值,而不能在子类型中设置,那么请回答一下?
-
其实在xml中,这点不会很明显。只有当您尝试从不同的元素类型中获取“类型”属性时,它才会很明显,例如这里的
。也许这最终没有用,我在问自己是否可以通过其他方式获得信息。
标签: c++ xsd xsd.exe codesynthesis