Search code examples
xmlxsdnamespaces

How to make a schema for an XML file with 2 namespaces


I would like to make a XSD for this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<filmes xmlns:acao="http://exemplo.com/acao"
        xmlns:comedia="http://exemplo.com/comedia"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="to be completed">

 <acao:filme>
    <acao:titulo>Velozes e Furiosos</acao:titulo>
    <acao:diretor>Rob Cohen</acao:diretor>
    <acao:atorPrincipal>Vin Diesel</acao:atorPrincipal>
    <acao:anoLancamento>2001</acao:anoLancamento>
  </acao:filme>

  <comedia:filme>
    <comedia:titulo>Se Beber, Não Case!</comedia:titulo>
    <comedia:diretor>Todd Phillips</comedia:diretor>
    <comedia:atorPrincipal>Bradley Cooper</comedia:atorPrincipal>
    <comedia:anoLancamento>2009</comedia:anoLancamento>
  </comedia:filme>

</filmes>

I have watched a possibility where I use a main XSD that imports a XSD for accao and another for comedia, but I am unable to do a schema that would validate this XML.


Solution

  • You may need to create separate XSDs for the "acao" and "comedia" namespaces and import them into the main XSD.

    First create a file named "acao.xsd" for the "acao" namespace:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://exemplo.com/acao"
               xmlns:acao="http://exemplo.com/acao">
    
      <xs:element name="filme">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="titulo" type="xs:string"/>
            <xs:element name="diretor" type="xs:string"/>
            <xs:element name="atorPrincipal" type="xs:string"/>
            <xs:element name="anoLancamento" type="xs:gYear"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    Then create a second one "comedia.xsd" for the "comedia" namespace:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://exemplo.com/comedia"
               xmlns:comedia="http://exemplo.com/comedia">
    
      <xs:element name="filme">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="titulo" type="xs:string"/>
            <xs:element name="diretor" type="xs:string"/>
            <xs:element name="atorPrincipal" type="xs:string"/>
            <xs:element name="anoLancamento" type="xs:gYear"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    Finally create the main XSD file ("filmes.xsd"):

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:acao="http://exemplo.com/acao"
               xmlns:comedia="http://exemplo.com/comedia"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               targetNamespace="http://exemplo.com/filmes"
               elementFormDefault="qualified">
    
      <!-- Import the acao and comedia namespaces -->
      <xs:import namespace="http://exemplo.com/acao" schemaLocation="acao.xsd"/>
      <xs:import namespace="http://exemplo.com/comedia" schemaLocation="comedia.xsd"/>
    
      <!-- Define the root element -->
      <xs:element name="filmes">
        <xs:complexType>
          <xs:sequence>
            <!-- Reference the elements from the imported namespaces -->
            <xs:element ref="acao:filme" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element ref="comedia:filme" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    The "ref" attribute is used to reference the elements from the imported XSDs.