When I try to validate the XML file I am given this error:
person.xml:3: element person: Schemas validity error : Element
{http://www.namespace.org/person}person
: No matching global declaration available for the validation root.
This is the content of the person.xml file:
<?xml version="1.0"?>
<p:person xmlns:p="http://www.namespace.org/person">
<firstName>name</firstName>
<surName>surname</surName>
<secondName>n2</secondName>
</p:person>
This is the content of the person.xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://www.namespace.org/person"
version="1.0">
<xs:element name="person">
<xs:complexType>
<xs:group ref="credential"/>
</xs:complexType>
</xs:element>
<xs:group name="credential">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="surName" type="xs:string"/>
<xs:element name="secondName" type="xs:string"/>
</xs:sequence>
</xs:group>
</xs:schema>
The error indicates that the namespaced element, {http://www.namespace.org/person}person
, is not found in the given XSD because the p
element there is not in the http://www.namespace.org/person
namespace. Correct the problem by adding targetNamespace="http://www.namespace.org/person"
to the xs:schema
root element of the XSD. Next, adjust the reference to the credential
group to also use that namespace.
Altogether, the following XML will be valid against the following XSD:
<?xml version="1.0"?>
<p:person xmlns:p="http://www.namespace.org/person">
<firstName>name</firstName>
<surName>surname</surName>
<secondName>n2</secondName>
</p:person>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://www.namespace.org/person"
targetNamespace="http://www.namespace.org/person"
version="1.0">
<xs:element name="person">
<xs:complexType>
<xs:group ref="p:credential"/>
</xs:complexType>
</xs:element>
<xs:group name="credential">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="surName" type="xs:string"/>
<xs:element name="secondName" type="xs:string"/>
</xs:sequence>
</xs:group>
</xs:schema>