如何在 XSD 中同时限制元素内容和定义属性

How to restrict element content and define attributes simultaneously in XSD

我正在尝试将 XSD 中的限制添加到复杂类型元素。当我使用 XML Notepad 2007 验证 XML 文件时,我正在尝试使用 minInclusive value="0.00034" 和 maxInclusive value="99" 添加边界。我已经搜索了档案并且很难使用在何处添加限制的语法。感谢任何帮助。

<xsd:element name="R-value">
    <xsd:annotation>
        <xsd:documentation>Resistance of material</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
        <xsd:simpleContent>
            <xsd:extension base="xsd:decimal">
                <xsd:attribute name="unit" type="resistanceUnitEnum" use="required"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>

定义一个类型来覆盖您的限制,然后扩展该类型以添加​​到您的属性中:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:simpleType name="R-value-content-type">
    <xsd:restriction base="xsd:decimal">
      <xsd:minInclusive value="0.00034"/>
      <xsd:maxInclusive value="99"/>
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:element name="R-value">
    <xsd:annotation>
      <xsd:documentation>Resistance of material</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="R-value-content-type">
          <xsd:attribute name="unit" use="required"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>  
</xsd:schema>