The ref attribute of an element in the xml schema document

I'm doing an xml xs file generator, but I'm in doubt if some parts should be filled.

<xs:element name="identificacao">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="modelo"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="prestador">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="identificacao"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="solicitacao">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="prestador" minOccurs="1" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

The model has to be present and cannot be repeated.

Should the elements provider and request have the attribute ref= filled?

For example:

  • 1 form for name = template.

  • 1 form for ref = identification.

  • As many forms as you want for ref=provider.

Is that right?

Author: PeerBr, 2015-02-27

1 answers

Your XSD validates the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<solicitacao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Users/PeerBr/Desktop/test.xsd">
    <prestador>
        <identificacao>
            <modelo/>
        </identificacao>
    </prestador>
    <prestador>
        <identificacao>
            <modelo/>
        </identificacao>
    </prestador>
</solicitacao>

Summarizing:

  • one request per file
  • each request must have at least one provider
  • each provider must have a unique ID
  • each ID must have a single model.

Yes, the attribute ref you need in this case. It designates that you refer to a complex type - that way, you can use that complex type in another part of your XSD without having to write from new. If you don't need to take advantage of it, you can do the following to avoid ref:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="solicitacao">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="prestador" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="identificacao">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="modelo"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Both ways validate equally.

 2
Author: PeerBr, 2015-04-05 18:58:59