XML 模式 - 列表中变量的约束
XML Schema - constraint on variable in list
我有一个模式,我想在其中对列表进行特定约束。在 Firm
下的 Employees
元素中,我需要员工列表中的所有 DepartmentId
都相同。因此,如果 Firm
的实例具有 1378
的 DepartmentId
,则 Firm
对象仅在所有 Employees
具有相同 ID 时才有效。这可能与 XSD 有关吗?最好能把它放在这里,这样我就不需要在根据代码中的 XSD 验证对象后检查它了。
<element name="Firm">
<complexType>
<sequence>
<element name="Name" type="string" />
<element name="Employees" type="tns:Employee" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="Employee">
<sequence>
<element name="EmployeeId" type="string" />
<element name="DepartmentId" type="string" />
<element name="Department" type="string" />
<element name="Name" type="string" />
<element name="Description" type="string" />
</sequence>
</complexType>
XSD1.0
你的约束不能在XSD 1.0中表达。
XSD1.1
您的约束 可以 通过 xs:assert
:
在 XSD 1.1 中表达
<xs:assert test="count(distinct-values(tns:Employees/tns:DepartmentId)) = 1"/>
上下文中...
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://example.com/tns"
targetNamespace="http://example.com/tns"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="Firm">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Employees" type="tns:Employee"
minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
<xs:assert test="count(distinct-values(tns:Employees/tns:DepartmentId))
= 1"/>
</xs:complexType>
</xs:element>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="EmployeeId" type="xs:string" />
<xs:element name="DepartmentId" type="xs:string" />
<xs:element name="Department" type="xs:string" />
<xs:element name="Name" type="xs:string" />
<xs:element name="Description" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
那么这个XML就会有效:
<?xml version="1.0" encoding="UTF-8"?>
<Firm>
<Name></Name>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
</Firm>
但是这个XML会无效:
<?xml version="1.0" encoding="UTF-8"?>
<Firm>
<Name></Name>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>9999</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
</Firm>
根据要求。
我有一个模式,我想在其中对列表进行特定约束。在 Firm
下的 Employees
元素中,我需要员工列表中的所有 DepartmentId
都相同。因此,如果 Firm
的实例具有 1378
的 DepartmentId
,则 Firm
对象仅在所有 Employees
具有相同 ID 时才有效。这可能与 XSD 有关吗?最好能把它放在这里,这样我就不需要在根据代码中的 XSD 验证对象后检查它了。
<element name="Firm">
<complexType>
<sequence>
<element name="Name" type="string" />
<element name="Employees" type="tns:Employee" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="Employee">
<sequence>
<element name="EmployeeId" type="string" />
<element name="DepartmentId" type="string" />
<element name="Department" type="string" />
<element name="Name" type="string" />
<element name="Description" type="string" />
</sequence>
</complexType>
XSD1.0
你的约束不能在XSD 1.0中表达。
XSD1.1
您的约束 可以 通过 xs:assert
:
<xs:assert test="count(distinct-values(tns:Employees/tns:DepartmentId)) = 1"/>
上下文中...
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://example.com/tns"
targetNamespace="http://example.com/tns"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="Firm">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Employees" type="tns:Employee"
minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
<xs:assert test="count(distinct-values(tns:Employees/tns:DepartmentId))
= 1"/>
</xs:complexType>
</xs:element>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="EmployeeId" type="xs:string" />
<xs:element name="DepartmentId" type="xs:string" />
<xs:element name="Department" type="xs:string" />
<xs:element name="Name" type="xs:string" />
<xs:element name="Description" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
那么这个XML就会有效:
<?xml version="1.0" encoding="UTF-8"?>
<Firm>
<Name></Name>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
</Firm>
但是这个XML会无效:
<?xml version="1.0" encoding="UTF-8"?>
<Firm>
<Name></Name>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>1378</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
<Employees>
<EmployeeId></EmployeeId>
<DepartmentId>9999</DepartmentId>
<Department></Department>
<Name></Name>
<Description></Description>
</Employees>
</Firm>
根据要求。