Re: Simple question on elements constraints

Adalbert Wysocki wrote:

> Hi all,
> I have a very simple question but it better to ask experienced people
> rather than write dirty thinks.
> The context is the validation of XML documents using XMLSchema.
> How can I specify that an element must have a fixed value and cannot
> have an empty one.
>
> example:
> -------
> <?xml version="1.0"?>
> <contract>
>   <id></id>
> </contract>
>
> This document is not valid because the element 'id' is empty whereas
> it should be filled with a value and this value equals for example
> '3000'.
>
> I tryed following:
> .....
> <xsd:element name="id" type="noEmptyElement" fixed="3000"/>
> ....
> <xsd:simpleType name="noEmptyElement">
>   <xsd:restriction base="xsd:string">
>     <minLength value="1"/>
>   </xsd:restriction>
> </xsd:simpleType>
> ...

Quoting from Structures 3.3
"If fixed is specified, then the element's content must either be empty,
in which case fixed behaves as default, or it must match the supplied
constraint string"

So IMO, it comes down to what is -
<id></id>
If this is an _empty_string_ but not an _empty_content_, then this
instance should fail your schema and the validator should pick this up
(as you want it to).  I believe that this is the case as in order to
specify an _empty_content_ one should use the xsi:null="true" in the
instance.  In which case, if you want to have instances being forced to
write "3000" and not allowing _empty_content_, just have
<xsd:element name="id" type="xsd:string" nullable="false" fixed="3000"/>

or, using defaults, just
<xsd:element name="id" fixed="3000"/>

However, this is just my interpretation of the quote above "element's
content must either be empty,".  There are a few ways this could be
interpreted

1. If this is interpreted as "empty content"  then your schema is okay
(as explained above) and maybe you should check your validator.

2. If this is interpreted as "empty string", then having <id></id> would
be treated as an _allowed_ value which then defaults to "3000", which is
not what you want.  In this case I agree with Morris' suggestion of
making a pattern restriction.

3. If this is interpreted as {content type}="empty" as defined in your
schema (as opposed to case 1 which is empty content in the instance) ,
then again your schema is okay as this clause is not used.  Ie by
specifying a simpleType, {content type}="noEmptyElement" and so the
content is _not_ empty, which _requires_ instances to have
<id>3000</id>.  Again, if this is the case, then your schema does what
you want it to and you should check your validator.

This analysis assumes that <id></id> is an empty string and not an empty
content.  But a Note in the specs implies otherwise - Structures 5.11
"To restrict a complex type definition with a simple base type
definition to empty, use a simple type definition with a fixed value of
the empty string: this preserves the type information"
This implies fixed="" is equivalent to empty???
If my previous assumption is incorrect, can someone correct me please.
And also explain how one can then specify an empty string in the
instance as being unique from an empty content.

cheers,
mick.

Received on Tuesday, 23 January 2001 21:09:09 UTC