0

Can we have some rule in XSD which says: All those elements having ID as its attribute should treat it as unique ID so that other elements should not use that ID. How to apply that as getElementById("id") works only after that.

Thanks

Chandan
  • 229
  • 1
  • 5
  • 14

1 Answers1

1

If you have only digits for your identifier, you can't use xs:id. Then here is a sample schema :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="root" type="rootType">
        <xs:key name="attritemIdentifier">
            <xs:selector xpath="item"/>
            <xs:field xpath="@XYZ"/>
        </xs:key>
    </xs:element>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element name="item" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="XYZ" type="xs:string"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

And here is a valid instance :

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item XYZ="12345">item0</item>
    <item XYZ="XYZ1">item1</item>
    <item XYZ="XYZ2">item2</item>
    <item XYZ="XYZ3">item3</item>
    <item XYZ="XYZ4">item4</item>
    <item XYZ="XYZ5">item5</item>
    <item XYZ="XYZ9">item6</item>
    <item XYZ="XYZ7">item7</item>
</root>

As soon as you have to attribute XYZ with the same value, you'll get the following error :

cvc-identity-constraint.4.2.2: Duplicate key value [XYZ9] declared for identity constraint "attritemIdentifier" of element "root"

Vincent Biragnet
  • 2,950
  • 15
  • 22
  • If I want that if any of my element have ID attribute then it should be of type xsd:ID then how can I do that .My ID's will be alphanumeric always. – Chandan Nov 25 '11 at 13:20
  • You can use ID for the attribute's name but not using the xs:Id type as soon as you begin with digits. You can replace the attribute in my schema like that : – Vincent Biragnet Nov 25 '11 at 13:23
  • exaclty...I am using the above " " in my XSD...so that it should be used as a unique ID by default...but then I am not able to use getElementById on my XML doc , it is returning null..may be bcoz I have some ID having duplicate values...but I am not getting any validation errors in code. – Chandan Nov 25 '11 at 13:27
  • You won't have any validation error unless you specify the xs:key mechanism as above in your schema. The name of the attribute is not enough to tell that the value has to be unique. – Vincent Biragnet Nov 25 '11 at 13:29
  • can you give the above xs:key for that applies to all the elements in xml doc , and is xs:key msdn specific – Chandan Nov 25 '11 at 13:49
  • If you use xs:key on all elements, all elements must have an ID elements. If all elements don't hav one; you should use xs:unique the same way. May be try xs:unique with .//* in the path attribute of the selector. xs:key is not implementation dependant, it's in the w3c recommandation. – Vincent Biragnet Nov 25 '11 at 14:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5347/discussion-between-vincent-biragnet-and-chandan) – Vincent Biragnet Nov 25 '11 at 14:01