Reputation: 3450
I get somewhat schizophrenic behavior from xsd validation. This link shows the xml and xsd + the errors in an online schema validator. When I run this locally with xmllint
xmllint --noout --nonet --schema devhelp2.xsd tester.devhelp2
I get similar warnings:
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub', attribute 'name': The attribute 'name' is not allowed.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub', attribute 'link': The attribute 'link' is not allowed.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub': The attribute '{urn:devhelp}name' is required but missing.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub': The attribute '{urn:devhelp}link' is required but missing.
but this has a hint that there is something wrong with the namespace.
I can make it validate by dropping the xmlns completely (as taken from zvon.org). See here for the new online validator example - I'd still like to understand it though, isn't there a solution keeping the xmlns?
Upvotes: 4
Views: 2848
Reputation: 7383
Reduced example
I reduced your example to following XML schema.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:devhelp"
xmlns="urn:devhelp"
elementFormDefault="qualified">
<xsd:attribute name="title" type="xsd:string"/>
<xsd:element name="book">
<xsd:complexType>
<xsd:attribute ref="title" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
and this XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<book xmlns="urn:devhelp" title="tester Reference Manual"/>
Validation errors
Not valid.
Error - Line 2, 60: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 60; cvc-complex-type.3.2.2: Attribute 'title' is not allowed to appear in element 'book'.
Error - Line 2, 60: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 60; cvc-complex-type.4: Attribute 'title' must appear on element 'book'.
The contradicting validation errors are a good indication that something is wrong with the namespaces.
Specification: Namespaces in XML 1.0
The specifications states
... Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
The first clause explains that attributes won't inherit the default namespace declaration of the element. Therefore /book/@title
has no namespace, while your XML schema requires a title
attribute of namespace urn:devhelp
.
The second clause is tricky, because it's easy to misunderstand. It just says that attributes won't need a namespace, because they could be used differently based on the surrounding element.
An example also mentions that behaviour:
... However, each of the following is legal, the second because the default namespace does not apply to attribute names:
...
<x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" >
<good a="1" b="2" />
<good a="1" n1:a="2" />
</x>
Solutions
Explicitly set a namespace for the attribute.
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<book xmlns="urn:devhelp" xmlns:mine="urn:devhelp" mine:title="tester Reference Manual"/>
or avoid attribute definitions outside of complexType
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:mine="urn:devhelp"
targetNamespace="urn:devhelp"
elementFormDefault="qualified">
<xsd:element name="book">
<xsd:complexType>
<xsd:attribute name="title" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Upvotes: 5