I am tasked with adding type="DeptType" to an xsd file and validating it in basex against an xml document. I have validated both in notepad++ but I get an error in basex when trying to validate.
XSD
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="dept" type="DeptType" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="staff" type="StaffType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:key name="deptKey">
<xs:selector xpath="dept"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:key name="staffKey">
<xs:selector xpath="staff"/>
<xs:field xpath="@id"/>
</xs:key>
<!-- keyref does not work with simpleContent -->
<xs:keyref name="staffdeptref" refer="deptKey">
<xs:selector xpath="staff"/>
<xs:field xpath="@dept"/>
</xs:keyref>
</xs:element>
<xs:complexType name="StaffType">
<xs:choice>
<xs:element name="staff" type="StaffType" maxOccurs="unbounded" />
<xs:element name="name" type="xs:string"/>
</xs:choice>
<xs:attribute name="dept"/>
<xs:attribute name="id"/>
</xs:complexType>
</xs:schema>
xml
<?xml version="1.0" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<dept id="i1"><name>it</name></dept>
<dept id="i2"><name>law</name></dept>
<staff id="i2"><name>steve</name></staff>
<staff id="i3" dept="i2"><name>jerry</name></staff>
</root>
basex validation validate:xsd("doc-4xsd.xml")
The error I get is enter image description here
I think it's because of this part:
<xs:element name="dept" type="DeptType" maxOccurs="unbounded">
<xs:complexType>
Since the complexType
is defined within the element
, you shouldn't specify the type
attribute. I think the type
attribute is just for referencing types elsewhere in the schema. So it's either:
<xs:element name="dept" maxOccurs="unbounded">
<xs:complexType>
or
<xs:element name="dept" type="DeptType" maxOccurs="unbounded"/>
<xs:complexType name="DeptType">
but not both.