将属性值限制为子属性(引用现有值)

发布于 2024-11-03 10:49:53 字数 1214 浏览 0 评论 0原文

我有一个像这样的 xml:

<userCredentials default="user1" >
    <userCredential username="user1" password="pwd" />
    <userCredential username="user2" password="pwd" />
    <userCredential username="user3" password="pwd" />
</userCredentials>

如何将属性 default 的值限制为 //userCredential[@username] 值之一?

下面是我的 xsd 方案:

<xs:complexType name="userCredential">
  <xs:attribute name="username" type="xs:string" use="required" />
  <xs:attribute name="password" type="xs:string" use="required" />
</xs:complexType>    

<xs:complexType name="userCredentials">
  <xs:sequence>
    <xs:element name="userCredential" type="tns:userCredential" minOccurs="1"
                maxOccurs="unbounded" />
  </xs:sequence>
  <xs:attribute name="default" use="required" >
      <xs:simpleType>
        <xs:restriction>
          <xs:pattern value="" />
        </xs:restriction>
      </xs:simpleType>
  </xs:attribute>        
</xs:complexType>  

I have a xml like this:

<userCredentials default="user1" >
    <userCredential username="user1" password="pwd" />
    <userCredential username="user2" password="pwd" />
    <userCredential username="user3" password="pwd" />
</userCredentials>

How can I resrict value of attribute default to be only one of //userCredential[@username] value?

Below my xsd scheme:

<xs:complexType name="userCredential">
  <xs:attribute name="username" type="xs:string" use="required" />
  <xs:attribute name="password" type="xs:string" use="required" />
</xs:complexType>    

<xs:complexType name="userCredentials">
  <xs:sequence>
    <xs:element name="userCredential" type="tns:userCredential" minOccurs="1"
                maxOccurs="unbounded" />
  </xs:sequence>
  <xs:attribute name="default" use="required" >
      <xs:simpleType>
        <xs:restriction>
          <xs:pattern value="" />
        </xs:restriction>
      </xs:simpleType>
  </xs:attribute>        
</xs:complexType>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-11-10 10:49:53

这是完全有可能的。下面是一个 XSD 示例,它强制块内所有用户名的唯一性,并且还要求 default 属性引用这些值之一:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="userCredentials" type="CredsType">

    <!-- ensure all user names are unique -->
    <xs:unique name="uniqueUserNames">
      <xs:selector xpath="userCredential"/>
      <xs:field xpath="@username"/>
    </xs:unique>

    <!-- ensure that the `default` attribute references existing username -->
    <xs:keyref name="defaultNameRef" refer="userNames">
      <xs:selector xpath="."/>
      <xs:field xpath="@default"/>
    </xs:keyref>
    <xs:key name="userNames">
      <xs:selector xpath="./userCredential"/>
      <xs:field xpath="@username"/>
    </xs:key>
  </xs:element>

  <xs:complexType name="CredsType">
    <xs:sequence>
      <xs:element name="userCredential" type="UserCredentialType"
                  maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="default" type="xs:NCName" />
  </xs:complexType>

  <xs:complexType name="UserCredentialType">
    <xs:attribute name="username" type="xs:NCName"/>
    <xs:attribute name="password" type="xs:string"/>
  </xs:complexType>
</xs:schema>

This is completely possible. Here's an example XSD that enforces the uniqueness of all usernames within the block and also requires that the default attribute references one of those values:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="userCredentials" type="CredsType">

    <!-- ensure all user names are unique -->
    <xs:unique name="uniqueUserNames">
      <xs:selector xpath="userCredential"/>
      <xs:field xpath="@username"/>
    </xs:unique>

    <!-- ensure that the `default` attribute references existing username -->
    <xs:keyref name="defaultNameRef" refer="userNames">
      <xs:selector xpath="."/>
      <xs:field xpath="@default"/>
    </xs:keyref>
    <xs:key name="userNames">
      <xs:selector xpath="./userCredential"/>
      <xs:field xpath="@username"/>
    </xs:key>
  </xs:element>

  <xs:complexType name="CredsType">
    <xs:sequence>
      <xs:element name="userCredential" type="UserCredentialType"
                  maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="default" type="xs:NCName" />
  </xs:complexType>

  <xs:complexType name="UserCredentialType">
    <xs:attribute name="username" type="xs:NCName"/>
    <xs:attribute name="password" type="xs:string"/>
  </xs:complexType>
</xs:schema>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文