XSD Schema validation passes when duplicate keys exists
In the XSD, in a sequence tag when there are "names" with the exact same name and different attributes - validation does not throw any errors. When there are duplicate elements with same ID's within the same namespace the error of "duplicate key is expected"
XSD tested with below. The elements "PostAddr" and "author" should be reported as duplicate entries.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:bks="urn:books">
<xsd:element name="books" type="bks:BooksForm"/>
<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book" type="bks:BookForm" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:choice minOccurs="0">
<xsd:element minOccurs="0" name="PostAddr" type="xsd:string" />
<xsd:element minOccurs="10" maxOccurs="300" name="PostAddr" type="xsd:string" />
<xsd:sequence minOccurs="0">
<xsd:element name="City" minOccurs="0" maxOccurs="unbounded" type="xsd:string" />
<xsd:element minOccurs="0" name="StateProv" type="xsd:string" />
<xsd:element minOccurs="0" name="PostalCode" type="xsd:string" />
<xsd:element minOccurs="0" name="Country" type="xsd:string" />
</xsd:sequence>
</xsd:choice>
<xsd:element name="author" type="xsd:string" minOccurs="0"/>
<xsd:element minOccurs="10" maxOccurs="100" name="author" type="xsd:string" />
<xsd:element name="title" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="genre" type="xsd:string" minOccurs="100" maxOccurs="300"/>
<xsd:element name="price" type="xsd:float" minOccurs="2" maxOccurs="unbounded" />
<xsd:element minOccurs="10" maxOccurs="300" name="PostAddr" type="xsd:string" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Code:
int main()
{
xmlParserCtxtPtr pctxt = NULL ;
xmlDocPtr doc;
xmlSchemaPtr schema = NULL;
xmlSchemaPtr temp = NULL;
xmlSchemaParserCtxtPtr ctxt;
xmlSchemaTypePtr schemaTypePtr = NULL;
struct _xmlSchemaParticle* p;
char *XSDFileName = "sample.xsd";
ctxt = xmlSchemaNewParserCtxt(XSDFileName);
xmlSchemaSetParserErrors(ctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
schema = xmlSchemaParse(ctxt);
schemaTypePtr = (xmlSchemaTypePtr)schema->typeDecl ;
p = (struct _xmlSchemaParticle*)schemaTypePtr->subtypes;
if (schema != NULL) {
print_schema(schema->doc->children) ;
//xmlSchemaDump(stdout, schema);
xmlSchemaFree(schema);
}
xmlSchemaFreeParserCtxt(ctxt);
xmlSchemaCleanupTypes();
xmlCleanupParser();
xmlMemoryDump();
return (0);
}
Edited by Nick Wellnhofer