Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to enforce unique primary key constraint in xsd

843834Dec 21 2008 — edited Dec 22 2008
Hi,

I'm trying to enforce primary key constraint in xsd. I'm using the following xsd to generate the xmls .
<?xml version="1.0" encoding="UTF-8"?>
<!--<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault=
"unqualified">-->
<xs:schema targetNamespace="http://TBD-URI" elementFormDefault="qualified"
 attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Employees">
            <xs:annotation>
                  <xs:documentation>Contains All Employee information</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="Employee" maxOccurs="unbounded">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="Empno" type="xs:int" />
                                          <xs:element name="Ename" type="xs:string" />
                                          <xs:element name="Sal" type="xs:float" />
                                          <xs:element name="Deptno" type="xs:int" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
            <xs:key name="PK_Employee_Empno">
                  <xs:selector xpath=".//Employee" />
                  <xs:field xpath="Empno" />
            </xs:key>
      </xs:element>
</xs:schema>
Here's the generated XML
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="http://TBD-URI"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://TBD-URI file:/C:/Documents%20and%20Settings/ctsuser/workspace/JAXB/src/test/resources/TEST.xsd">
    <Employee>
        <Empno>0</Empno>
        <Ename>Ename0</Ename>
        <Sal>0</Sal>
        <Deptno>0</Deptno>
    </Employee>
    <Employee>
        <Empno>0</Empno>
        <Ename>Ename1</Ename>
        <Sal>0</Sal>
        <Deptno>0</Deptno>
    </Employee>
</Employees>
The generated XML allow the same Empno on which the primary key constraint has been defined. I'll appreciate if someone can provide pointers on how to enforce this constraint so that it doesn't allow the same Empno to be repeated.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2009
Added on Dec 21 2008
2 comments
157 views