XMl Schema: Unique Key Values ​​in the Parent

I have the following XML:

    <enumTypes xmlns="tempURI">
    <enumType id="1">
        <enumValue id="1" value="Item1"/>
        <enumValue id="2" value="Item2"/>
        <enumValue id="3" value="Item3"/>
    </enumType>
    <enumType id="2">
        <enumValue id="1" value="Item1"/>
        <enumValue id="2" value="Item2"/>
    </enumType>
</enumTypes>

I also have the following diagram:

                <xs:element name="enumTypes">
                <xs:complexType>
                    <xs:sequence minOccurs="1" maxOccurs="unbounded">
                        <xs:element name="enumType">
                            <xs:complexType>
                                <xs:sequence minOccurs="1" maxOccurs="unbounded">
                                    <xs:element name="enumValue">
                                        <xs:complexType>
                                            <xs:attribute name="id" type="xs:string" use="required"/>
                                            <xs:attribute name="value" type="xs:string" use="required"/>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                                <xs:attribute name="id" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
    <xs:key name="enumTypeKey">
        <xs:selector xpath="enumTypes/enumType"/>
        <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="enumValueKey">
        <xs:selector xpath="enumTypes/enumType/enumValue"/>
        <xs:field xpath="@id"/>
    </xs:key>

I am trying to make the enumValue identifier be unique WITHIN enumType, but so far I can only get it to make them unique in all enumTypes.

I assume there is a problem with my XPath selector, but I can’t understand that it figured out.

Any help would be appreciated!

+3
source share
2 answers

I think you just gave a snippet of your xsd? Easier to debug if you can provide a working snippet. I added an environment <element name="root">to test it.

, enumValueKey enumValues enumType 's; , enumType.

, , - <key> enumValues enumType ( xpath), :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="enumTypes">
          <xs:complexType>
            <xs:sequence minOccurs="1" maxOccurs="unbounded">
              <xs:element name="enumType">
                <xs:complexType>
                  <xs:sequence minOccurs="1" maxOccurs="unbounded">
                    <xs:element name="enumValue">
                      <xs:complexType>
                        <xs:attribute name="id" type="xs:string" use="required"/>
                        <xs:attribute name="value" type="xs:string" use="required"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="id" type="xs:string"/>
                </xs:complexType>
                <xs:key name="enumValueKey">
                  <xs:selector xpath="enumValue"/>
                  <xs:field xpath="@id"/>
                </xs:key>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:key name="enumTypeKey">
      <xs:selector xpath="enumTypes/enumType"/>
      <xs:field xpath="@id"/>
    </xs:key>
  </xs:element>
</xs:schema>

, XML-, ( 1), :

...
<enumType id="2">
  <enumValue id="1" value="Item1"/>
  <enumValue id="1" value="Item2"/>
</enumType>
...

, . , .

+3

, xs: key <xs:element name="enumTypes">. :

<xs:element name="enumTypes">
 ... what you want ...
 <xs:key ... and so on
</xs:element> 
0

Source: https://habr.com/ru/post/1779039/


All Articles