简体   繁体   English

如何使用XSD / XML Schema约束XML元素以包含有限长度的字符串内容?

[英]How can I use XSD / XML Schema to constrain an XML element to have string content of limited length?

I have an XSD Schema where I am trying to implement some basic validation. 我有一个XSD架构,正在尝试实现一些基本的验证。

The provided code is as follows: 提供的代码如下:

<xs:complexType>
    <xs:sequence>
          <xs:element name="PersonID" type="xs:string" minOccurs="1" maxOccurs="1"/>
          <xs:element name="AgreementID" type="xs:string" minOccurs="1" maxOccurs="1"/>

I want to add some validation so the element PersonID can only have a maximum length of 8 characters. 我想添加一些验证,以便元素PersonID只能具有8个字符的最大长度。 The agreementID to have 7 and so on. 协议ID必须为7,依此类推。

What is the easiest way to implement this? 实现此目的最简单的方法是什么? I have tried looking at length but it doesn't seem to be validating when building the file. 我尝试查看长度,但在构建文件时似乎没有进行验证。

Thanks 谢谢

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

  <xs:simpleType name="PersonIDType">
    <xs:restriction base="xs:string">
      <xs:maxLength value="8" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="AgreementIDType">
    <xs:restriction base="xs:string">
      <xs:maxLength value="7" />
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element name="PersonID" type="PersonIDType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="AgreementID" type="AgreementIDType" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM