XSD 架构中的元素强制属性声明:

XSD 架构中的元素强制属性声明:

问题描述:

我想声明一个要包含在复杂类型声明中的元素,并且该元素有一个强制属性:option=MyOption",但是option"属性的值可以是任何值,具体取决于上下文.

I want to declare an element to be included in a complex type declaration, and the element has a mandatory attribute: "option=MyOption", but the value of the "option" attribute could be anything, depending on the context.

也就是说:在使用包含该元素的复杂类型的任何文档中,具有某些未知值的属性选项"应该是强制性的.

That is: the attribute "option" with some unknown value should be mandatory in any document using the complex type containing this element.

示例:

    <xs:element name="SpecialOption" type="xs:string"/>

    <xs:complexType name="SpecialOptions">
        <xs:sequence>
            <xs:element ref="SpecialOption" minOccurs="1" maxOccurs="100"/>
            <xs:element ref="XXX"/>     
        </xs:sequence>
    </xs:complexType>   

在这种情况下,复杂类型SpecialOptions"中的SpecialOption"元素应该具有这个强制属性.

In this case the "SpecialOption" element in the complex type "SpecialOptions" should have this mandatory attribute.

我不知道如何在 XSD 中为元素声明强制属性,也不知道如何指定该属性必须具有未知的值.

您需要修改SpecialOption"元素的定义以包含所需的属性.更新此代码:

You need to modify the definition of the "SpecialOption" element to include the required attribute. Update this code:

<xs:element name="SpecialOption" type="xs:string"/>

到此:

<xs:element name="SpecialOption">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Option" type="xs:string" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

通过此更改,您的复杂类型将在SpecialOptions"复杂类型中的SpecialOption"元素的所有实例上包含所需的Option"属性.将Option"属性声明为 xs:string 类型将允许在此字段中传递任何值.

With this change your complex type will contain the required "Option" attribute on all instances of the "SpecialOption" element in the "SpecialOptions" complex type. Declaring the "Option" attribute to be of type xs:string will allow any value to be passed in this field.