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>
Update per OP questions in comments:
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.
Recommendation Regarding Attributes and Namespaces
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:
- Have no
targetNamespace
.
- Declare attributes locally to elements or attribute groups.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…