Let's take the following example XML:
<device>
<name>NiceDevice</name>
<value>123</value>
</device>
<user>
<name>user1</name>
<usesDevice>NiceDevice</usesDevice>
</user>
<user>
<name>user2</name>
<usesDevice>NiceDevice</usesDevice>
</user>
validated by this XSD:
<xs:element name="device" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<element name="name" type="xs:string"/>
<element name="value" type="xs:integer"/>
</xs:complexType>
</xs:element>
<xs:element name="user" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<element name="name" type="xs:string"/>
<element name="usesDevice" type="xs:string"/>
</xs:complexType>
</xs:element>
What I now want to do is validate only xmls where the value of user/usesDevice
also appears somewhere as device/value
.
Or in other words: I want to only allow the use of devices which were defined somewhere.
Meaning the following XML should not be valid:
<device>
<name>NiceDevice</name>
<value>123</value>
</device>
<user>
<name>user1</name>
<usesDevice>NiceDevice</usesDevice>
</user>
<user>
<name>user2</name>
<usesDevice>BadDevice</usesDevice>
</user>
Because BadDevice
was never used as device/name
.
Use xs:key
and xs:keyref
to constrain usesDevices
to have a value of a device
name
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="r">
<xs:complexType>
<xs:sequence>
<xs:element name="device" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="value" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="user" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="usesDevice" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="deviceKey" >
<xs:selector xpath="device"/>
<xs:field xpath="name" />
</xs:key>
<xs:keyref name="deviceKeyRef" refer="deviceKey">
<xs:selector xpath="user" />
<xs:field xpath="usesDevice" />
</xs:keyref>
</xs:element>
</xs:schema>
Then this XML will be valid:
<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<device>
<name>NiceDevice</name>
<value>123</value>
</device>
<user>
<name>user1</name>
<usesDevice>NiceDevice</usesDevice>
</user>
<user>
<name>user2</name>
<usesDevice>NiceDevice</usesDevice>
</user>
</r>
But this XML,
<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<device>
<name>NiceDevice</name>
<value>123</value>
</device>
<user>
<name>user1</name>
<usesDevice>NiceDevice</usesDevice>
</user>
<user>
<name>user2</name>
<usesDevice>BadDevice</usesDevice>
</user>
</r>
will not,
[Error] try.xml:16:5: cvc-identity-constraint.4.3: Key 'deviceKeyRef' with value 'BadDevice' not found for identity constraint of element 'r'.
as requested.
Note that I've added to your XML a single root element, which is required for your XML to be well-formed.
来源:https://stackoverflow.com/questions/51537997/allow-only-other-existing-xml-values-in-xsd-xskey-and-xskeyref