xmlxsdxsd-validation

Error validating XML file: Error resolving component in XML schema


I am having trouble validating this XML file. I've looked up online to similar threads but I didn't find anything that could help me. I receive the following error when I try to valide my xml file:

'Error:(17, 69) src-resolve.4.2: Error resolving component 'vi:visitaType'. It was detected that 'vi:visitaType' is in namespace 'http://www.clinica/visita', but components from this namespace are not referenceable from schema document 'file:/C:/Users/studio/Documents/psw_esercitazioni%20personali/xml%20schema/testEsame1s/clinica.xsd'. If this is the incorrect namespace, perhaps the prefix of 'vi:visitaType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/C:/Users/studio/Documents/psw_esercitazioni%20personali/xml%20schema/testEsame1s/clinica.xsd'.

This is the XML file I'm trying to validate:

<?xml version="1.0" encoding="UTF-8" ?>
<cli:clinica xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:cli="http://www.clinica/clinica"
              xmlns:vi="http://www.clinica/visita"
              xmlns:an="http://www.clinica/animale"
              xmlns:pe="http://www.clinica/persona"
              xsd:schemaLocation="http://www.clinica/visita clinica.xsd">
    <giorno>2023-06-14</giorno>
    <visiteGiorno>
        <visitaSingola>
            <idVisita>33</idVisita>
            <costo>33</costo>
            <proprietario>
                <nome>giorgio</nome>
                <datiBancari>test</datiBancari>
                <indirizzo>test</indirizzo>
            </proprietario>
            <animale>
                <specie>test</specie>
                <sesso>test</sesso>
                <numero>33</numero>
            </animale>
        </visitaSingola>
    </visiteGiorno>

</cli:clinica>

And this is the XML schema that contain the line " <xsd:element name="visitaSingola" type="vi:visitaType"/>" that is causing the error:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://www.clinica/clinica"
        targetNamespace="http://www.clinica/clinica"
        xmlns:vi="http://www.clinica/visita">

    <xsd:element name="clinica" type="clinicaType"/>
    <xsd:complexType name="clinicaType">
        <xsd:sequence>
            <xsd:element name="giorno" type="xsd:date"/>
            <xsd:element name="visiteGiorno" type="vgType"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="vgType">
        <xsd:sequence>
            <xsd:element name="visitaSingola" type="vi:visitaType"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

How can I fix this error?

edit: Here is also the visita.xsd file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.clinica/visita"
            targetNamespace="http://www.clinica/visita"
            xmlns:pers="http://www.clinica/persona"
            xmlns:anim="http://www.clinica/animale">
    <xsd:import namespace="http://www.clinica/animale" schemaLocation="animale.xsd"/>
    <xsd:import namespace="http://www.clinica/persona" schemaLocation="persona.xsd"/>

    <xsd:element name="visita" type="visitaType"/>
    <xsd:complexType name="visitaType">
        <xsd:sequence>
            <xsd:element name="idVisita" type="xsd:string"/>
            <xsd:element name="costo" type="xsd:string"/>
            <xsd:element name="proprietario" type="pers:personaType"/>
            <xsd:element name="animale" type="anim:animaleType"/>
        </xsd:sequence>
    </xsd:complexType>


</xsd:schema>

Solution

  • The complex type clinicaType is in the target namespace of the schema so you need to qualify the reference: it should be

    <xsd:element name="clinica" type="ci:clinicaType"/>
    

    with the namespace declaration xmlns:ci="http://www.clinica/clinica"

    and similarly for the reference to vgType.

    The error message relates to type visitaType which you have referenced with a namespace prefix (type="vi:visitaType") but this type is defined in a schema module which you haven't imported. The clinica.xsd module can't reference types defined in visita.xsd unless it contains an xsd:import for visita.xsd.