Hi guys,

I have built xsd for my xml.
Everything is working ok, but now I need to add support to the XML that a specific attribute will be unique.

The attribute that I need it to be unique is the "Id" attribute of the "Test" element, you can find it with the following code:

<xs:attribute ref="Id" use="required" />

here is my XSD:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Interactions">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="unbounded" name="Test">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="unbounded" name="Interaction">
                <xs:complexType>
                  <xs:sequence>
                    <xs:choice minOccurs="1" maxOccurs="unbounded">
                      <xs:element maxOccurs="unbounded" name="div">
                        <xs:complexType mixed="true">
                          <xs:sequence minOccurs="0">
                            <xs:element maxOccurs="unbounded" name="span">
                              <xs:complexType>
                                <xs:simpleContent>
                                  <xs:extension base="xs:string">
                                    <xs:attribute ref="class" use="required" />
                                  </xs:extension>
                                </xs:simpleContent>
                              </xs:complexType>
                            </xs:element>
                          </xs:sequence>
                          <xs:attribute ref="class" use="required" />
                        </xs:complexType>
                      </xs:element>
                      <xs:element maxOccurs="unbounded" name="br">
                        <xs:complexType>
                          <xs:attribute ref="class" use="required" />
                        </xs:complexType>
                      </xs:element>
                    </xs:choice>
                  </xs:sequence>
                  <xs:attribute ref="Id" use="required" />
                  <xs:attribute ref="InPrinters" use="required" />
                  <xs:attribute ref="PicturePath" use="required" />
                  <xs:attribute ref="InteractionButtonId" use="required" />
                  <xs:attribute ref="InteractionIconId" use="required" />
                  <xs:attribute ref="TimeOut" use="optional" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute ref="Id" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:attribute name="InteractionButtonId">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="InterBtnClrId" />
        <xs:enumeration value="InterBtnOkId" />
        <xs:enumeration value="InterBtnYnId" />
        <xs:enumeration value="InterBtnSkpId" />
        <xs:enumeration value="InterBtnVluId" />
        <xs:enumeration value="InterBtnYesId" />
        <xs:enumeration value="InterBtnNoId" />
        <xs:enumeration value="InterBtnNullId" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="InteractionIconId">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="InterIcnClrId" />
        <xs:enumeration value="InterIcnSnglId" />
        <xs:enumeration value="InterIcnMultlId" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="InPrinters">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="1" />
        <xs:enumeration value="2" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="Id">
    <xs:simpleType>
      <xs:restriction base="xs:long" />
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="PicturePath">
    <xs:simpleType>
      <xs:restriction base="xs:string" />
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="class">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="InteractionCrucial" />
        <xs:enumeration value="InteractionDefault" />
        <xs:enumeration value="InteractionNotDefault" />
        <xs:enumeration value="InteractionPlain" />
        <xs:enumeration value="InteractionHeader" />
        <xs:enumeration value="InteractionEmphasis" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="TimeOut">
    <xs:simpleType>
      <xs:restriction base="xs:long" />
    </xs:simpleType>
  </xs:attribute>
</xs:schema>

Please help me, I need it ASAP and I cannot work it out maybe I do somthing wrong here.

Dani AI

Generated

As noted, an identity constraint is the right approach. In this schema the repeated Test elements are children of the Interactions element, so the xs:unique must be declared on the parent that contains those repeats (the Interactions complexType). Place the constraint as a child of that complexType (typically after the xs:sequence that declares the Test elements) and select Test nodes; the field then points to the attribute on those nodes.

A minimal insertion (placed inside the Interactions complexType, after the sequence) looks like this:

<xs:unique name="uniqueTestId">
  <xs:selector xpath="Test"/>
  <xs:field xpath="@Id"/>
</xs:unique>

Notes and common pitfalls:

  • The xpath values are relative to the element where the identity constraint is declared; do not use a leading slash. The selector must pick the repeating element (Test), and the field selects the attribute (@Id) on those elements.
  • Attribute and element names are case-sensitive. If the schema or instance uses namespaces, bind and use the appropriate prefix in the XPath expression so names match the instance.
  • Because Id is typed (here as a numeric type), equality is checked with the typed value; different lexical forms that normalize to the same value (for example numeric forms) are considered equal.
  • If a validator reports an XPath error, confirm the xs prefix in the schema matches the namespace declaration and that the xs:unique is inside the correct element/complexType scope.

For formal rules and examples see the XML Schema specification and Microsoft’s documentation on unique/key/keyref constraints:

Recommended Answers

All 2 Replies

Without seeing your XML file, I cannot give a precise answer. The element you need to use is <xsd:unique> and your schema modification should look something like this:

<xsd:unique  name= "uniqueId">
      <xsd:selector xpath="./yourxpath" />
      <xsd:field xpath="@Id"/>
    </xsd:unique>

fpmurphy thank you for your replay,

I know that i need to use the xsd:unique tag,
but i cannot work it out.
can't you look at my xsd and tell me where and how do i need to insert this tag.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.