gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:19:52 25 4
gpt4 key购买 nike

我有这样一个 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>

最佳答案

这是完全可能的。下面是一个 XSD 示例,它强制 block 内所有用户名的唯一性,并且还要求 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>

关于xml - 将属性值限制为子属性(引用现有值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5802615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com