开发者

Make a second element on XSD required when a first element is used

I have a simple XML file, with 3 basic elements. The first one is required (minOccurs=1) and the second and third are optional. However, if the second element is used, then I would like the make the third one required. He's the XML:

<?xml version="1.0" encoding="utf-8" ?>
<resultado>
        <consulta fonte="1" origem="Origem">0</consulta>
        <status resultado="REGULAR">0</status>
        <informacoes>
                <informacao tipo="CPF" dado="23340058865"></informacao>
                <informacao tipo="Nome" dado="Nome Titular CPF"></informacao>
                &开发者_如何学Golt;informacao tipo="Endereco" dado="Rua Sem Fim, 48"></informacao>
                <informacao tipo="Cidade" dado="Fim do Mundo"></informacao>
                <informacao tipo="Estado" dado="TO"></informacao>
                <informacao tipo="Cep" dado="93847832"></informacao>
                <informacao tipo="Fone" dado="5938476354"></informacao>
        </informacoes>
</resultado>

If the status element is passed, then informacoes becomes a required element.

Below is my XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="codigoresultado">
        <xs:restriction base="xs:unsignedByte">
            <xs:enumeration value="0"/>
            <xs:enumeration value="1"/>
            <xs:enumeration value="2"/>
            <xs:enumeration value="3"/>
            <xs:enumeration value="4"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="resultadoconsulta">
        <xs:restriction base="xs:unsignedByte">
            <xs:enumeration value="0"/>
            <xs:enumeration value="1"/>
            <xs:enumeration value="2"/>
            <xs:enumeration value="3"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="resultado">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="consulta" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="resultadoconsulta">
                                <xs:attribute name="fonte" type="xs:integer" use="required" />
                                <xs:attribute name="origem" type="xs:string" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="status" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="codigoresultado">
                                <xs:attribute name="resultado" type="xs:string" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="informacoes" minOccurs="0" >
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="1" maxOccurs="unbounded" name="informacao">
                                <xs:complexType>
                                    <xs:attribute name="tipo" type="xs:string" use="required" />
                                    <xs:attribute name="dado" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Is that possible? If it is, how? Thanks!


The only way I would see is to bundle up these two elements into a <xs:sequence> of their own which would be optional.

If it's present, then both elements have to be present (or both can be left out).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜