I have an xsd to validate an xml file, but I’m getting the error Invalid content was found starting with element 'user'. One of '{user}' is expected.
If I change the namespace declaration to xmlns:synchronisation="synchronisation" and put synchronisation:syncQueryMapping as the root tag, but keep the rest the same, it is validated, but I do not understand why this works, why it is necessary and why the rest of the tags don’t require it.
I can’t seem to understand & fix the issue, so any help would be greatly appreciated!
Thanks!
The xml file:
<?xml version="1.0" encoding="UTF-8"?>
<syncQueryMapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="synchronisation"
xsi:schemaLocation="synchronisation syncQueryMappingSchema.xsd">
<user>
<tableName></tableName>
<nameColumn></nameColumn>
<passwordColumn></passwordColumn>
</user>
<group>
<tableName></tableName>
<nameColumn></nameColumn>
</group>
<userGroupMapping>
<tableName></tableName>
<userNameColumn></userNameColumn>
<groupNameColumn></groupNameColumn>
</userGroupMapping>
</syncQueryMapping>
The xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0" targetNamespace="synchronisation" xmlns="synchronisation">
<!-- JAXB Configuration -->
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="synchronisation.implementation" />
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>
<xs:complexType name="dbSyncUserType" >
<xs:sequence>
<xs:element type="xs:string" name="tableName" />
<xs:element type="xs:string" name="nameColumn" />
<xs:element type="xs:string" name="passwordColumn" />
<xs:element type="xs:string" name="suspendStartDateColumn" minOccurs="0" />
<xs:element type="xs:string" name="suspendEndDateColumn" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="dbSyncGroupType">
<xs:sequence>
<xs:element type="xs:string" name="tableName" />
<xs:element type="xs:string" name="nameColumn" />
<xs:element type="xs:string" name="typeColumn" minOccurs="0" />
<xs:element type="typeColumnDataTypeType" name="typeColumnDataType" minOccurs="0" />
<xs:element type="xs:string" name="typeValue" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="typeColumnDataTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="INTEGER" />
<xs:enumeration value="VARCHAR" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="dbSyncUserTableJoinType">
<xs:sequence>
<xs:element type="xs:string" name="userKeyColumn" />
<xs:element type="xs:string" name="mappingsForeignKeyColumn" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="dbSyncGroupTableJoinType">
<xs:sequence>
<xs:element type="xs:string" name="groupKeyColumn" />
<xs:element type="xs:string" name="mappingsForeignKeyColumn" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="dbSyncUserGroupMappingJoinType">
<xs:sequence>
<xs:element type="dbSyncUserTableJoinType" name="userTable" />
<xs:element type="dbSyncGroupTableJoinType" name="groupTable" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="dbSyncUserGroupMappingType">
<xs:sequence>
<xs:element type="xs:string" name="tableName" />
<xs:element type="xs:string" name="userNameColumn" />
<xs:element type="xs:string" name="groupNameColumn" />
<xs:element type="dbSyncUserGroupMappingJoinType" name="join" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<!-- Root element -->
<xs:element name="syncQueryMapping">
<xs:complexType>
<xs:sequence>
<xs:element type="dbSyncUserType" name="user" />
<xs:element type="dbSyncGroupType" name="group" />
<xs:element type="dbSyncUserGroupMappingType" name="userGroupMapping" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
When you added
xmlns="synchronisation"to your XML document you are specifying that all nested elements without a prefix belong to that namespace (syncQueryMappinganduser).You need to add
elementFormDefault="qualified"on the root element in your XML schema to indicate that all the elements in a XML document corresponding to this XML schema are namespace qualified.Without that specified in order for your XML document to be valid only the global elements should be namespace qualfied. This would mean you could not use a default namespace.