Simple XML Schema problem

M

Maxim

If there is a document like this:

<Configurations>

<Configuration default="true">
...
</Configuration>

<Configuration>
...
</Configuration>

...

</Configurations>

How can I ensure with a schema that only 0 or 1 <Configuration>
element has the "default" attribute set to "true"?

I suppose this is a fairly common problem and someone somewhere must
have gone through this already. Appreciate any hints.

Thanks,

Maxim
 
M

Martin Honnen

Maxim said:
If there is a document like this:

<Configurations>

<Configuration default="true">
...
</Configuration>

<Configuration>
...
</Configuration>

...

</Configurations>

How can I ensure with a schema that only 0 or 1 <Configuration>
element has the "default" attribute set to "true"?

I suppose this is a fairly common problem and someone somewhere must
have gone through this already. Appreciate any hints.

You could define the type of the attribute as xs:boolean so that it can
only have the values true or false and then you could specify a
uniqueness constraint for <Configuration> elements regarding that attribute:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Configurations">
<xs:complexType>
<xs:sequence>
<xs:element ref="Configuration" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueConfigDefault">
<xs:selector xpath="Configuration" />
<xs:field xpath="@default" />
</xs:unique>
</xs:element>

<xs:element name="Configuration">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="default" type="xs:boolean" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

</xs:schema>

That way one <Configuration> can have the default="true" attribute and
others can simply not specify the attribute as you have in your example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top