Condividi tramite


validation pattern for allowing non-empty string in xml file

I was trying to validate a xml file using xsd wherein a node value should be non-empty. Means there should be atleast one non-whitespace character into that. After lots of googling, I finally wrote up a small pattern to validate that. Here is the fragment.

Define a new type derived from string type and apply the pattern constraint.

<!-- Non-empty string -->

<xs:simpleType name="NonEmptyString">

<xs:restriction base="xs:string">

<xs:minLength value="1" />

<xs:pattern value=".*[^\s].*" />

</xs:restriction>

</xs:simpleType>

Now use the new type "NonEmptyString" for all the nodes you want to enforce non-empty constraint.

<

xs:element name="Name" type="NonEmptyString" />

Comments

  • Anonymous
    May 19, 2009
    Thanks, Just what I needed!

  • Anonymous
    July 12, 2009
    The comment has been removed

  • Anonymous
    January 05, 2015
    I couldn't find a way to do it without declaring a new type. Thanks, this was really the best way to do it.