When parsing some foreign XML file with Xerces and with given XSD, I receive an error that tag is not declared.
The tag is declared as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://alicebot.org/2001/AIML-1.0.1" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron"
targetNamespace="http://alicebot.org/2001/AIML-1.0.1" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.0" xml:lang="EN">
<xs:element name="aiml">
<xs:annotation>
<xs:documentation>An AIML object is represented by an aiml element in an XML document.</xs:documentation>
<xs:appinfo>
<sch:title>Schematron validation</sch:title>
<sch:ns prefix="aiml" uri="http://alicebot.org/2001/AIML-1.0.1"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="topic">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="category" type="Category"/>
</xs:sequence>
<xs:attribute name="name" type="SimplePatternExpression" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="category" type="Category"/>
</xs:choice>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
The code which is passing this scheme is
<?xml version="1.0" encoding="ISO-8859-1"?>
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 ../../resources/schema/AIML.xsd">
<category>
...
While the code which is not passing is
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0">
<category>
...
Latter looks simpler, but not passes.
If I change enumeration to
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
<xs:enumeration value="1.0"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
it is still not passes.
How to quick fix XSD?
Why Xerces doesn't give precise error but just behaving blind?
For reference, here is your XSD with the types of category
and SimplePatternExpression
stubbed out to be complete:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron"
targetNamespace="http://alicebot.org/2001/AIML-1.0.1"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="1.0"
xml:lang="EN">
<xs:element name="aiml">
<xs:annotation>
<xs:documentation>An AIML object is represented by an aiml element in an XML document.</xs:documentation>
<xs:appinfo>
<sch:title>Schematron validation</sch:title>
<sch:ns prefix="aiml" uri="http://alicebot.org/2001/AIML-1.0.1"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="topic">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="category" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="category" type="xs:string"/>
</xs:choice>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1">
<category/>
</aiml>
(assuming no external tie to an XSD) will yield an error such as
[Error] try.xml:2:21: cvc-elt.1.a: Cannot find the declaration of element 'aiml'.
because the parser never located the XSD.
xsi:noNamespaceSchemaLocation
:<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<category/>
</aiml>
will yield an error such as
[Error] try.xsd:10:26: TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'http://alicebot.org/2001/AIML-1.0.1'.
[Error] try.xml:4:47: cvc-elt.1.a: Cannot find the declaration of element 'aiml'.
Now the error is because the parser has found the XSD, and the XSD states via targetNamespace
that the aiml
element is expected to be in the http://alicebot.org/2001/AIML-1.0.1
namespace.
At this point, you have to decide whether your intent is to conform to the XSD, which is insisting that the elements of the XML instance be in a namespace, or your intent is to modify the XSD so that your simpler XML instance is valid.
To allow your XML to be valid without being in a namespace, delete the targetNamespace="http://alicebot.org/2001/AIML-1.0.1"
attribute from the xs:schema
element in the XSD.
To place the elements of your XML into the namespace required by the XSD, modify the XML as follows:
<?xml version="1.0" encoding="UTF-8"?>
<a:aiml version="1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://alicebot.org/2001/AIML-1.0.1"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 try.xsd">
<a:category/>
</a:aiml>
Note the declaration of a
as a namespace prefix for the http://alicebot.org/2001/AIML-1.0.1
namespace and the use of that prefix on the aiml
and category
elements. Note also the specification of the XSD for the http://alicebot.org/2001/AIML-1.0.1
namespace via the xsi:schemaLocation
attribute.