XML 架构需要字符串或元素

XML Schema require string or elements

很抱歉,如果这个问题已经得到回答,我只是没有搜索正确的术语,但是有没有办法创建一个 XML 架构,其父元素需要一个字符串值,或者如果不是目前,2 个子元素 childAchildB

我希望以下结果有效

<myParent>This is my string</myParent>

<myParent>
    <childA>Child A string</childA>
    <childB>Child B string</childB>
</myParent>

您可以使用 mixed='true' attribute of xs:complexType 实现此目的。这会在 XSD-1.0.

中启用验证

因此您可以使用以下 XSD-1.0 代码:

<xs:element minOccurs="1" maxOccurs="unbounded" name="myParent">                                                  
    <xs:complexType mixed="true">
        <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="childA" type="xs:string" />
                <xs:element name="childB" type="xs:string" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:element>

这将验证您的上述 XML。它匹配所有 xs:string children 和 childAchildB children.

更具体地说,您可能必须使用 XSD-1.1.