Restrict complexType with attributes in XSD?

不羁的心 提交于 2019-11-29 16:12:34

In order to have an attribute on an element with restricted content, define a new xs:simpleType and then use xs:extension to extend it with an attribute:

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

  <xs:element name="algo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="nota" type="t_algo" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="t_algo">
    <xs:simpleContent>
      <xs:extension base="t_algo_content">
        <xs:attribute name="modul" type="t_modul"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="t_modul">
    <xs:restriction base="xs:string">
      <xs:pattern value="m0[0-9]"/>
    </xs:restriction> 
  </xs:simpleType> 

  <xs:simpleType name="t_algo_content">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="10"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

Note also that I've simplified your regex pattern in the first case and used minInclusive/maxInclusive to more naturally express your integer range in the second case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!