【问题标题】:XML Schema require string or elementsXML Schema 需要字符串或元素
【发布时间】:2019-07-13 03:12:10
【问题描述】:

抱歉,如果这个问题已经得到解答,我只是没有搜索正确的术语,但有没有办法创建一个 XML 模式,其父元素需要一个字符串值,或者如果不存在,则需要 2 个子元素@ 987654322@和childB

我希望以下结果有效

<myParent>This is my string</myParent>

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

【问题讨论】:

标签: xml xsd


【解决方案1】:

您可以通过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 孩子和childAchildB 孩子。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2012-04-23
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多