【问题标题】:Define element and attribute with restrictions定义有限制的元素和属性
【发布时间】:2023-03-21 11:42:01
【问题描述】:

我需要定义具有可选属性的元素序列的 XSD。我还需要限制元素和属性的字符串长度。使用以下代码我似乎无法定义“限制”:

  <xs:element name="PeopleMentioned" minOccurs="0">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Person" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="PersonTopic" type="xs:string" />
              </xs:extension>
            </xs:simpleContent>                        
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

我需要限制“Person”和“PersonTopic”的长度,就像通常对简单类型所做的那样:

  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:maxLength value="18" />
    </xs:restriction>
  </xs:simpleType>

【问题讨论】:

标签: xml xsd


【解决方案1】:

对元素和属性进行限制定义,即PersonPersonTopic是最大长度为18的字符串:

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

  <xsd:element name="PeopleMentioned">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Person" minOccurs="1" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:simpleContent>
              <xsd:extension base="String18">
                <xsd:attribute name="PersonTopic" type="String18"/>
              </xsd:extension>
            </xsd:simpleContent>                        
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:simpleType name="String18">  
    <xsd:restriction base="xsd:string">  
      <xsd:maxLength value="18" />
    </xsd:restriction>  
  </xsd:simpleType>
</xsd:schema>

【讨论】:

  • 谢谢@kjhughes。我需要两个单独的长度,但根据你的回答,我定义了两种简单的类型(String18 和 String50)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2018-03-08
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多