Welcome to MSDN Blogs Sign in | Join | Help

Syndication

Tags

    No tags have been created or used yet.
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" />

Published Friday, August 12, 2005 12:52 PM by neerajag

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: validation pattern for allowing non-empty string in xml file @ Wednesday, May 20, 2009 7:32 AM

Thanks,

Just what I needed!

Aharon Levine

# re: validation pattern for allowing non-empty string in xml file @ Monday, July 13, 2009 5:11 AM

Consider a element :

<A>

 <B/>

 <C/>

</A>

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

valdating a element such that, it must contain a child elemet

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker