I would like to create XML Schema for this chunk of xml, I would like to restrict the values of "name" attribute, so that in output document on and only one instance of day is allowed for each week day:
我想为这个xml块创建XML Schema,我想限制“name”属性的值,这样在输出文档中,每个星期都只允许一天的实例:
<a>
<day name="monday" />
<day name="tuesday" />
<day name="wednesday" />
</a>
I have tried to use this:
我试过用这个:
<xs:complexType name="a">
<xs:sequence>
<xs:element name="day" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="monday" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="day" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tuesday" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
but XML Schema validator in eclipse says error "Multiple elements with name 'day', with different types, appear in the model group.".
但是eclipse中的XML Schema验证器说错误“具有不同类型的名称'day'的多个元素出现在模型组中。”
Is there any other way?
还有别的办法吗?
5 个解决方案
#1
12
You need something like this:
你需要这样的东西:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="day"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="day">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="monday"/>
<xs:enumeration value="tuesday"/>
<xs:enumeration value="wednesday"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
#2
7
In order to satisfy the at-most-once constraint described in the original question, you need to use a xs:unique element in your schema.
为了满足原始问题中描述的最多一次约束,您需要在架构中使用xs:unique元素。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="day" maxOccurs="7" minOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(mon|tues|wednes|thurs|fri|satur|sun)day"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:unique name="eachDayAtMostOnce">
<xs:selector xpath="day"/>
<xs:field xpath="@name"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
#3
0
Try this:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="day">
<xs:complexType>
<xs:attribute name="atrib" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
I think the maxOccurs="unbounded" is what you need.
我认为maxOccurs =“unbounded”就是你所需要的。
#4
0
Have you considered something like this? (i.e. day is a type, and the elements are named after the days of the week)
你考虑过这样的事吗? (即日是一种类型,元素以一周中的日期命名)
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="day" />
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="monday" maxOccurs="1" minOccurs="0" type="day" />
<xs:element name="tuesday" maxOccurs="1" minOccurs="0" type="day" />
<xs:element name="wednesday" maxOccurs="1" minOccurs="0" type="day" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
#5
0
Use choice instead of sequence
使用选择而不是序列
By that way u can have only day element one's in a element
通过这种方式,你可以在元素中只有一天元素
And for name attribute use ref attribute and reference it with enumeration
并且对于name属性使用ref属性并使用枚举引用它
#1
12
You need something like this:
你需要这样的东西:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="day"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="day">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="monday"/>
<xs:enumeration value="tuesday"/>
<xs:enumeration value="wednesday"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
#2
7
In order to satisfy the at-most-once constraint described in the original question, you need to use a xs:unique element in your schema.
为了满足原始问题中描述的最多一次约束,您需要在架构中使用xs:unique元素。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="day" maxOccurs="7" minOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(mon|tues|wednes|thurs|fri|satur|sun)day"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:unique name="eachDayAtMostOnce">
<xs:selector xpath="day"/>
<xs:field xpath="@name"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
#3
0
Try this:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="day">
<xs:complexType>
<xs:attribute name="atrib" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
I think the maxOccurs="unbounded" is what you need.
我认为maxOccurs =“unbounded”就是你所需要的。
#4
0
Have you considered something like this? (i.e. day is a type, and the elements are named after the days of the week)
你考虑过这样的事吗? (即日是一种类型,元素以一周中的日期命名)
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="day" />
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="monday" maxOccurs="1" minOccurs="0" type="day" />
<xs:element name="tuesday" maxOccurs="1" minOccurs="0" type="day" />
<xs:element name="wednesday" maxOccurs="1" minOccurs="0" type="day" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
#5
0
Use choice instead of sequence
使用选择而不是序列
By that way u can have only day element one's in a element
通过这种方式,你可以在元素中只有一天元素
And for name attribute use ref attribute and reference it with enumeration
并且对于name属性使用ref属性并使用枚举引用它