The following XML schema fails to validate with following XML instance document. Is there any way to rewrite the schema so the instance document validates, within the given constraints?
Constraints
- The attribute cannot be local to the element
- The instance document must be unchanged
(Invalid) Schema
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="sample-attribute" type="xs:string" />
<xs:element name="sample-element">
<xs:complexType>
<xs:attribute ref="sample-attribute" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Instance
<?xml version="1.0" encoding="utf-8"?>
<sample-element xmlns="http://tempuri.org/XMLSchema.xsd" sample-attribute="test" />
Namespaces in XML states that “The namespace name for an unprefixed attribute name always has no value”; on the other hand you constrained the attribute not to be local, so the only way to do it (credit goes to @GrahamHannington) is to wrap it in an attribute group, thus allowing the attribute definition to be reused without being qualified.