关于调查回复XML中同名不同类型元素的技术咨询
It sounds like your XML structure (likely in the responses section you didn’t fully share) has elements with the same name but holding different data types—for example, a <response> element that might contain a date (for question 1), a string (questions 2/3), and an integer (question 4). XML validators and XSD schemas flag this because they expect consistent typing for elements sharing the same name in the same scope.
Here are practical, tailored solutions for your survey use case:
1. Add a Type Attribute to Differentiate Responses
Keep the generic element name but add an explicit attribute to define the data type. This keeps your XML concise while resolving the type conflict.
Example XML:
<survey> <questions> <question id="1">When did you start playing Pokemon Go?</question> <question id="2">What type of phone/OS do you play on?</question> <question id="3">What team are you on?</question> <question id="4">What Level Are You?</question> </questions> <responses> <response question-id="1" type="date">2016-07-06</response> <response question-id="2" type="string">iOS</response> <response question-id="3" type="string">Valor</response> <response question-id="4" type="integer">35</response> </responses> </survey>
Corresponding XSD Snippet:
<xs:element name="response"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="question-id" type="xs:int" use="required"/> <xs:attribute name="type" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
For stricter validation, define the type attribute as an enumeration of allowed values (date, string, integer).
2. Use Specific Element Names per Response Type
Replace the generic <response> element with named elements that match the data type. This makes your XML self-documenting and eliminates type ambiguity entirely.
Example XML:
<survey> <questions> <!-- ... your existing questions ... --> </questions> <responses> <date-response question-id="1">2016-07-06</date-response> <string-response question-id="2">iOS</string-response> <string-response question-id="3">Valor</string-response> <integer-response question-id="4">35</integer-response> </responses> </survey>
Corresponding XSD Snippet:
<xs:element name="date-response"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:date"> <xs:attribute name="question-id" type="xs:int" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="string-response"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="question-id" type="xs:int" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="integer-response"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:int"> <xs:attribute name="question-id" type="xs:int" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
3. Use a Union Type (XSD 1.0)
If you want to keep the same element name without attributes, define a union type that allows all possible data types for the response. This is simpler but less strict—any response can be any of the allowed types.
XSD Snippet:
<xs:element name="response"> <xs:complexType> <xs:simpleContent> <xs:extension base="ResponseType"> <xs:attribute name="question-id" type="xs:int" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:simpleType name="ResponseType"> <xs:union memberTypes="xs:date xs:string xs:int"/> </xs:simpleType>
4. Conditional Type Assignment (XSD 1.1)
For precise validation (ensuring the correct type is used for each question ID), use XSD 1.1's conditional type assignment. This lets you map specific types to specific question-id values.
XSD Snippet:
<xs:element name="response"> <xs:alternative test="@question-id='1'" type="DateResponseType"/> <xs:alternative test="@question-id='2' or @question-id='3'" type="StringResponseType"/> <xs:alternative test="@question-id='4'" type="IntegerResponseType"/> </xs:element> <xs:complexType name="DateResponseType"> <xs:simpleContent> <xs:extension base="xs:date"> <xs:attribute name="question-id" type="xs:int" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> <!-- Define StringResponseType and IntegerResponseType similarly -->
Choose the solution that best fits your needs: option 2 is most explicit, option 1 balances conciseness and clarity, and option 4 offers the strictest validation if you’re using XSD 1.1.
内容的提问来源于stack exchange,提问作者screencut

