Search code examples
regexxmlxsdxsd-validationxml-validation

How to require more than whitespace content in XSD?


I've been having problems to develop a regex expression that would allow me to check the following:

Tag text is not:

Empty

<TextTag></TextTag>

White space

<TextTag>    </TextTag>

line Break

<TextTag>
</TextTag>

But at the same time allow any spaces and line breaks in the middle of the text like so:

<TextTag>this would 
be an example </TextTag>

the closest I've been able to reach was .*[^\s-].*but it fails when there is a line break in the middle like the example I gave you.

Do you know how could I do this?


Solution

  • You can use a xsd:whiteSpace facet with value="collapse" and require a length of at least 1:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="MoreThanWhiteSpaceElement">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:whiteSpace value="collapse"/>
            <xs:minLength value="1"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:schema>