2

I have elements in my xml schema that are of type string. I need to restrict the string, however, to disallow any whitespace at the beginning, end, or "inside" the string and also have a length of at least 1.

Here are some values followed by what I would expect the validation to result in:

"HELLO" (Valid)
"H" (Valid)
"" (Not Valid) [length = 0]
" HELLO" (Not Valid) [starts with space]
"HELLO " (Not Valid) [ends with space]
"HEL LO" (Not Valid) [contains a space]

I know how to make a simple type which restricts string and I know how to require the length to be at least 1:

<xs:simpleType name="MyString">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
    </xs:restriction>
</xs:simpleType>

Now I just need a pattern to add to my simple type that does what I need.

Can someone tell me what pattern to use?

Thank you.

NOTE - I am not using a program like Xml Spy to validate my Xml documents. I am writing a C# program that serializes/deserializes Xml documents using XmlSerializer.

The reason why I bring this up is because I have read various other posts about Xml schema and white spaces and using xs:whiteSpace = preserve, collapse, replace. I don't think that XmlSerializer handles this though.

Jan Tacci
  • 3,131
  • 16
  • 63
  • 83

1 Answers1

1

I think regular expression pattern is what you are looking for. Something like:

<xs:simpleType name="MyString">
    <xs:restriction base="xs:string">
        <xs:pattern value="[^\s]+"/>
    </xs:restriction>
</xs:simpleType>

Note that space before/after value may not be actually part of value unless xs:whiteSpace = preserve. Consider reading following sections of the XSD specification White Space Normalization during Validation and Built in data types:string.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • That worked thanks! :) Do you also know what pattern I would use to validate that a string does not start with or end with a space? I don't care if it has spaces in the middle I just don't want spaces at the beginning or end. For example "Hello There" would be valid but not " Hello There" and not "Hello There ". – Jan Tacci Jul 21 '13 at 00:13
  • @JanTacci something like (untested, use one of many regex tools to try): "[^\s].*[^\s]|[^\s]" (not-whitespace followed by any number of any chars followed by not-whitespace OR single not-whitespace). – Alexei Levenkov Jul 21 '13 at 00:40