【问题标题】:Content restriction and attribute validation on the same element in XSDXSD 中同一元素的内容限制和属性验证
【发布时间】:2011-11-21 13:33:00
【问题描述】:

我想验证元素“测试”是否应该

  • 限制其内容(例如,使用模式限制),并且
  • 包含某些属性(例如,“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:attribute”元素上收到以下错误:

“属性”和内容模型互斥

有没有办法验证同一元素上的内容限制属性?

【问题讨论】:

    标签: attributes xsd restriction


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!这非常有效。这也很方便,因为我想在其他地方重新使用限制。顺便说一句 - 我遇到了多行内容的问题,但通过在模式后添加 '' 解决了这些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多