I have the following xml which has xsd schema, but is poor and no use for serialization.
<rulestruct>
<rule>
<type name="vector" />
<ruleident ruleidentifier="" />
<pattern type="" />
</rule>
<rule>
<type name="expression" />
<ruleident ruleidentifier="" />
<pattern type="" />
</rule>
<rule>
<type name="vector" />
<ruleident ruleidentifier="" />
<pattern type="" />
</rule>
<rule>
<type name="statement" />
<ruleident ruleidentifier="" />
<pattern type="" />
</rule>
<rule>
<type name="statement" />
<ruleident ruleidentifier="" />
<pattern type="" />
</rule>
</rulestruct>
Each rulestruct can have 1.N rules. Each rule can be repeated. Order must be preseved. Each rule has 1.N elements, some have 9 elements, others have 10, 13. There are 9 different rule types.
I was thinking of using a elements group to represent each rule type 开发者_高级运维but i'm not quite sure how to map it.
If you want to preserve the same name of element (rule) for various rule types, you defined abstract element (attribute abstract="true"
) and all of its children will have xs:ComplextContent>xs:extension
with base attribute equal to your abstract type name.
In your XML each rule element must have xsi:type
attrite to distinguish element concrete type.
Explanation and example are here.
If you want/can use different element names for each rule type, you can use substitution groups. You common ancestor is again defined by attribute abstract. The concrete types are defined as xs:element with attribute substitutionGroup
equal to name of common ancestor.
Explanation and example are here.
you use jaxb ? with xsd you can use the inheritance and you can play with minOccurs/maxOccurs if elements are needed or not.
Here an example, i have write it from scratch without testing it, perhaps there are errors :
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<xsd:annotation>
<xsd:appinfo>
<jaxb:globalBindings>
<xjc:simple />
</jaxb:globalBindings>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="rulestruct" type="PRuleStruct" />
<xsd:complexType name="PRuleStruct">
<xsd:sequence>
<xsd:element name="rule" type="PRule" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PRule">
<xsd:sequence>
<xsd:element name="vector" type="PVector" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
You have seen there is often many solutions for your problem. My choice is often to use the inheritance and less markers possible and to play with the name. Here my purpose:
<rules>
<ruleVector ruleidentifier="sample" patternType="sample">
</ruleVector>
精彩评论