Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

xml - Unqualified XSD global attribute references

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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes.

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

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

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

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: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 to the answer above, nor an alternative answer, just related information that you might find helpful (it is not within the constraints of your question).

You could leave your original schema untouched, and explicitly qualify (add a namespace prefix to) the attribute name in the document instance, like this:

<?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 t: namespace prefix on both the root element name and the attribute name.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...