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

后端 未结 1 1940
青春惊慌失措
青春惊慌失措 2021-01-25 15:52

Let\'s take the following example XML:


  NiceDevice
  123


  

        
相关标签:
1条回答
  • 2021-01-25 16:14

    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.

    0 讨论(0)
提交回复
热议问题