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 (3)

  1. Aharon Levine says:

    Thanks,

    Just what I needed!

  2. valdating a element such that, it must contain a child elemet says:

    Consider a element :

    <A>

     <B/>

     <C/>

    </A>

    A either contain B or C or both. The only invalid condition is empty A.

  3. Kevin Q says:

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

Skip to main content