XSD中同一元素的内容限制和属性验证 [英] Content restriction and attribute validation on the same element in XSD
本文介绍了XSD中同一元素的内容限制和属性验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证元素测试"应该
I would like to validate that an element 'Test' should
- 对其内容进行限制(例如,使用模式限制),并且
- 包含某些属性(例如,"id","class"和"name").
我正在编写的XSD如下:
The XSD I'm writing looks like this:
<xsd:element name="Test" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType mixed="true">
<xsd:simpleContent>
<xsd:restriction>
<xsd:pattern value="xyz"/>
</xsd:restriction>
</xsd:simpleContent>
<xsd:attribute name="id" type="xsd:string"></xsd:attribute>
<xsd:attribute name="class" type="xsd:string"></xsd:attribute>
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>
但是,当我在Visual Studio中编写此代码时,在'xsd:attribute'元素上出现以下错误:
However, when I code this in Visual Studio, I get the following error on the 'xsd:attribute' elements:
属性"和内容模型是互斥的
'attribute' and content model are mutually exclusive
有没有办法验证同一元素上的内容限制和属性?
Is there a way to validate both a content restriction and attributes on the same element?
推荐答案
您需要分离出限制条件并为其命名,然后将其称为扩展的基本类型.像这样:
You need to separate out your restriction and give it a name, then refer to it as a base type for an extension. Like this:
<xsd:simpleType name="RestrictedString">
<xsd:restriction base="xsd:string">
<xsd:pattern value="xyz" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="Test">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="RestrictedString">
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="class" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
这篇关于XSD中同一元素的内容限制和属性验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文