Allow only other existing XML values in XSD? (xs:key and xs:keyref)

老子叫甜甜 提交于 2019-12-02 06:06:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!