XSD syntax for declaring element that cannot have a child element or other content

Go To StackoverFlow.com


Would this be the proper method of declaring an XML element Foo that cannot have a child element or other content?

<xs:element name="Foo" type="xs:string" fixed="" nillable="true" />

The valid examples of this element in an XML document would be:




Anything else would be invalid, e.g.:


It would be nice, though not essential, that the following be valid

2012-04-04 19:03
by Olumide


I combined a few options into a single schema and attempted to use it to validate some test elements.

Sources: http://www.w3schools.com/schema/schema_complex_empty.asp


The schema:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
      <xs:choice maxOccurs="unbounded">
        <xs:element name="foo">
        <xs:element name="y1">
            <xs:restriction base="xs:string">
              <xs:length value="0" />
        <xs:element name="y2">
              <xs:restriction base="xs:anyType" />

Test cases:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 <!--  These six are all valid -->
  <foo />
  <y1 />
  <y2 />
  <!-- These three are all invalid! -->
  <!--  These are invalid too. -->

It looks like any of the three element declarations would do what you want, except for the extra blank line feature.

I wouldn't include type="xs:string", fixed="", or nillable="true" because these are all semantically different from an empty tag. An empty tag wouldn't really have a type (like string) because it's empty. It also wouldn't have a fixed value of the null string, because that's different from being empty. A nillable element is semantically different from an empty tag because it is a semantically equivalent to the tag being absent (Source: XSLT 2.0 and XSLT 2.0 Programmer's Reference by Michael Kay, p. 182.). An empty tag like <br/> does not mean that, in this case, the line break is absent. It means that the line break is present but has no content or attriubtes.

2012-04-04 21:39
by k-den
Thanks! Now I'm trying to do the opposite i.e. create an element <Foo> that must contain a character string. BTW <Foo> has multiple attributes - Olumide 2012-04-10 13:16


There are two approaches to defining a type that enforces empty content: one is to use a simple type of xs:string which is constrained using an enumeration or pattern facet (or using fixed) to be zero-length; the other is to use a complex type defined as (say) an empty sequence of elements. For validation purposes, there's not much to choose between them; but if you're interested in doing more advanced things with your schema, like defining type hierarchies and substitution groups, or mapping the schema to a Java object model, it could make a difference which you choose.

2012-04-04 21:27
by Michael Kay


<xs:element name="Foo">
    <xs:attribute name="Bar" type="xs:positiveInteger"/>

You could leave out the attribute tag to leave it totally empty...

2012-04-04 19:08
by Mike Corcoran
I'm not sure it enforces the critical restrictions. <Foo> can contain no other elements or content - Olumide 2012-04-04 21:06