Reputation: 67
I am trying to validate my XML against an XSD which is imported in another XSD.
Please have a look at the following code:
xml1.xml:
<e1 xmlns="n1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="n2 main.xsd">
</e1>
xsd1.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="n1"
elementFormDefault="qualified">
<xsd:element name="e1"/>
</xsd:schema>
main.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="n2"
elementFormDefault="qualified">
<xsd:import namespace = "n1" schemaLocation = "xsd1.xsd"/>
</xsd:schema>
Validating xml1.xml (in netbeans with xml tools or in java) simply throws:
cvc-elt.1: Cannot find the declaration of element 'e1'. [3]
What could be the problem?
Upvotes: 2
Views: 518
Reputation: 111521
Your model of xsi:schemaLocation
requires adjustment. It is not an include statement that automatically brings XSDs into scope; it is a hint that says for a given namespace where validation might be able to find an appropriate XSD.
When you declare, xsi:schemaLocation="n2 main.xsd"
, you're hinting to look in main.xsd for the n2
namespace, but you've said nothing about the n1
namespace actually associated with your root element. Validation encounters the root element in the n1
namespace, consults your hints, finds nothing, and let's you know:
cvc-elt.1: Cannot find the declaration of element 'e1'. [3]
as it should. Your path forward is clear: Add a hint for the n1
namespace of your root element:
xsi:schemaLocation="n2 main.xsd n1 xsd1.xsd"
and your XML document will validate successfully.
Upvotes: 2