Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specifying combinations of attributes 1

Status
Not open for further replies.

Frink

Programmer
Mar 16, 2001
798
GB
Is it possible to specify multiple fixed combinations of attributes for an element?
i.e.
<xs:element name="parameters" type="action1"/>
or
<xs:element name="parameters" type="action2"/>

where:
<xs:complexType name="action1">
<xs:attribute name="duration" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="action2">
<xs:attribute name="start" type="xs:integer" use="required"/>
<xs:attribute name="finish" type="xs:integer" use="required"/>
</xs:complexType>

so the following is valid xml
<parameters duration="1 month"/>
<parameters start="12" finish="17"/>

but
<parameters start="12" duration="1 month"/>
is not.

- Frink
 
You won't be able to do that without some fundamental addition to the element appealing to the aid of XMLSchema-instance's type element. Like this.
[tt]
<root xmlns:xsi=" <parameters xsi:type="TYPE_DURATION" duration="1 month" />
<parameters xsi:type="TYPE_START" start="12" finish="1" />
</root>
[/tt]
The set of xsi:type is defined in the schema.
[tt]
<xs:schema id="co_occur" elementFormDefault="unqualified" xmlns:xs="<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="parameters" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="TYPE_DURATION">
<xs:complexContent>
<xs:extension base="TYPE_ABSTRACT">
<xs:attribute name="duration" type="xs:string" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="TYPE_START">
<xs:complexContent>
<xs:extension base="TYPE_ABSTRACT">
<xs:attribute name="start" type="xs:integer" use="required" />
<xs:attribute name="finish" type="xs:integer" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
[/tt]
If you're not in a position to make use of schemainstance type element, it is out-of-the-scope of xml schema as w3c sees it.
 
Further notes:

I had deviced a complex type that I called TYPE_ABSTRACT which is empty. That's why I did not post it there. The above should work fine, only that the reference to it in the extension might confused you. It is empty like this and you can just append it to the schema. It's purpose is to hold common attributes for different xsi type. As there is none, it is empty.
[tt]
<xs:complexType name="TYPE_ABSTRACT" abstract="true">
<!-- hold attributes common to other concrete type -->
</xs:complexType>
[/tt]
When one needs to appeal to this attribute holder, you can add the reference to it in the element. Like this in the sequence element.
[tt]
<xs:sequence>
<xs:element name="parameters" minOccurs="0" maxOccurs="unbounded" [blue]type="TYPE_ABSTRACT"[/blue] />
</xs:sequence>
[/tt]
 
Cheers tsuji, I'll have a look at your posts in a bit more detail in the next few days.

- Frink
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top