简体   繁体   English

如何使用XSD限制将Xml内容限制为特定长度?

[英]How to constrain Xml content to a specific length using xsd restrictions?

Using XSD, is it possible to constrain the total text within a Node. 使用XSD,可以将一个节点中的全部文本限制在其中。 In the example below, I want Address Node content to be restricted to 255 characters. 在下面的示例中,我希望地址节点的内容限制为255个字符。

<Address>
    <Line1>Text</Line1>
    <Line2>Text</Line2>
    <City></City>
    <Street></Street>
    <State></State>
    <Country></Country>
</Address>

So, if I only had Line1 and Line2 in my address and City, Street, State and Country were empty, then Line1 could be 254 characters and Line2 will be 1 character. 因此,如果我的地址中只有Line1和Line2,并且City,Street,State和Country为空,则Line1可以为254个字符,而Line2为1个字符。

Is it possible to set such constraints/restrictions within the xsd itself? 是否可以在xsd本身中设置此类约束/约束?

You can constrain the text with a single element to be a given size ie 您可以将单个元素的文本限制为给定的大小,即

            <xs:element name="Line1">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:maxLength value="255" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>

But you can't say Line1 + line2 + City ... < 255. 但是您不能说Line1 + line2 + City ... <255。

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

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