开发者

XSD validation error using choices

开发者 https://www.devze.com 2023-03-27 02:23 出处:网络
I need either element A or B or both.If i use choices, then it throws an exception Element \'A\' cannot have character [children], because the type\'s content type is element-only.

I need either element A or B or both. If i use choices, then it throws an exception Element 'A' cannot have character [children], because the type's content type is element-only. How to achieve the desired result.

        <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="A">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="C"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="B"/>
         </xsd:sequence>
       </xsd:complexType>

sample XML is

         <start>
            <A>
                <C>hhg</C>
            </A>
        </start>
        <start>
            <A>
                <C>开发者_运维百科;hhg</C>
            </A>
            <B>fgeg</B>
        </start>
        <start>
            <B>fergf</B>
        </start>


Use minOccurs="0", e.g.:

<xs:element name="A" minOccurs="0">
...
<xs:element name="B" minOccurs="0"/>

For XML:

<root>
    <start>
        <A>
            <C>hhg</C>
        </A>
    </start>
    <start>
        <A>
            <C>hhg</C>
        </A>
        <B>fgeg</B>
    </start>
    <start>
        <B>fergf</B>
    </start>
</root>

Appropriate XSD should be:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="start">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="0" name="A">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="C" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element minOccurs="0" name="B" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="test">
    <xs:choice>
        <xs:element name="A">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="C"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="B"/>
    </xs:choice>
</xs:complexType>
</xs:schema>

This is validated ok in Oxygen ...

0

精彩评论

暂无评论...
验证码 换一张
取 消