xsd attribute and element validation

我怕爱的太早我们不能终老 提交于 2019-12-11 18:18:56

问题


I am trying to create an XSD schema which will validate the following xml.

<Item ItemGUID="3F2504E0-4F89-11D3-9A0C-0305E82C3301">The name of the item</Item>

I want to validate the max length of the attribute "ItemGUID" to 36 characters and "The name of the item" to a max 25 characters.

How can it be validated to satisfy the above condition using the xsd schema?


回答1:


With XML Schema, you can do something like this:

<xs:element name="Item">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="string25">
        <xs:attribute name="ItemGUID" type="string36" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element> 

<xs:simpleType name="string25">
  <xs:restriction base="xs:string">
    <xs:minLength value="1"/>
    <xs:maxLength value="25"/>
  </xs:restriction>
</xs:simpleType>


<xs:simpleType name="string36">
  <xs:restriction base="xs:string">
    <xs:minLength value="1"/>
    <xs:maxLength value="36"/>
  </xs:restriction>
</xs:simpleType>

I haven't tried it, but if this doesn't work it should be very close to what you need.



来源:https://stackoverflow.com/questions/832542/xsd-attribute-and-element-validation

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