XSD 具有可变属性的元素

XSD element with a variable # of attributes

从以下定义开始:

<xs:element name="Credentials">
    <xs:complexType>
      <xs:attribute name="accountID" type="xs:string" use="required"/>
      <xs:attribute name="username" type="xs:string" use="required"/>
      <xs:attribute name="password" type="xs:string" use="required"/>
      <xs:attribute name="cred1" type="xs:string"/>
      <xs:attribute name="cred2" type="xs:string"/>
      <xs:attribute name="cred3" type="xs:string"/>
      <xs:attribute name="cred4" type="xs:string"/>
      <xs:attribute name="cred5" type="xs:string"/>
      <xs:attribute name="cred6" type="xs:string"/>
      <xs:attribute name="cred7" type="xs:string"/>
      <xs:attribute name="cred8" type="xs:string"/>
      <xs:attribute name="cred9" type="xs:string"/>
      <xs:attribute name="cred10" type="xs:string"/>
      <xs:attribute name="cred11" type="xs:string"/>
      <xs:attribute name="cred12" type="xs:string"/>
      <xs:attribute name="cred13" type="xs:string"/>
      <xs:attribute name="cred14" type="xs:string"/>
      <xs:attribute name="cred15" type="xs:string"/>
      <xs:attribute name="cred16" type="xs:string"/>
      <xs:attribute name="cred17" type="xs:string"/>
      <xs:attribute name="cred18" type="xs:string"/>
      <xs:attribute name="cred19" type="xs:string"/>
      <xs:attribute name="cred20" type="xs:string"/>
    </xs:complexType>

我怎样才能把它改成这样的:

<xs:element name="Credentials">
    <xs:complexType>
      <xs:attribute name="accountID" type="xs:string" use="required"/>
      <xs:attribute name="username" type="xs:string" use="required"/>
      <xs:attribute name="password" type="xs:string" use="required"/>
      <!-- 0 or more of these attributes below - even 100 but I doubt I would need 100 for anything - i.e. optional -->
      <xs:attribute name=don't care, but it should have a name" type="xs:string"/>
    </xs:complexType>

正在验证此类有效数据:

<Credentials accountID="abc" username="me" password="mine"/>
<Credentials accountID="abc" username="me" password="mine" foo="bar"/>
<Credentials accountID="abc" username="me" password="mine" apples="oranges" fruit="sweet" just="another attribute"/>

如果要允许任何属性,请使用 xs:anyAttribute:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Credentials">
    <xs:complexType>
      <xs:attribute name="accountID" type="xs:string" use="required"/>
      <xs:attribute name="username" type="xs:string" use="required"/>
      <xs:attribute name="password" type="xs:string" use="required"/>
      <xs:anyAttribute processContents="skip"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

对于processContents

  • 使用 skip 允许任何属性,忽略任何声明。
  • 使用 lax 允许任何属性,但要求对已声明的属性进行验证 属性。
  • 使用 strict 只允许声明的验证属性 成功。