Use of required and optional

L

LJ

I am trying to define two xml attribute in my xsd so that they are
mutual exclusive and one of them is required.

For example, if I have two attributes, they are either

<xsd:attribute name="p" type="xsd:string" use="required" />
<xsd:attribute name="c" type="xsd:string" use="optional" />
or

<xsd:attribute name="p" type="xsd:string" use="optional" />
<xsd:attribute name="c" type="xsd:string" use="required" />

how can I specify this in the xsd?

Thx
 
M

Martin Honnen

LJ said:
I am trying to define two xml attribute in my xsd so that they are
mutual exclusive and one of them is required.

For example, if I have two attributes, they are either

<xsd:attribute name="p" type="xsd:string" use="required" />
<xsd:attribute name="c" type="xsd:string" use="optional" />
or

<xsd:attribute name="p" type="xsd:string" use="optional" />
<xsd:attribute name="c" type="xsd:string" use="required" />

how can I specify this in the xsd?

XSD does not allow you to define such constraints between different
attributes.
 
S

Stan Kitsis [MSFT]

You have a few options.
1. If they are trully mutually exclusive, you might be able to only use one
of them
2. Create two attributes: type (which can be either p or c) and value (which
will be a string representing the value)
3. Convert your attributes to elements and use <xs:choice>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
G

George Bina

If you really want to keep the document structure and none of the above
alternatives solve your problem then you have also the following
possibilities:

A. Make both attributes optional and use Schematron embedded rules in
XML Schema to check that you have only one of them
B. Use Relax NG instead of XML Schema

Here it is a sample for case A. XML Schema with embedded Schematron
rules:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:element name="test">
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check that we have only one attribute.">
<sch:rule context="test">
<sch:assert test="count(@p|@c)=1">Ony one of the attributes
p and c can be
specified.</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:attribute name="p" type="xs:string" use="optional"/>
<xs:attribute name="c" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:schema>

on the document
<test c="1" p="2"/>

will give:
SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 1:0
Description: Ony one of the attributes p and c can be specified.
(count(@p|@c)=1)

And here it is a Relax NG sample schema:

<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="test">
<choice>
<attribute name="c">
<data type="string"/>
</attribute>
<attribute name="p">
<data type="string"/>
</attribute>
</choice>
</element>
</start>
</grammar>

And in compact syntax that is:

default namespace = ""

start =
element test {
attribute c { xsd:string }
| attribute p { xsd:string }
}


Best Regards,
George
 

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
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top