xml XSD:如何限制字符串类型属性中的字符数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/966948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSD: how to restrict number of characters in string type attribute?
提问by Ankush
I have a question regarding adding restriction in my xml schema(xsd). I have a complex-type like:
我有一个关于在我的 xml 模式(xsd)中添加限制的问题。我有一个复杂类型,如:
<xsd:complexType name="xxx">
<xsd:attribute/>
.....
</xsd:complexType>
I have many attributes in this complex-type, and few of them are of string type. Now I want those string type attributes to be restricted to y no. of chars, how do I add this restriction?
我在这个复杂类型中有很多属性,其中很少有字符串类型。现在我希望将这些字符串类型属性限制为 y no。字符数,如何添加此限制?
Thanks! -Ankush
谢谢!-安库什
回答by marc_s
You need to create a simple type like so:
您需要像这样创建一个简单的类型:
<xs:simpleType name="LimitedString">
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
and then use this new type in your schema:
然后在你的架构中使用这个新类型:
<xs:complexType name="test">
<xs:sequence>
<xs:element name="abc" type="xs:String" />
</xs:sequence>
<xs:attribute type="LimitedString" name="myattr" />
</xs:complexType>
Marc
马克
回答by Cheeso
You can restrict the string to a number of chars like this:
您可以将字符串限制为多个字符,如下所示:
<xs:simpleType name="threeCharString">
<xs:annotation>
<xs:documentation>3-char strings only</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:length value="3"/>
</xs:restriction>
</xs:simpleType>
The xs:lengthin the above limits the length of the string to exactly3 chars. You can also use xs:minLengthand xs:maxlength, or both.
所述的xs:长度在上述范围的字符串的长度,以恰好3个字符。您还可以使用xs:minLength和xs:maxlength,或两者都使用。
You can provide a pattern like so:
您可以提供这样的模式:
<xs:simpleType name="fourCharAlphaString">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]{4}"/>
</xs:restriction>
</xs:simpleType>
The above says, 4 chars, of any of a-z, A-Z. The xs:patternis a regular expression, so go to town with it.
上面说,4个字符,az,AZ中的任何一个。该XS:模式是一个正则表达式,所以去镇上吧。
You can restrict the string to a particular set of strings in this way:
您可以通过这种方式将字符串限制为一组特定的字符串:
<xs:simpleType name="iso3currency">
<xs:annotation>
<xs:documentation>ISO-4217 3-letter currency codes. Only a subset are defined here.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:length value="3"/>
<xs:enumeration value="AUD"/>
<xs:enumeration value="BRL"/>
<xs:enumeration value="CAD"/>
<xs:enumeration value="CNY"/>
<xs:enumeration value="EUR"/>
<xs:enumeration value="GBP"/>
<xs:enumeration value="INR"/>
<xs:enumeration value="JPY"/>
<xs:enumeration value="RUR"/>
<xs:enumeration value="USD"/>
</xs:restriction>
</xs:simpleType>
回答by Nemir
The answer by marc_s would require you to define a new type for every possible string length you might use. Instead you can define the restriction directly on the attribute itself.
marc_s 的答案要求您为可能使用的每个可能的字符串长度定义一个新类型。相反,您可以直接在属性本身上定义限制。
<xs:complexType name="Test">
<xs:attribute name="LimitedString">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value = "50"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
回答by Indranil Acharya
Add this and it worked for me,
添加这个,它对我有用,
<xs:element name="vendor">
<xs:simpleType>
<xs:annotation>
<xs:documentation>80-char strings only</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="80"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
回答by Luixv
you can always define the maximal length of a string in xsd.
Just add the attribute maxLengthresp. minLength.
您始终可以在 xsd 中定义字符串的最大长度。只需添加属性maxLengthresp。minLength.

