I am preparing XSD for some XML file required in my application. Snap shot of the XML is shown here:


            <Role HomeFolder="All Folders\Sales" />




Specified XML has one attributes: HomeFolder. While preparing XSD, I want to put constraint that attribute value HomeFolder should have atlest some value in it. It should not just have white spaces only.


For example,


HomeFolder="All Folders"                       - Valid

HomeFolder="     All   Folders     "           - Valid

HomeFolder="        "                              - Invalid (tab as whitespace character)

HomeFolder="   "                                   - Invalid (space as whitespace character)

HomeFolder=""                                      - Invalid

Would you please let me know whether it is possible to assign such rules (for attribute) in XSD or not?

Who is Participating?
Geert BormansConnect With a Mentor Information ArchitectCommented:
you are incorrect.
The regex will NOT accept "      ",
because after a series of spaces, at least ONE non-whitespace character is required,
that is what [^\s] does (this means one none whitte-space character)
Have you tested?
I did, and it seems to work with all your examples, as you asked
Geert BormansInformation ArchitectCommented:
For me the most flexible way of dealing with this is by restricting the content of the attribute (a named simple type in this case) with a pattern.
The regular expression in the pattern would then be a series of zero or more whitespace. One non whitespace character and then anything
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Roles">
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="Role">
                        <xs:extension base="xs:string">
                            <xs:attribute name="HomeFolder" type="myNotEmptyStringType"/>
 <xs:simpleType name="myNotEmptyStringType">
     <xs:restriction base="xs:string">
         <xs:pattern value="\s*[^\s].*"></xs:pattern>

Open in new window

dhiraj79Author Commented:
Hi Gertone,
Thanks for your reply, but there is a problem
  This is for XSD problem
  the regex you give will accept following:
  "       "
  it will accept string which has only white space characters....
  this is what i dont want to accept
I do not want to accept "" or " " or "     " etc
ZvonkoSystems architectCommented:
Is meta character \S not defined in XSD regexp pattern?
Geert BormansInformation ArchitectCommented:
to answer zvonko's comment, yes it is. I have a tendency to build character classes the way I did, rather than using \S or other negative build in character classes... just too lazy to change old habbits I assume :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.