1

I'm working on an XSD file and when validating an XML file with it I want to restrict empty elements. Eg.,

<myElement>this is not empty</myElement>

would pass validation, however

<myElement/> or <myElement></myElement>

would not pass validation.

Thanks for any help you may offer!

ps. I accidentally posted this at https://stackoverflow.com/questions/4126546/prevent-empty-elements-in-xml-via-xsd under an unregistered account. I apologize for any inconveniences.

UPDATE: The element must be able to not exist in the XML at all, so I can not use minOccurs="1".

Community
  • 1
  • 1
hmcclungiii
  • 1,765
  • 2
  • 16
  • 27

2 Answers2

4

You can do something like

<xs:simpleType name="myString">
  <xs:restriction base="xs:string">
     <xs:minLength value="1"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="root" type="myString"></xs:element>

However that will still match on <root> </root> but you could use a regex restriction to change that if it is an issue.

tyranid
  • 13,028
  • 1
  • 32
  • 34
0

You can have minOccurs="0" to require the tag, then have a validation regex that checks for one or more characters.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • The element must be able to not exist in the XML at all, so I can not use minOccurs="1". Thanks for the help though! I've updated the OP to reflect this. – hmcclungiii Nov 08 '10 at 18:25
  • See change - make it minOccurs='0' in that case. The rest still applies. – duffymo Nov 08 '10 at 20:52