Reputation: 10609
I am trying to reference a few attributes in the XSD file (XML Schema), and getting the following errors:
Attribute 'code' is not allowed to appear in element 'project'.
Attribute 'type' is not allowed to appear in element 'duration'.
Attribute 'type' is not allowed to appear in element 'duration'.
Can anyone explain to me what I'm doing wrong?
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<timesheets xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets timesheets.xsd">
<employee id="234456" ismanager="false" hiredate="2003-01-12">
<firstname>Julie</firstname>
<surname>Smith</surname>
</employee>
<projects>
<project code="TSK2367">
<task>
<description>Create project plan for MIJ website</description>
<duration type="days">
<planned>2</planned>
<actual>1</actual>
</duration>
</task>
<task>
<description>Draft the conceptual web site navigation for MIJ</description>
<duration type="hours">
<planned>3</planned>
<actual>2</actual>
</duration>
</task>
</project>
</projects>
</timesheets>
And this is my XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
>
<xs:attributeGroup name="employeeAttrs">
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="ismanager" type="xs:boolean" />
<xs:attribute name="hiredate" type="xs:date" />
</xs:attributeGroup>
<xs:element name="firstname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
<xs:attribute name="code" type="xs:string" />
<xs:element name="description" type="xs:string" />
<xs:attribute name="type" type="xs:string" />
<xs:element name="planned" type="xs:int" />
<xs:element name="actual" type="xs:int" />
<xs:element name="timesheets">
<xs:complexType>
<xs:sequence>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element ref="firstname" />
<xs:element ref="surname" />
</xs:sequence>
<xs:attributeGroup ref="employeeAttrs" />
</xs:complexType>
</xs:element>
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element name="project" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="task" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="description" />
<xs:element name="duration">
<xs:complexType>
<xs:sequence>
<xs:element ref="planned" />
<xs:element ref="actual" />
</xs:sequence>
<xs:attribute ref="type" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute ref="code" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 1
Views: 622
Reputation: 111726
If you wish to change the XML to conform to the XSD, define a namespace prefix for xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
and use it on the attributes that are not being found:
<?xml version="1.0" encoding="UTF-8"?>
<timesheets xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets
timesheets.xsd">
<employee id="234456" ismanager="false" hiredate="2003-01-12">
<firstname>Julie</firstname>
<surname>Smith</surname>
</employee>
<projects>
<project ts:code="TSK2367">
<task>
<description>Create project plan for MIJ website</description>
<duration ts:type="days">
<planned>2</planned>
<actual>1</actual>
</duration>
</task>
<task>
<description>Draft the conceptual web site navigation for MIJ</description>
<duration ts:type="hours">
<planned>3</planned>
<actual>2</actual>
</duration>
</task>
</project>
</projects>
</timesheets>
If you wish to change the XSD to make the XML be valid, move the defintions of code
and type
local to where they're used rather than global:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
>
<xs:attributeGroup name="employeeAttrs">
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="ismanager" type="xs:boolean" />
<xs:attribute name="hiredate" type="xs:date" />
</xs:attributeGroup>
<xs:element name="firstname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
<xs:element name="description" type="xs:string" />
<xs:element name="planned" type="xs:int" />
<xs:element name="actual" type="xs:int" />
<xs:element name="timesheets">
<xs:complexType>
<xs:sequence>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element ref="firstname" />
<xs:element ref="surname" />
</xs:sequence>
<xs:attributeGroup ref="employeeAttrs" />
</xs:complexType>
</xs:element>
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element name="project" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="task" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="description" />
<xs:element name="duration">
<xs:complexType>
<xs:sequence>
<xs:element ref="planned" />
<xs:element ref="actual" />
</xs:sequence>
<xs:attribute name="type" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="code" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
So are you saying that there is no way to reference an attribute without placing its definition in the global scope?
No, I showed that if you want to eliminate your errors by changing the XSD, you could make the declaration of the code
and type
attributes be local rather than global.
Also, why did the attributeGroup referencing not throw an error?
Because the attribute declarations are then local to the attributeGroup.
The most common way to use attributes, and the way I'd recommend you follow unless you're defining an attribute-based vocabulary deliberately intended to be mixed with multiple namespaces, is to have them be in no namespace.
Your options for having attributes be in no namespace:
targetNamespace
.Upvotes: 3