29

This is my sample XML Code:

<bestContact>
<firstName><![CDATA[12345]]></firstName>
<lastName />
</bestContact>

I am using:

<xs:element name="lastName" type="xs:string" minOccurs="1" nillable="false"/>

The XSD Should be validate lastName as not null or empty.

6 Answers 6

61

Try

<xs:element name="lastName" minOccurs="1" nillable="false">
  <xs:simpleType>
     <xs:restriction base="xs:string">
       <xs:minLength value="1"/>
     </xs:restriction>
  </xs:simpleType>
</xs:element>
Sign up to request clarification or add additional context in comments.

1 Comment

Using this I get "Element 'lastName' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element." I think there is a problem on using type="xs:string" in the container element.
22
<xsd:element name="lastName" type="NonEmptyString" nillable="false"/>

<xsd:simpleType name="NonEmptyString">
  <xsd:restriction base="xs:string">
    <xsd:minLength value="1" />
    <xsd:pattern value=".*[^\s].*" />
  </xsd:restriction>
</xsd:simpleType>

2 Comments

I like this better than the 2 offered by Gab, this one works for me.Since I was looking for an expression that just got rid of the leading spaces, not the spaces in between text
I think you can just replace this regex by \S+ as Gab pointed out.
11

This is IMHO a better pattern:

<xs:simpleType name="NonEmptyString">
   <xs:restriction base="xs:string">
      <xs:pattern value="^(?!\s*$).+" />
   </xs:restriction>
</xs:simpleType>

or

<xs:simpleType name="NonEmptyStringWithoutSpace">
   <xs:restriction base="xs:string">
      <xs:pattern value="\S+"/>
   </xs:restriction>
</xs:simpleType>

4 Comments

is there any advantage to your first proposal over the second one?
Well a (non empty) blanck string will be allowed by the 1st one but not the 2nd. It just depends on your needs
how can i have a <xs:attribute name="" type="MDFS" use="optional" /> optional, pattern <xs:simpleType name="MDFS"> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z]\d{3}" /> </xs:restriction> </xs:simpleType>, since it is optional, having a type, it can not be null !!!! is it possible being null having a type pattern ?
(? will lead to an invalid xsd so the 1st solution will not work
9

@Kamal has given you basically right answer here. This is why - nillable always seems to cause problems. Effectively, you can consider nillable as meaning allow the xsi:nil attribute on this element. The XML Schema spec describes nillable as an out of band signal - it's basically used to indicate NULL to databases.

What you want is an element that must be at least one character long as given by @Kamal

Comments

0

This was my favourite solution.

<xs:simpleType name="NonEmptyString">
    <xs:restriction base="xs:string">
        <xs:pattern value="[\s\S]*[^ ][\s\S]*"/>
    </xs:restriction>
</xs:simpleType>

Comments

0

My 2 solutions:

  1. Use xs:token so leading & trailing spaces are ignored and a minimum length of 1:
  <xs:simpleType name="NonEmptyElementType">
    <xs:restriction base="xs:token">
      <xs:minLength value="1"/>
    </xs:restriction>
  </xs:simpleType>
  1. Use xs:string so leading & trailing spaces are permitted, and a simple regex to verify at least one non-whitespace character is present (in this solution the <xs:minLength value="1"/> is not necessary, but I like the readability of it for future maintainers.):
  <xs:simpleType name="NonEmptyElementType">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:pattern value=".*\S.*"/>
    </xs:restriction>
  </xs:simpleType>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.