问题
I try to define a complex type for a selection that can consist of different type of entries but only one entry is allowed to have an attribute "multiselect".
Here is what I tried:
<element name="selection" minOccurs="0" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="name" type="string" />
<element name="source">
<complexType>
<choice>
<element name="item" minOccurs="1" maxOccurs="unbounded" type="string" />
<element name="path" type="string" minOccurs="1" maxOccurs="1" />
</choice>
</complexType>
</element>
</sequence>
<attribute name="multiselection" type="boolean" minOccurs="1" maxOccurs="1" />
</complexType>
</element>
The result should be that there can be more elements of "selection" where it doesn't matter if the source is of type "item" or of type "path". But only one of the "selection" elements is allowed to to have the attribute multiselection = true.
But as it seems there is no min-/maxOccures for attributes. How can I workaround this ?
Thx
回答1:
First of all, min/maxOccurs is reserved for particles (local elements, element references, group references, sequence, choice). An attribute occurrence is controlled by
use = (optional | prohibited | required) - the default value is optional
To further constrain that among a set of elements, only one may have the attribute specified with a logical value of true (either a 1 or the literal true
) - this is something you cannot do with XSD 1.0 alone. You may use Schematron on top of XSD.
Alternatively, you can easily achieve this in XSD 1.1.
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sample">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="selection" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="source">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded" type="xsd:string"/>
<xsd:element name="path" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="multiselection" type="xsd:boolean" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:assert test="count(selection[@multiselection=true()])=1"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Something along these lines (both false or both true would fail the validation):
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<selection multiselection="false">
<name>name1</name>
<source>
<item>item1</item>
<item>item1</item>
</source>
</selection>
<selection multiselection="false">
<name>name1</name>
<source>
<item>item1</item>
<item>item1</item>
</source>
</selection>
</sample>
cvc-assertion.3.13.4.1: Assertion evaluation ('count(selection[@multiselection=true()])=1') for element 'sample' with type '#anonymous' did not succeed.
Making one of them true
should yield successful validation.
来源:https://stackoverflow.com/questions/15338186/xsd-restrict-element-occurrence-with-an-specific-attribute