admin管理员组文章数量:1276754
I want to be able to represent two different XML structures with the same schema: One:
<polly> wibble </polly>
Two:
<polly>
<element> foo </element>
<element> bar </element>
<element> baz </element>
</polly>
(in this case they're all string
elements, but I'd like to be able to solve this for other types)...
What I've tried is to create a hierarchy like with a base type:
<xsd:complexType name="polly"/>
then two subtypes, a string:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly"/>
</xsd:complexContent>
</xsd:complexType>
and then the array type:
<xsd:complexType name="polly_array">
<xsd:complexContent>
<xsd:extension base="tns:polly">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="element" type="tns:polly_array_element"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="polly_array_element">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
which I can then instantiate with:
<xsd:element minOccurs="0" name="polly" type="tns:polly"/>
So good so far, but I can't tell it that polly_string
should really be a simple type! And I can't place a restriction
on it either, because it's already an extension
(at least from my limited reading they're mutually exclusive).
I've mostly used vs code to do my validating, as it's more verbose than other editors, but I must admit I don't quite understand what it's telling me, hence asking here.
I've tried:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly">
<xsd:restriction base="xs:string"/>
</xsd:extension>
</xsd:complexContent>
but I get:
The content of 'polly_string' is invalid. Element 'restriction' is invalid, misplaced, or occurs too often.
I've tried (which wouldn't get me the whole way, but was an exploration):
<xsd:complexType name="polly_string">
<xsd:simpleContent>
<xsd:extension base="tns:polly">
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
but I get:
Complex Type Definition Representation Error for type 'polly_string'. When is used, the base type must be a complexType whose content type is simple, or, only if restriction is specified, a complex type with mixed content and emptiable particle, or, only if extension is specified, a simple type. 'polly' satisfies none of these conditions.
which I think means "You can't create a simple extension of something that may be complex"?
I've tried:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly"/>
<xsd:extension base="xs:string"/>
</xsd:complexContent>
</xsd:complexType>
But I get:
The content of 'polly_string' is invalid. Element 'extension' is invalid, misplaced, or occurs too often.
presumably meaning "only single inheritance allowed buddy: straight to XML jail!"
I've seen some other posts with similar-looking answers, but I can't quite seem to see how to apply the solutions :-(
any kind souls able to help?
I want to be able to represent two different XML structures with the same schema: One:
<polly> wibble </polly>
Two:
<polly>
<element> foo </element>
<element> bar </element>
<element> baz </element>
</polly>
(in this case they're all string
elements, but I'd like to be able to solve this for other types)...
What I've tried is to create a hierarchy like with a base type:
<xsd:complexType name="polly"/>
then two subtypes, a string:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly"/>
</xsd:complexContent>
</xsd:complexType>
and then the array type:
<xsd:complexType name="polly_array">
<xsd:complexContent>
<xsd:extension base="tns:polly">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="element" type="tns:polly_array_element"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="polly_array_element">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
which I can then instantiate with:
<xsd:element minOccurs="0" name="polly" type="tns:polly"/>
So good so far, but I can't tell it that polly_string
should really be a simple type! And I can't place a restriction
on it either, because it's already an extension
(at least from my limited reading they're mutually exclusive).
I've mostly used vs code to do my validating, as it's more verbose than other editors, but I must admit I don't quite understand what it's telling me, hence asking here.
I've tried:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly">
<xsd:restriction base="xs:string"/>
</xsd:extension>
</xsd:complexContent>
but I get:
The content of 'polly_string' is invalid. Element 'restriction' is invalid, misplaced, or occurs too often.
I've tried (which wouldn't get me the whole way, but was an exploration):
<xsd:complexType name="polly_string">
<xsd:simpleContent>
<xsd:extension base="tns:polly">
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
but I get:
Complex Type Definition Representation Error for type 'polly_string'. When is used, the base type must be a complexType whose content type is simple, or, only if restriction is specified, a complex type with mixed content and emptiable particle, or, only if extension is specified, a simple type. 'polly' satisfies none of these conditions.
which I think means "You can't create a simple extension of something that may be complex"?
I've tried:
<xsd:complexType name="polly_string">
<xsd:complexContent>
<xsd:extension base="tns:polly"/>
<xsd:extension base="xs:string"/>
</xsd:complexContent>
</xsd:complexType>
But I get:
The content of 'polly_string' is invalid. Element 'extension' is invalid, misplaced, or occurs too often.
presumably meaning "only single inheritance allowed buddy: straight to XML jail!"
I've seen some other posts with similar-looking answers, but I can't quite seem to see how to apply the solutions :-(
any kind souls able to help?
Share Improve this question asked Feb 24 at 17:17 andreasmartensandreasmartens 1081 silver badge6 bronze badges 2- See links at bottom of following : w3schools.blog/xsd-xml-schema-definition-tutorial – jdweng Commented Feb 24 at 18:46
- @jdweng: I don't get it, all those links are dead to me, they all send me to the homepage of w3schools.blog. – andreasmartens Commented Feb 25 at 8:57
1 Answer
Reset to default 2You can't define that an element is allowed to have either a simple type or a complex type. The closest you can get is to define it with mixed content so it allows any mixture of text nodes and child elements. In XSD 1.1 you can define an additional constraint with assertions: for example, having given it a mixed content model, you can then assert xpath="exists(child::text()) ne exists(child::*)"
to say that you can either have text node children or element children but not both.
本文标签: xsdHow can I create multiple inheritance or polymorphism in XML SchemaStack Overflow
版权声明:本文标题:xsd - How can I create multiple inheritance or polymorphism in XML Schema? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251512a2365876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论