简体   繁体   中英

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. In the example below, I want Address Node content to be restricted to 255 characters.

<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.

Is it possible to set such constraints/restrictions within the xsd itself?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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