views:

517

answers:

2

Is there a way to prevent empty elements of the form <myElement/> being used in your xml? In other words, can you specify in your xsd that <myElement/> is invalid?

Using nillable="false" doesn't work, nor does minOccurs="1" - both of those allow <myElement/>.

+2  A: 

If you're trying to prevent the element from appearing at all, you can mark it with minOccurs="0". I'm guessing this isn't what you're after, so if you're trying to make sure there are always attributes attached to the complex element, then you have to specify usage="required" on at least one of the attributes or use an attribute group. If myElement is a simple type and you want to make sure it has a value, then you could always restrict the type of it. If you want a non-zero string, then you could do:

<xsd:element name="myElement">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>
Travis Gockel
That looks promising. What if it's not a simple type, e.g. "xsd:date"
Chris Welsh
`xsd:date` is a simple type. But if it's not a simple type, you can use `<xsd:complexType>` . Although, with those, I typically use the `type` attribute of `xsd:element` i.e.: `<xsd:element name="myElement" type="someComplexType" />`
Travis Gockel
So you're saying `minLength` is a valid property for non-string simple types?
Chris Welsh
No, but if you've specified the type as an `xsd:date` , then an empty element is not acceptable, as it is for almost all types.
Travis Gockel
Well, that's my point - as far as I'm concerned, an empty value for an element defined as `xsd:date` should be invalid, but none of the tools I'm using seem to support that interpretation. I guess I need better tools.
Chris Welsh
+1  A: 

If your schema-validation isn't able to show error, when an Element of data-type DATE is null, then you can use a pattern [if it isn't a burden for you to type the required format];

I have added an example, implementation of similar code would work on your tool,

This is the sample XML:

<root>
   <date1>12/31/1999</date1> <!-- The Date format defined here is MM/DD/YYYY, null value or Date with any other format aren't accepted-->
</root>

This is the corresponding XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:include schemaLocation="Date_Def.xsd"/>
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="date1" type="DATE_TYPE" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Observe that I am including one more schema file which includes the definition of type DATE_TYPE,
Here is the Date_Def.xsd file:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:simpleType name="DATE_TYPE">
    <xs:restriction base="xs:string">
      <xs:pattern value="([0][1-9]|[1][0-2])/([0][1-9]|[1-2][0-9]|[3][0-1])/[1-2][0-9][0-9][0-9]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

The Date format defined here is MM/DD/YYYY, null value or Date with any other format aren't accepted, If you want to accept also a null tag, the replace pattern with this ..

<xs:pattern value="|(([0][1-9]|[1][0-2])/([0][1-9]|[1-2][0-9]|[3][0-1])/[1-2][0-9][0-9][0-9])"/>

Validation of which accepts, either null tag or a Date-value of pattern MM/DD/YYYY.

If you need more help on design of patterns, then feel free to make it a post in SO, hope it helped. :-)

[note :: The Type-Definition can also be defined in a same file, which needs additional name-spaces mentioned in XML as well as XSD files, defining an external file is harmless and re-usable]

infant programmer
Thanks for the detailed response. I managed to find tools that did the validation correctly. This pattern stuff is useful, tho.
Chris Welsh
@Chris, np[15 chars]
infant programmer