XML schema for elements with same name but different sub elements
What I'm looking to be able to create is one XSD which would validate both of the following:
<parent>
<mother>
<name>
<firstname>foo</firstname>
<surname> bar </surname>
<maidenname>rab</maidenname>
</name>
</mother>
</parent>
and
<parent>
<father>
<name>
<firstname>foo</firstname>
<surname> bar </surname>
</name>
</father>
</parent>
I ideally want to be able to use the same element name but have different requirements for it based on the parent attribute. What I've tried so far is:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="firstname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="maidenname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="mother">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="father">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name">
<xs:complexType>
<xs:all>
<xs:element ref="firstname" />
<xs:element ref="surname" />
<xs:element ref="maidenname" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="parent">
<xs:complexType>
<xs开发者_如何学Python:choice>
<xs:element ref="mother"/>
<xs:element ref="father"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="surname">
<xs:complexType mixed="true" />
</xs:element>
</xs:schema>
You can create two different types (e.g. fathersName
and mothersName
) and use them for the respective name
elements. Here's the full XSD:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="firstname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="maidenname">
<xs:complexType mixed="true" />
</xs:element>
<xs:element name="mother">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="mothersName" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="father">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="fathersName" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="fathersName">
<xs:all>
<xs:element ref="firstname" />
<xs:element ref="surname" />
</xs:all>
</xs:complexType>
<xs:complexType name="mothersName">
<xs:all>
<xs:element ref="firstname" />
<xs:element ref="surname" />
<xs:element ref="maidenname" />
</xs:all>
</xs:complexType>
<xs:element name="parent">
<xs:complexType>
<xs:choice>
<xs:element ref="mother"/>
<xs:element ref="father"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="surname">
<xs:complexType mixed="true" />
</xs:element>
</xs:schema>
Also, if the names are supposed to be simple strings, you could write the XSD in a more concise way:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mother">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="mothersName" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="father">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="fathersName" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="fathersName">
<xs:all>
<xs:element name="firstname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
</xs:all>
</xs:complexType>
<xs:complexType name="mothersName">
<xs:all>
<xs:element name="firstname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
<xs:element name="maidenname" type="xs:string" />
</xs:all>
</xs:complexType>
<xs:element name="parent">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="mother"/>
<xs:element ref="father"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Finally, you may write mothersName
as an extension of fathersName
, but this comes with a cost: the order of elements has to be (at least partially) fixed. Here's how the two type definitions would look:
<!-- ... -->
<xs:complexType name="fathersName">
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="mothersName">
<xs:complexContent>
<xs:extension base="fathersName">
<xs:sequence>
<xs:element name="maidenname" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- ... -->
精彩评论