问题
i write about problem that i have but now i show all code xml file
<?xml version="1.0"?>
<Purchase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://NamespaceTest.com/Purchase Main.xsd"
xmlns="http://NamespaceTest.com/Purchase">
<element style="ide">it can't contain other elements</element>
<element style="rem">it can contain some other <subelement>elements</subelement></element>
<element style="rem"> this style can contain other<subelement> elements</subelement></element>
</Purchase>
and Main.xsd file
<?xml version="1.0" encoding="utf-16"?>
<xs:schema elementFormDefault="qualified"
targetNamespace="http://NamespaceTest.com/Purchase"
xmlns:pur="http://NamespaceTest.com/Purchase"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Purchase">
<xs:complexType>
<xs:sequence>
<xs:group ref="pur:ide_group" maxOccurs="1"/>
<xs:group ref="pur:rem_group" maxOccurs="2" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="ide_group">
<xs:sequence>
<xs:element name="element" type="pur:ide_type"/>
</xs:sequence>
</xs:group>
<xs:complexType name="ide_type" mixed="true">
<xs:attribute name="style" type="pur:ide_list"/>
</xs:complexType>
<xs:simpleType name="ide_list">
<xs:restriction base="xs:token">
<xs:enumeration value="ide"/>
</xs:restriction>
</xs:simpleType>
<xs:group name="rem_group">
<xs:sequence>
<xs:element name="element" type="pur:rem_type"/>
</xs:sequence>
</xs:group>
<xs:complexType name="rem_type" mixed="true">
<xs:sequence>
<xs:element name="subelement"/>
</xs:sequence>
<xs:attribute name="style" type="pur:rem_list"/>
</xs:complexType>
<xs:simpleType name="rem_list">
<xs:restriction base="xs:token">
<xs:enumeration value="rem"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
i have error: cos-element-consistent: Error for type '#AnonType_Purchase'. Multiple elements with name 'element', with different types, appear in the model group. my problem is that xml file have elements with same name but different style, and i need make rule that depend on style of element.
回答1:
You have a few options:
If you can use XSD 1.1, you may be able to use either conditional type assignment or assertions to impose distinct constraints on sibling elements with the same name and declared type.
You can modify the design of your XML to use different element type names for the different element types you have. (Clearly they are different element types: you want to assign different types to them. Why call them by the same name, given that they are different?)
What you cannot do, with XSD, is declare sibling elements of the same name with different types; the language requires that the declared type of any element be determinable by considering just the path from the validation root to the element.
来源:https://stackoverflow.com/questions/26524614/multiple-elements-with-same-name-with-different-types-appear-in-the-model-gro