I am struggling with trying to develop a schema which allows attributes from mixed namespaces.
Here is xxx_schema2.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="qualified"
targetNamespace="http://www.mrbouffant.com/schema2"
xmlns:xxx="http://www.mrbouffant.com/schema2">
<xs:attributeGroup name="schema2AttributeGroup">
<xs:attribute name="schema2Attribute1 " type="xs:string"/>
<xs:attribute name="schema2Attribute2 " type="xs:string"/>
</xs:attributeGroup>
</xs:schema>
Here is xxx_schema1.xsd which imports xxx_schema2:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:xxx="http://www.mrbouffant.com/schema2">
<xs:import namespace="http://www.mrbouffant.com/schema2" schemaLocation="xxx_schema2.xsd"/>
<!-- ROOT ELEMENT -->
<xs:element name="rootElement" type="rootElementType" />
<!-- COMPLEX TYPES -->
<xs:complexType name="rootElementType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attributeGroup ref="xxx:schema2AttributeGroup"/>
<xs:attribute name="schema1Attribute1" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
Here is the XML document which I would like to validate against xxx_schema1.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<rootElement xmlns:xxx='http://www.mrbouffant.com/schema2/'
schema1Attribute1="foo"
xxx:schema2Attribute1="bar"
xxx:schema2Attribute2="far" />
When the Saxon-EE parser attempts to validate the XML document against the schema, the errors it generates are literally:
Engine name: Saxon-EE 9.3.0.5
Severity: error
Description: Attribute @xxx:schema2Attribute1 is not allowed on element <rootElement>
(it would be allowed in namespace http://www.mrbouffant.com/schema2)
AND
Engine name: Saxon-EE 9.3.0.5
Severity: error
Description: Attribute @xxx:schema2Attribute2 is not allowed on element <rootElement>
(it would be allowed in namespace http://www.mrbouffant.com/schema2)
Please can you help me understand what I have done wrong in my schema definitions or XML document which prevent the validation from being successful? Thank you.
The namespace declaration for
http://www.mrbouffant.com/schema2in your instance XML has a trailing slash and does not match your Schema’s declared namespace.Remove the trailing slash and it validates just fine: