Unqualified XSD attribute references

The following XML schema cannot be validated with the following XML instance document. Is there a way to rewrite the schema so that the instance document checks within the given limits?

Limitations

  • Attribute cannot be local to an element
  • Instance document must be immutable

(Invalid) Scheme

<?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" /> 
+4
source share
2 answers

The namespace in XML reads: "The namespace name for a name without an attribute prefix always does not matter"; on the other hand, you limited the attribute to non-local, so the only way to do this (credit belongs to @GrahamHannington) is to wrap it in an attribute group, which allows you to define the attribute again without re-qualification.

+5
source

Yes.

Wrap the (global) xs:attribute element in the xs:attributeGroup element.

In the xs:element refer to the xs:attributeGroup element.

The name xs:attributeGroup element can have the same value as the name attribute of the xs:attribute element.

Scheme

 <?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:attributeGroup name="sample-attribute"> <xs:attribute name="sample-attribute" type="xs:string" use="required"/> </xs:attributeGroup> <xs:element name="sample-element"> <xs:complexType> <xs:attributeGroup ref="sample-attribute" /> </xs:complexType> </xs:element> </xs:schema> 

Information not directly related to the question

This is not an extension of the answer above, but also an alternative answer, just related information that you may find useful (it is not within the limits of your question).

You can leave your original schema untouched and explicitly assign (add a namespace prefix) the attribute name in the document instance, for example:

 <?xml version="1.0" encoding="utf-8"?> <t:sample-element xmlns:t="http://tempuri.org/XMLSchema.xsd" t:sample-attribute="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd sample.xsd"/> 

(Note the namespace prefix t: both the name of the root element and the attribute name.)

+8
source

Source: https://habr.com/ru/post/1391564/


All Articles