2011-09-21 81 views
8

在相同的元素的属性的验证我想验证的元素“测试”应内容限制和XSD

  • 具有其含量受限制的(例如,使用图案限制),和
  • 包含某些属性(例如,'id','class'和'name')。

的XSD我在写这个样子的:

<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:属性' 元素:

“属性”和内容模型是相互排斥的

有没有办法来验证这两个内容限制属性在同一个元素上?

回答

13

您需要分离出您的限制并给它一个名称,然后将其称为扩展的基本类型。像这样:

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

谢谢!这工作完美。这也很方便,因为我想在其他地方重新使用限制。顺便说一句 - 我有多行内容的问题,但通过在模式后面添加''来解决这些问题。 – Jonathan