Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » Help on Adding Identity Constraint to an element
Help on Adding Identity Constraint to an element [message #24089] Thu, 03 July 2003 06:49 Go to next message
Eclipse UserFriend
Originally posted by: prabhakar.arunagiri.ssiworldwide.com

I am using eclipse to automatically generate a new schema (ie without gui
and using java) - wanted to know how to implement the following unique,key
and keyref constraints for a element as below

<xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
<xsd:unique name="PERSON_PK">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:unique>
<xsd:key name="PERSON_KEY">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:key>
<xsd:keyref name="PERSON_FK" refer="PERSON_ID">
<xsd:selector xpath="MAILING_ADDRESS"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:keyref>
</xsd:element>
<xsd:complexType name="ROOT_ELEMENT">
<xsd:sequence>
........ and expanding the complex type mentioned above

I had seen the XSDIdentityConstraintDefinition class with xpath, selector
etc and know that the functionality can be implemented.
Couldnt really add it to the schema (getting some exception or the
other)as there is no similar kind of example implementation

Request to help in this regard( with a code snippet may be!)


thanks & regards
prabhakar
Re: Help on Adding Identity Constraint to an element [message #24200 is a reply to message #24089] Thu, 03 July 2003 14:07 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

--------------5F8431D81A59BA8EA87DDEF0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Prabhakar,

I'm updating XSDPrototypicalSchema to include this example:

public void initializeSimpleRecursiveElementDeclaration()

{
// Create an element declaration and name it
simpleRecursiveElementDeclaration.
//
XSDElementDeclaration simpleRecursiveElementDeclaration =
xsdFactory.createXSDElementDeclaration();

simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");

// Create an annotation placeholder and set it to the
element.
//
XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
simpleRecursiveElementDeclaration.setAnnotation(annotation);

// Set the element type definition to complex
SimpleRecursiveComplexTypeDefinition from this schema.
//
simpleRecursiveElementDeclaration.setTypeDefinition

(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));

// Create a unique identity constraint called unique.
//
XSDIdentityConstraintDefinition unique =
xsdFactory.createXSDIdentityConstraintDefinition();

unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);

unique.setName("unique");
XSDXPathDefinition uniqueSelector =
xsdFactory.createXSDXPathDefinition();
uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
uniqueSelector.setValue("simpleRecursiveElementDeclaration");

unique.setSelector(uniqueSelector);
XSDXPathDefinition uniqueField =
xsdFactory.createXSDXPathDefinition();
uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);

uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
unique.getFields().add(uniqueField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);

// Create a key identity constraint called key.
//
XSDIdentityConstraintDefinition key =
xsdFactory.createXSDIdentityConstraintDefinition();

key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);

key.setName("key");
XSDXPathDefinition keySelector =
xsdFactory.createXSDXPathDefinition();
keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
keySelector.setValue("simpleRecursiveElementDeclaration");
key.setSelector(keySelector);
XSDXPathDefinition keyField =
xsdFactory.createXSDXPathDefinition();
keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
keyField.setValue("simpleAttributeDeclarationGroupMember");
key.getFields().add(keyField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);

// Create a keyref identity constraint called keyref that
references key.
//
XSDIdentityConstraintDefinition keyref =
xsdFactory.createXSDIdentityConstraintDefinition();

keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);

keyref.setName("keyref");
XSDXPathDefinition keyrefSelector =
xsdFactory.createXSDXPathDefinition();
keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
keyrefSelector.setValue("simpleRecursiveElementDeclaration");

keyref.setSelector(keyrefSelector);
XSDXPathDefinition keyrefField =
xsdFactory.createXSDXPathDefinition();
keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
keyrefField.setValue("simpleAttributeDeclaration");
keyref.setReferencedKey(key);
keyref.getFields().add(keyrefField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);

// Add the element to the schema.
//

prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);

// This is the result that is produced.
//
// <xsd:element name="simpleRecursiveElementDeclaration"
type="PTS:SimpleRecursiveComplexTypeDefinition">
// <xsd:annotation/>
// <xsd:unique name="unique">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclarationGroupMember"/>
// </xsd:unique>
// <xsd:key name="key">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclarationGroupMember"/>
// </xsd:key>
// <xsd:keyref name="keyref" refer="PTS:key">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclaration"/>
// </xsd:keyref>
// </xsd:element>
}

I noticed that if setReferenceKey is not called for a KEYREF, I get this
exception:

java.lang.NullPointerException
at
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)

at
org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)

at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)

at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)

at
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)

at
org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)

at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)

at
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)

at
org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
at
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)

at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)

at java.lang.reflect.Method.invoke(Native Method)
at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
at org.eclipse.core.launcher.Main.run(Main.java:747)
at org.eclipse.core.launcher.Main.main(Main.java:583)

So I'll also fix that bug like this:

> etools-diff XSDIdentityConstraintDefinitionImpl.java
786c786
< niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
---
> niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
theReferencedKey.getURI());


You can avoid this bug by making sure the referenced key is set for a
KEYREF...


prabhakar wrote:

> I am using eclipse to automatically generate a new schema (ie without gui
> and using java) - wanted to know how to implement the following unique,key
> and keyref constraints for a element as below
>
> <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <xsd:unique name="PERSON_PK">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:unique>
> <xsd:key name="PERSON_KEY">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:key>
> <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:keyref>
> </xsd:element>
> <xsd:complexType name="ROOT_ELEMENT">
> <xsd:sequence>
> ....... and expanding the complex type mentioned above
>
> I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> etc and know that the functionality can be implemented.
> Couldnt really add it to the schema (getting some exception or the
> other)as there is no similar kind of example implementation
>
> Request to help in this regard( with a code snippet may be!)
>
> thanks & regards
> prabhakar

--------------5F8431D81A59BA8EA87DDEF0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Prabhakar,
<p>I'm updating XSDPrototypicalSchema to include this example:
<blockquote><tt>public void initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
<p><tt>&nbsp;&nbsp;&nbsp;&nbsp; {</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create an element declaration
and name it simpleRecursiveElementDeclaration.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDElementDeclaration simpleRecursiveElementDeclaration
= xsdFactory.createXSDElementDeclaration();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");</tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create an annotation placeholder
and set it to the element.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDAnnotation annotation =
xsdFactory.createXSDAnnotation();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setAnnotation(annotation); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Set the element type definition
to complex SimpleRecursiveComplexTypeDefinition from this schema.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setTypeDefinition</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; (prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));</tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a unique identity
constraint called unique.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
unique = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setName("unique");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition uniqueSelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setSelector(uniqueSelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition uniqueField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueField.setValue("simpleAttributeDeclarationGroupMember ");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.getFields().add(uniqueField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a key identity constraint
called key.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
key = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setName("key");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keySelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);</tt >
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keySelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setSelector(keySelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyField.setValue("simpleAttributeDeclarationGroupMember"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.getFields().add(keyField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a keyref identity
constraint called keyref that references key.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
keyref = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setName("keyref");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyrefSelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setSelector(keyrefSelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyrefField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefField.setValue("simpleAttributeDeclaration");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setReferencedKey(key);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.getFields().add(keyrefField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add the element to the schema.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prototypeSchema.getContents().add(simpleRecursiveElementDecl aration); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This is the&nbsp; result
that is produced.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="simpleRecursiveElementDeclaration" type="PTS:SimpleRecursiveComplexTypeDefinition"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:annotation/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:unique name="unique"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:unique></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:key name="key"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:key></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:keyref name="keyref" refer="PTS:key"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:keyref></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:element></tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; }</tt></blockquote>
I noticed that if setReferenceKey is not called for a KEYREF, I get this
exception:
<blockquote>java.lang.NullPointerException
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.&lt;init>(XSDPrototypicalSchema.java:184)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at java.lang.reflect.Method.invoke(Native
Method)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.run(Main.java:747)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.main(Main.java:583)</blockquote >
So I'll also fix that bug like this:
<blockquote>> etools-diff XSDIdentityConstraintDefinitionImpl.java
<br>786c786
<br> &lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&a mp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp; niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
<br>---
<br>> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null : theReferencedKey.getURI());
<br>&nbsp;</blockquote>
You can avoid this bug by making sure the referenced key is set for a KEYREF...
<br>&nbsp;
<p>prabhakar wrote:
<blockquote TYPE=CITE>I am using eclipse to automatically generate a new
schema (ie without gui
<br>and using java) - wanted to know how to implement the following unique,key
<br>and keyref constraints for a element as below
<p>&lt;xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:unique&nbsp; name="PERSON_PK">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="PERSON"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:unique>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:key name="PERSON_KEY">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="PERSON"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:key>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:keyref name="PERSON_FK" refer="PERSON_ID">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="MAILING_ADDRESS"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:keyref>
<br>&lt;/xsd:element>
<br>&lt;xsd:complexType name="ROOT_ELEMENT">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:sequence>
<br>....... and expanding the complex type mentioned above
<p>I had seen the XSDIdentityConstraintDefinition class with xpath, selector
<br>etc and know that the functionality can be implemented.
<br>Couldnt really add it to the schema (getting some exception or the
<br>other)as there is no similar kind of example implementation
<p>Request to help in this regard( with a code snippet may be!)
<p>thanks &amp; regards
<br>prabhakar</blockquote>
</html>

--------------5F8431D81A59BA8EA87DDEF0--
Re: Help on Adding Identity Constraint to an element [message #24326 is a reply to message #24200] Fri, 04 July 2003 09:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: prabhakar.arunagiri.ssiworldwide.com

Ed,
Thanks - worked like a charm - but i have another problem - when i was
checking the schema using XSDDiagnostics iam getting the following error

XSD: Key reference '#PERSON_K' is unresolved

(The same schema is working well and was able to validate XML doc data
successfully - when i was cross testing with XML SPY tool!)

the schema generated is ------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">text</xsd:documentation>
</xsd:annotation>
<xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
<xsd:unique name="PERSON_PK">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:unique>
<xsd:key name="PERSON_K">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:key>
<xsd:keyref name="PERSON_MAK" refer="PERSON_K">
<xsd:selector xpath="MAILING_ADDRESS"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:keyref>
</xsd:element>
<xsd:complexType name="ROOT_ELEMENT">
....................... and so on

request help on the problem . (looks like its the last hurdle left before
our program can deliver a clean error free schema!)

Thanks once again for the solution given-

regards
prabhakar



Ed Merks wrote:


> --------------5F8431D81A59BA8EA87DDEF0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> Prabhakar,

> I'm updating XSDPrototypicalSchema to include this example:

> public void initializeSimpleRecursiveElementDeclaration()

> {
> // Create an element declaration and name it
> simpleRecursiveElementDeclaration.
> //
> XSDElementDeclaration simpleRecursiveElementDeclaration =
> xsdFactory.createXSDElementDeclaration();

>
simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");

> // Create an annotation placeholder and set it to the
> element.
> //
> XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
> simpleRecursiveElementDeclaration.setAnnotation(annotation);

> // Set the element type definition to complex
> SimpleRecursiveComplexTypeDefinition from this schema.
> //
> simpleRecursiveElementDeclaration.setTypeDefinition

>
(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));

> // Create a unique identity constraint called unique.
> //
> XSDIdentityConstraintDefinition unique =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);

> unique.setName("unique");
> XSDXPathDefinition uniqueSelector =
> xsdFactory.createXSDXPathDefinition();
> uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> uniqueSelector.setValue("simpleRecursiveElementDeclaration");

> unique.setSelector(uniqueSelector);
> XSDXPathDefinition uniqueField =
> xsdFactory.createXSDXPathDefinition();
> uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);

> uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
> unique.getFields().add(uniqueField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);

> // Create a key identity constraint called key.
> //
> XSDIdentityConstraintDefinition key =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);

> key.setName("key");
> XSDXPathDefinition keySelector =
> xsdFactory.createXSDXPathDefinition();
> keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> keySelector.setValue("simpleRecursiveElementDeclaration");
> key.setSelector(keySelector);
> XSDXPathDefinition keyField =
> xsdFactory.createXSDXPathDefinition();
> keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> keyField.setValue("simpleAttributeDeclarationGroupMember");
> key.getFields().add(keyField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);

> // Create a keyref identity constraint called keyref that
> references key.
> //
> XSDIdentityConstraintDefinition keyref =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);

> keyref.setName("keyref");
> XSDXPathDefinition keyrefSelector =
> xsdFactory.createXSDXPathDefinition();
> keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> keyrefSelector.setValue("simpleRecursiveElementDeclaration");

> keyref.setSelector(keyrefSelector);
> XSDXPathDefinition keyrefField =
> xsdFactory.createXSDXPathDefinition();
> keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> keyrefField.setValue("simpleAttributeDeclaration");
> keyref.setReferencedKey(key);
> keyref.getFields().add(keyrefField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);

> // Add the element to the schema.
> //

> prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);

> // This is the result that is produced.
> //
> // <xsd:element name="simpleRecursiveElementDeclaration"
> type="PTS:SimpleRecursiveComplexTypeDefinition">
> // <xsd:annotation/>
> // <xsd:unique name="unique">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclarationGroupMember"/>
> // </xsd:unique>
> // <xsd:key name="key">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclarationGroupMember"/>
> // </xsd:key>
> // <xsd:keyref name="keyref" refer="PTS:key">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclaration"/>
> // </xsd:keyref>
> // </xsd:element>
> }

> I noticed that if setReferenceKey is not called for a KEYREF, I get this
> exception:

> java.lang.NullPointerException
> at
>
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)

> at
> org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)

> at
>
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)

> at
>
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)

> at
>
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)

> at
> org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)

> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)

> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)

> at
> org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> at
>
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)

> at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)

> at java.lang.reflect.Method.invoke(Native Method)
> at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> at org.eclipse.core.launcher.Main.run(Main.java:747)
> at org.eclipse.core.launcher.Main.main(Main.java:583)

> So I'll also fix that bug like this:

> > etools-diff XSDIdentityConstraintDefinitionImpl.java
> 786c786
> < niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> ---
> > niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
> theReferencedKey.getURI());


> You can avoid this bug by making sure the referenced key is set for a
> KEYREF...


> prabhakar wrote:

> > I am using eclipse to automatically generate a new schema (ie without gui
> > and using java) - wanted to know how to implement the following unique,key
> > and keyref constraints for a element as below
> >
> > <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> > <xsd:unique name="PERSON_PK">
> > <xsd:selector xpath="PERSON"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:unique>
> > <xsd:key name="PERSON_KEY">
> > <xsd:selector xpath="PERSON"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:key>
> > <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> > <xsd:selector xpath="MAILING_ADDRESS"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:keyref>
> > </xsd:element>
> > <xsd:complexType name="ROOT_ELEMENT">
> > <xsd:sequence>
> > ....... and expanding the complex type mentioned above
> >
> > I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> > etc and know that the functionality can be implemented.
> > Couldnt really add it to the schema (getting some exception or the
> > other)as there is no similar kind of example implementation
> >
> > Request to help in this regard( with a code snippet may be!)
> >
> > thanks & regards
> > prabhakar

> --------------5F8431D81A59BA8EA87DDEF0
> Content-Type: text/html; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
> <html>
> Prabhakar,
> <p>I'm updating XSDPrototypicalSchema to include this example:
> <blockquote><tt>public void
initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
> <p><tt>ÿÿÿÿ {</tt>
> <br><tt>ÿÿÿÿÿÿ // Create an element declaration
> and name it simpleRecursiveElementDeclaration.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDElementDeclaration simpleRecursiveElementDeclaration
> = xsdFactory.createXSDElementDeclaration();</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");</tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create an annotation placeholder
> and set it to the element.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDAnnotation annotation =
> xsdFactory.createXSDAnnotation();</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.setAnnotation(annotation); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Set the element type definition
> to complex SimpleRecursiveComplexTypeDefinition from this schema.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ simpleRecursiveElementDeclaration.setTypeDefinition</tt>
> <br><tt>ÿÿÿÿÿÿÿÿ
(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));</tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a unique identity
> constraint called unique.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> unique = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ unique.setName("unique");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition uniqueSelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ unique.setSelector(uniqueSelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition uniqueField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueField.setValue("simpleAttributeDeclarationGroupMember ");</tt>
> <br><tt>ÿÿÿÿÿÿ unique.getFields().add(uniqueField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a key identity constraint
> called key.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> key = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ key.setName("key");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keySelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);</tt >
> <br><tt>ÿÿÿÿÿÿ
keySelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ key.setSelector(keySelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ
keyField.setValue("simpleAttributeDeclarationGroupMember"); </tt>
> <br><tt>ÿÿÿÿÿÿ key.getFields().add(keyField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a keyref identity
> constraint called keyref that references key.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> keyref = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setName("keyref");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyrefSelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ
keyrefSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setSelector(keyrefSelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyrefField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ keyrefField.setValue("simpleAttributeDeclaration");</tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setReferencedKey(key);</tt>
> <br><tt>ÿÿÿÿÿÿ keyref.getFields().add(keyrefField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Add the element to the schema.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ
prototypeSchema.getContents().add(simpleRecursiveElementDecl aration); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // This is theÿ result
> that is produced.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿ
> <xsd:element name="simpleRecursiveElementDeclaration"
type="PTS:SimpleRecursiveComplexTypeDefinition"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:annotation/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:unique name="unique"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:unique></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:key name="key"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:key></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:keyref name="keyref" refer="PTS:key"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:keyref></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿ
> </xsd:element></tt>
> <br><tt>ÿÿÿÿ }</tt></blockquote>
> I noticed that if setReferenceKey is not called for a KEYREF, I get this
> exception:
> <blockquote>java.lang.NullPointerException
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
> <br>ÿÿÿÿÿÿÿ at java.lang.reflect.Method.invoke(Native
> Method)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.launcher.Main.run(Main.java:747)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.core.launcher.Main.main(Main.java:583)</blockquote >
> So I'll also fix that bug like this:
> <blockquote>> etools-diff XSDIdentityConstraintDefinitionImpl.java
> <br>786c786
> <br><ÿÿÿÿÿÿÿÿÿÿ niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> <br>---
> <br>>ÿÿÿÿÿÿÿÿÿÿ niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
theReferencedKey.getURI());
> <br>ÿ</blockquote>
> You can avoid this bug by making sure the referenced key is set for a
KEYREF...
> <br>ÿ
> <p>prabhakar wrote:
> <blockquote TYPE=CITE>I am using eclipse to automatically generate a new
> schema (ie without gui
> <br>and using java) - wanted to know how to implement the following
unique,key
> <br>and keyref constraints for a element as below
> <p><xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:uniqueÿ name="PERSON_PK">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="PERSON"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:unique>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:key name="PERSON_KEY">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="PERSON"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:key>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:keyref>
> <br></xsd:element>
> <br><xsd:complexType name="ROOT_ELEMENT">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:sequence>
> <br>....... and expanding the complex type mentioned above
> <p>I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> <br>etc and know that the functionality can be implemented.
> <br>Couldnt really add it to the schema (getting some exception or the
> <br>other)as there is no similar kind of example implementation
> <p>Request to help in this regard( with a code snippet may be!)
> <p>thanks & regards
> <br>prabhakar</blockquote>
> </html>

> --------------5F8431D81A59BA8EA87DDEF0--
Re: Help on Adding Identity Constraint to an element [message #24422 is a reply to message #24326] Fri, 04 July 2003 11:31 Go to previous message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

--------------3EB7D7AE192E58C2ADEE4E50
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Prabhakar,

When I open the sample schema you show below in the Sample XML Schema Editor, I don't get any diagnostics:

[Image]

I'm not sure what version of XSD you are using or what you did to obtain the diagnostic you mention.


prabhakar wrote:

> Ed,
> Thanks - worked like a charm - but i have another problem - when i was
> checking the schema using XSDDiagnostics iam getting the following error
>
> XSD: Key reference '#PERSON_K' is unresolved
>
> (The same schema is working well and was able to validate XML doc data
> successfully - when i was cross testing with XML SPY tool!)
>
> the schema generated is ------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <xsd:annotation>
> <xsd:documentation xml:lang="en">text</xsd:documentation>
> </xsd:annotation>
> <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <xsd:unique name="PERSON_PK">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:unique>
> <xsd:key name="PERSON_K">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:key>
> <xsd:keyref name="PERSON_MAK" refer="PERSON_K">
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:keyref>
> </xsd:element>
> <xsd:complexType name="ROOT_ELEMENT">
> ...................... and so on
>
> request help on the problem . (looks like its the last hurdle left before
> our program can deliver a clean error free schema!)
>
> Thanks once again for the solution given-
>
> regards
> prabhakar
>
> Ed Merks wrote:
>
> > --------------5F8431D81A59BA8EA87DDEF0
> > Content-Type: text/plain; charset=us-ascii
> > Content-Transfer-Encoding: 7bit
>
> > Prabhakar,
>
> > I'm updating XSDPrototypicalSchema to include this example:
>
> > public void initializeSimpleRecursiveElementDeclaration()
>
> > {
> > // Create an element declaration and name it
> > simpleRecursiveElementDeclaration.
> > //
> > XSDElementDeclaration simpleRecursiveElementDeclaration =
> > xsdFactory.createXSDElementDeclaration();
>
> >
> simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");
>
> > // Create an annotation placeholder and set it to the
> > element.
> > //
> > XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
> > simpleRecursiveElementDeclaration.setAnnotation(annotation);
>
> > // Set the element type definition to complex
> > SimpleRecursiveComplexTypeDefinition from this schema.
> > //
> > simpleRecursiveElementDeclaration.setTypeDefinition
>
> >
> (prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));
>
> > // Create a unique identity constraint called unique.
> > //
> > XSDIdentityConstraintDefinition unique =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);
>
> > unique.setName("unique");
> > XSDXPathDefinition uniqueSelector =
> > xsdFactory.createXSDXPathDefinition();
> > uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > uniqueSelector.setValue("simpleRecursiveElementDeclaration");
>
> > unique.setSelector(uniqueSelector);
> > XSDXPathDefinition uniqueField =
> > xsdFactory.createXSDXPathDefinition();
> > uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);
>
> > uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
> > unique.getFields().add(uniqueField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);
>
> > // Create a key identity constraint called key.
> > //
> > XSDIdentityConstraintDefinition key =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);
>
> > key.setName("key");
> > XSDXPathDefinition keySelector =
> > xsdFactory.createXSDXPathDefinition();
> > keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > keySelector.setValue("simpleRecursiveElementDeclaration");
> > key.setSelector(keySelector);
> > XSDXPathDefinition keyField =
> > xsdFactory.createXSDXPathDefinition();
> > keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> > keyField.setValue("simpleAttributeDeclarationGroupMember");
> > key.getFields().add(keyField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);
>
> > // Create a keyref identity constraint called keyref that
> > references key.
> > //
> > XSDIdentityConstraintDefinition keyref =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);
>
> > keyref.setName("keyref");
> > XSDXPathDefinition keyrefSelector =
> > xsdFactory.createXSDXPathDefinition();
> > keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > keyrefSelector.setValue("simpleRecursiveElementDeclaration");
>
> > keyref.setSelector(keyrefSelector);
> > XSDXPathDefinition keyrefField =
> > xsdFactory.createXSDXPathDefinition();
> > keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> > keyrefField.setValue("simpleAttributeDeclaration");
> > keyref.setReferencedKey(key);
> > keyref.getFields().add(keyrefField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);
>
> > // Add the element to the schema.
> > //
>
> > prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);
>
> > // This is the result that is produced.
> > //
> > // <xsd:element name="simpleRecursiveElementDeclaration"
> > type="PTS:SimpleRecursiveComplexTypeDefinition">
> > // <xsd:annotation/>
> > // <xsd:unique name="unique">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclarationGroupMember"/>
> > // </xsd:unique>
> > // <xsd:key name="key">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclarationGroupMember"/>
> > // </xsd:key>
> > // <xsd:keyref name="keyref" refer="PTS:key">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclaration"/>
> > // </xsd:keyref>
> > // </xsd:element>
> > }
>
> > I noticed that if setReferenceKey is not called for a KEYREF, I get this
> > exception:
>
> > java.lang.NullPointerException
> > at
> >
> org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
>
> > at
> > org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
>
> > at
> > org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
>
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
>
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)
>
> > at
> > org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> > at
> >
> org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
>
> > at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
>
> > at java.lang.reflect.Method.invoke(Native Method)
> > at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> > at org.eclipse.core.launcher.Main.run(Main.java:747)
> > at org.eclipse.core.launcher.Main.main(Main.java:583)
>
> > So I'll also fix that bug like this:
>
> > > etools-diff XSDIdentityConstraintDefinitionImpl.java
> > 786c786
> > < niceSetAttributeURIValue(theElement,
> > XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> > ---
> > > niceSetAttributeURIValue(theElement,
> > XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
> > theReferencedKey.getURI());
>
> > You can avoid this bug by making sure the referenced key is set for a
> > KEYREF...
>
> > prabhakar wrote:
>
> > > I am using eclipse to automatically generate a new schema (ie without gui
> > > and using java) - wanted to know how to implement the following unique,key
> > > and keyref constraints for a element as below
> > >
> > > <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> > > <xsd:unique name="PERSON_PK">
> > > <xsd:selector xpath="PERSON"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:unique>
> > > <xsd:key name="PERSON_KEY">
> > > <xsd:selector xpath="PERSON"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:key>
> > > <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> > > <xsd:selector xpath="MAILING_ADDRESS"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:keyref>
> > > </xsd:element>
> > > <xsd:complexType name="ROOT_ELEMENT">
> > > <xsd:sequence>
> > > ....... and expanding the complex type mentioned above
> > >
> > > I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> > > etc and know that the functionality can be implemented.
> > > Couldnt really add it to the schema (getting some exception or the
> > > other)as there is no similar kind of example implementation
> > >
> > > Request to help in this regard( with a code snippet may be!)
> > >
> > > thanks & regards
> > > prabhakar
>
> > --------------5F8431D81A59BA8EA87DDEF0
> > Content-Type: text/html; charset=us-ascii
> > Content-Transfer-Encoding: 7bit
>
> > <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
> > <html>
> > Prabhakar,
> > <p>I'm updating XSDPrototypicalSchema to include this example:
> > <blockquote><tt>public void
> initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
> > <p><tt>
Re: Help on Adding Identity Constraint to an element [message #573534 is a reply to message #24089] Thu, 03 July 2003 14:07 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
--------------5F8431D81A59BA8EA87DDEF0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Prabhakar,

I'm updating XSDPrototypicalSchema to include this example:

public void initializeSimpleRecursiveElementDeclaration()

{
// Create an element declaration and name it
simpleRecursiveElementDeclaration.
//
XSDElementDeclaration simpleRecursiveElementDeclaration =
xsdFactory.createXSDElementDeclaration();

simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");

// Create an annotation placeholder and set it to the
element.
//
XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
simpleRecursiveElementDeclaration.setAnnotation(annotation);

// Set the element type definition to complex
SimpleRecursiveComplexTypeDefinition from this schema.
//
simpleRecursiveElementDeclaration.setTypeDefinition

(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));

// Create a unique identity constraint called unique.
//
XSDIdentityConstraintDefinition unique =
xsdFactory.createXSDIdentityConstraintDefinition();

unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);

unique.setName("unique");
XSDXPathDefinition uniqueSelector =
xsdFactory.createXSDXPathDefinition();
uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
uniqueSelector.setValue("simpleRecursiveElementDeclaration");

unique.setSelector(uniqueSelector);
XSDXPathDefinition uniqueField =
xsdFactory.createXSDXPathDefinition();
uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);

uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
unique.getFields().add(uniqueField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);

// Create a key identity constraint called key.
//
XSDIdentityConstraintDefinition key =
xsdFactory.createXSDIdentityConstraintDefinition();

key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);

key.setName("key");
XSDXPathDefinition keySelector =
xsdFactory.createXSDXPathDefinition();
keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
keySelector.setValue("simpleRecursiveElementDeclaration");
key.setSelector(keySelector);
XSDXPathDefinition keyField =
xsdFactory.createXSDXPathDefinition();
keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
keyField.setValue("simpleAttributeDeclarationGroupMember");
key.getFields().add(keyField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);

// Create a keyref identity constraint called keyref that
references key.
//
XSDIdentityConstraintDefinition keyref =
xsdFactory.createXSDIdentityConstraintDefinition();

keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);

keyref.setName("keyref");
XSDXPathDefinition keyrefSelector =
xsdFactory.createXSDXPathDefinition();
keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
keyrefSelector.setValue("simpleRecursiveElementDeclaration");

keyref.setSelector(keyrefSelector);
XSDXPathDefinition keyrefField =
xsdFactory.createXSDXPathDefinition();
keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
keyrefField.setValue("simpleAttributeDeclaration");
keyref.setReferencedKey(key);
keyref.getFields().add(keyrefField);

simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);

// Add the element to the schema.
//

prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);

// This is the result that is produced.
//
// <xsd:element name="simpleRecursiveElementDeclaration"
type="PTS:SimpleRecursiveComplexTypeDefinition">
// <xsd:annotation/>
// <xsd:unique name="unique">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclarationGroupMember"/>
// </xsd:unique>
// <xsd:key name="key">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclarationGroupMember"/>
// </xsd:key>
// <xsd:keyref name="keyref" refer="PTS:key">
// <xsd:selector
xpath="simpleRecursiveElementDeclaration"/>
// <xsd:field
xpath="simpleAttributeDeclaration"/>
// </xsd:keyref>
// </xsd:element>
}

I noticed that if setReferenceKey is not called for a KEYREF, I get this
exception:

java.lang.NullPointerException
at
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)

at
org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)

at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)

at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)

at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)

at
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)

at
org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)

at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)

at
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)

at
org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
at
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)

at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)

at java.lang.reflect.Method.invoke(Native Method)
at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
at org.eclipse.core.launcher.Main.run(Main.java:747)
at org.eclipse.core.launcher.Main.main(Main.java:583)

So I'll also fix that bug like this:

> etools-diff XSDIdentityConstraintDefinitionImpl.java
786c786
< niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
---
> niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
theReferencedKey.getURI());


You can avoid this bug by making sure the referenced key is set for a
KEYREF...


prabhakar wrote:

> I am using eclipse to automatically generate a new schema (ie without gui
> and using java) - wanted to know how to implement the following unique,key
> and keyref constraints for a element as below
>
> <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <xsd:unique name="PERSON_PK">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:unique>
> <xsd:key name="PERSON_KEY">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:key>
> <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:keyref>
> </xsd:element>
> <xsd:complexType name="ROOT_ELEMENT">
> <xsd:sequence>
> ....... and expanding the complex type mentioned above
>
> I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> etc and know that the functionality can be implemented.
> Couldnt really add it to the schema (getting some exception or the
> other)as there is no similar kind of example implementation
>
> Request to help in this regard( with a code snippet may be!)
>
> thanks & regards
> prabhakar

--------------5F8431D81A59BA8EA87DDEF0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Prabhakar,
<p>I'm updating XSDPrototypicalSchema to include this example:
<blockquote><tt>public void initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
<p><tt>&nbsp;&nbsp;&nbsp;&nbsp; {</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create an element declaration
and name it simpleRecursiveElementDeclaration.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDElementDeclaration simpleRecursiveElementDeclaration
= xsdFactory.createXSDElementDeclaration();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");</tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create an annotation placeholder
and set it to the element.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDAnnotation annotation =
xsdFactory.createXSDAnnotation();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setAnnotation(annotation); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Set the element type definition
to complex SimpleRecursiveComplexTypeDefinition from this schema.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.setTypeDefinition</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; (prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));</tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a unique identity
constraint called unique.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
unique = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setName("unique");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition uniqueSelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.setSelector(uniqueSelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition uniqueField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uniqueField.setValue("simpleAttributeDeclarationGroupMember ");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unique.getFields().add(uniqueField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a key identity constraint
called key.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
key = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setName("key");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keySelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);</tt >
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keySelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.setSelector(keySelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyField.setValue("simpleAttributeDeclarationGroupMember"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.getFields().add(keyField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a keyref identity
constraint called keyref that references key.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDIdentityConstraintDefinition
keyref = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setName("keyref");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyrefSelector
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setSelector(keyrefSelector);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XSDXPathDefinition keyrefField
= xsdFactory.createXSDXPathDefinition();</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyrefField.setValue("simpleAttributeDeclaration");</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.setReferencedKey(key);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyref.getFields().add(keyrefField);</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add the element to the schema.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prototypeSchema.getContents().add(simpleRecursiveElementDecl aration); </tt><tt></tt>
<p><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This is the&nbsp; result
that is produced.</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="simpleRecursiveElementDeclaration" type="PTS:SimpleRecursiveComplexTypeDefinition"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:annotation/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:unique name="unique"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:unique></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:key name="key"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:key></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;xsd:keyref name="keyref" refer="PTS:key"></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
&lt;xsd:field xpath="simpleAttributeDeclaration"/></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
&lt;/xsd:keyref></tt>
<br><tt> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:element></tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; }</tt></blockquote>
I noticed that if setReferenceKey is not called for a KEYREF, I get this
exception:
<blockquote>java.lang.NullPointerException
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.util.XSDPrototypicalSchema.&lt;init>(XSDPrototypicalSchema.java:184)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at java.lang.reflect.Method.invoke(Native
Method)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.run(Main.java:747)
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; at org.eclipse.core.launcher.Main.main(Main.java:583)</blockquote >
So I'll also fix that bug like this:
<blockquote>> etools-diff XSDIdentityConstraintDefinitionImpl.java
<br>786c786
<br> &lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&a mp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp; niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
<br>---
<br>> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; niceSetAttributeURIValue(theElement,
XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null : theReferencedKey.getURI());
<br>&nbsp;</blockquote>
You can avoid this bug by making sure the referenced key is set for a KEYREF...
<br>&nbsp;
<p>prabhakar wrote:
<blockquote TYPE=CITE>I am using eclipse to automatically generate a new
schema (ie without gui
<br>and using java) - wanted to know how to implement the following unique,key
<br>and keyref constraints for a element as below
<p>&lt;xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:unique&nbsp; name="PERSON_PK">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="PERSON"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:unique>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:key name="PERSON_KEY">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="PERSON"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:key>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:keyref name="PERSON_FK" refer="PERSON_ID">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:selector xpath="MAILING_ADDRESS"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:field xpath="PERSON_ID"/>
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;/xsd:keyref>
<br>&lt;/xsd:element>
<br>&lt;xsd:complexType name="ROOT_ELEMENT">
<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;xsd:sequence>
<br>....... and expanding the complex type mentioned above
<p>I had seen the XSDIdentityConstraintDefinition class with xpath, selector
<br>etc and know that the functionality can be implemented.
<br>Couldnt really add it to the schema (getting some exception or the
<br>other)as there is no similar kind of example implementation
<p>Request to help in this regard( with a code snippet may be!)
<p>thanks &amp; regards
<br>prabhakar</blockquote>
</html>

--------------5F8431D81A59BA8EA87DDEF0--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Help on Adding Identity Constraint to an element [message #573667 is a reply to message #24200] Fri, 04 July 2003 09:30 Go to previous message
Eclipse UserFriend
Originally posted by: prabhakar.arunagiri.ssiworldwide.com

Ed,
Thanks - worked like a charm - but i have another problem - when i was
checking the schema using XSDDiagnostics iam getting the following error

XSD: Key reference '#PERSON_K' is unresolved

(The same schema is working well and was able to validate XML doc data
successfully - when i was cross testing with XML SPY tool!)

the schema generated is ------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">text</xsd:documentation>
</xsd:annotation>
<xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
<xsd:unique name="PERSON_PK">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:unique>
<xsd:key name="PERSON_K">
<xsd:selector xpath="PERSON"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:key>
<xsd:keyref name="PERSON_MAK" refer="PERSON_K">
<xsd:selector xpath="MAILING_ADDRESS"/>
<xsd:field xpath="PERSON_ID"/>
</xsd:keyref>
</xsd:element>
<xsd:complexType name="ROOT_ELEMENT">
....................... and so on

request help on the problem . (looks like its the last hurdle left before
our program can deliver a clean error free schema!)

Thanks once again for the solution given-

regards
prabhakar



Ed Merks wrote:


> --------------5F8431D81A59BA8EA87DDEF0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> Prabhakar,

> I'm updating XSDPrototypicalSchema to include this example:

> public void initializeSimpleRecursiveElementDeclaration()

> {
> // Create an element declaration and name it
> simpleRecursiveElementDeclaration.
> //
> XSDElementDeclaration simpleRecursiveElementDeclaration =
> xsdFactory.createXSDElementDeclaration();

>
simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");

> // Create an annotation placeholder and set it to the
> element.
> //
> XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
> simpleRecursiveElementDeclaration.setAnnotation(annotation);

> // Set the element type definition to complex
> SimpleRecursiveComplexTypeDefinition from this schema.
> //
> simpleRecursiveElementDeclaration.setTypeDefinition

>
(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));

> // Create a unique identity constraint called unique.
> //
> XSDIdentityConstraintDefinition unique =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);

> unique.setName("unique");
> XSDXPathDefinition uniqueSelector =
> xsdFactory.createXSDXPathDefinition();
> uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> uniqueSelector.setValue("simpleRecursiveElementDeclaration");

> unique.setSelector(uniqueSelector);
> XSDXPathDefinition uniqueField =
> xsdFactory.createXSDXPathDefinition();
> uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);

> uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
> unique.getFields().add(uniqueField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);

> // Create a key identity constraint called key.
> //
> XSDIdentityConstraintDefinition key =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);

> key.setName("key");
> XSDXPathDefinition keySelector =
> xsdFactory.createXSDXPathDefinition();
> keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> keySelector.setValue("simpleRecursiveElementDeclaration");
> key.setSelector(keySelector);
> XSDXPathDefinition keyField =
> xsdFactory.createXSDXPathDefinition();
> keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> keyField.setValue("simpleAttributeDeclarationGroupMember");
> key.getFields().add(keyField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);

> // Create a keyref identity constraint called keyref that
> references key.
> //
> XSDIdentityConstraintDefinition keyref =
> xsdFactory.createXSDIdentityConstraintDefinition();

>
keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);

> keyref.setName("keyref");
> XSDXPathDefinition keyrefSelector =
> xsdFactory.createXSDXPathDefinition();
> keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> keyrefSelector.setValue("simpleRecursiveElementDeclaration");

> keyref.setSelector(keyrefSelector);
> XSDXPathDefinition keyrefField =
> xsdFactory.createXSDXPathDefinition();
> keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> keyrefField.setValue("simpleAttributeDeclaration");
> keyref.setReferencedKey(key);
> keyref.getFields().add(keyrefField);

>
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);

> // Add the element to the schema.
> //

> prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);

> // This is the result that is produced.
> //
> // <xsd:element name="simpleRecursiveElementDeclaration"
> type="PTS:SimpleRecursiveComplexTypeDefinition">
> // <xsd:annotation/>
> // <xsd:unique name="unique">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclarationGroupMember"/>
> // </xsd:unique>
> // <xsd:key name="key">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclarationGroupMember"/>
> // </xsd:key>
> // <xsd:keyref name="keyref" refer="PTS:key">
> // <xsd:selector
> xpath="simpleRecursiveElementDeclaration"/>
> // <xsd:field
> xpath="simpleAttributeDeclaration"/>
> // </xsd:keyref>
> // </xsd:element>
> }

> I noticed that if setReferenceKey is not called for a KEYREF, I get this
> exception:

> java.lang.NullPointerException
> at
>
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)

> at
> org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)

> at
>
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)

> at
>
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)

> at
>
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)

> at
>
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)

> at
> org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)

> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)

> at
>
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)

> at
> org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> at
>
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)

> at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)

> at java.lang.reflect.Method.invoke(Native Method)
> at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> at org.eclipse.core.launcher.Main.run(Main.java:747)
> at org.eclipse.core.launcher.Main.main(Main.java:583)

> So I'll also fix that bug like this:

> > etools-diff XSDIdentityConstraintDefinitionImpl.java
> 786c786
> < niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> ---
> > niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
> theReferencedKey.getURI());


> You can avoid this bug by making sure the referenced key is set for a
> KEYREF...


> prabhakar wrote:

> > I am using eclipse to automatically generate a new schema (ie without gui
> > and using java) - wanted to know how to implement the following unique,key
> > and keyref constraints for a element as below
> >
> > <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> > <xsd:unique name="PERSON_PK">
> > <xsd:selector xpath="PERSON"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:unique>
> > <xsd:key name="PERSON_KEY">
> > <xsd:selector xpath="PERSON"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:key>
> > <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> > <xsd:selector xpath="MAILING_ADDRESS"/>
> > <xsd:field xpath="PERSON_ID"/>
> > </xsd:keyref>
> > </xsd:element>
> > <xsd:complexType name="ROOT_ELEMENT">
> > <xsd:sequence>
> > ....... and expanding the complex type mentioned above
> >
> > I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> > etc and know that the functionality can be implemented.
> > Couldnt really add it to the schema (getting some exception or the
> > other)as there is no similar kind of example implementation
> >
> > Request to help in this regard( with a code snippet may be!)
> >
> > thanks & regards
> > prabhakar

> --------------5F8431D81A59BA8EA87DDEF0
> Content-Type: text/html; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
> <html>
> Prabhakar,
> <p>I'm updating XSDPrototypicalSchema to include this example:
> <blockquote><tt>public void
initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
> <p><tt>ÿÿÿÿ {</tt>
> <br><tt>ÿÿÿÿÿÿ // Create an element declaration
> and name it simpleRecursiveElementDeclaration.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDElementDeclaration simpleRecursiveElementDeclaration
> = xsdFactory.createXSDElementDeclaration();</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");</tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create an annotation placeholder
> and set it to the element.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDAnnotation annotation =
> xsdFactory.createXSDAnnotation();</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.setAnnotation(annotation); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Set the element type definition
> to complex SimpleRecursiveComplexTypeDefinition from this schema.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ simpleRecursiveElementDeclaration.setTypeDefinition</tt>
> <br><tt>ÿÿÿÿÿÿÿÿ
(prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));</tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a unique identity
> constraint called unique.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> unique = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ unique.setName("unique");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition uniqueSelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ unique.setSelector(uniqueSelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition uniqueField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ
uniqueField.setValue("simpleAttributeDeclarationGroupMember ");</tt>
> <br><tt>ÿÿÿÿÿÿ unique.getFields().add(uniqueField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a key identity constraint
> called key.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> key = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ key.setName("key");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keySelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);</tt >
> <br><tt>ÿÿÿÿÿÿ
keySelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ key.setSelector(keySelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ
keyField.setValue("simpleAttributeDeclarationGroupMember"); </tt>
> <br><tt>ÿÿÿÿÿÿ key.getFields().add(keyField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Create a keyref identity
> constraint called keyref that references key.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ XSDIdentityConstraintDefinition
> keyref = xsdFactory.createXSDIdentityConstraintDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setName("keyref");</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyrefSelector
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ
keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL); </tt>
> <br><tt>ÿÿÿÿÿÿ
keyrefSelector.setValue("simpleRecursiveElementDeclaration"); </tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setSelector(keyrefSelector);</tt>
> <br><tt>ÿÿÿÿÿÿ XSDXPathDefinition keyrefField
> = xsdFactory.createXSDXPathDefinition();</tt>
> <br><tt>ÿÿÿÿÿÿ keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);</tt>
> <br><tt>ÿÿÿÿÿÿ keyrefField.setValue("simpleAttributeDeclaration");</tt>
> <br><tt>ÿÿÿÿÿÿ keyref.setReferencedKey(key);</tt>
> <br><tt>ÿÿÿÿÿÿ keyref.getFields().add(keyrefField);</tt>
> <br><tt>ÿÿÿÿÿÿ
simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // Add the element to the schema.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ
prototypeSchema.getContents().add(simpleRecursiveElementDecl aration); </tt><tt></tt>
> <p><tt>ÿÿÿÿÿÿ // This is theÿ result
> that is produced.</tt>
> <br><tt>ÿÿÿÿÿÿ //</tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿ
> <xsd:element name="simpleRecursiveElementDeclaration"
type="PTS:SimpleRecursiveComplexTypeDefinition"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:annotation/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:unique name="unique"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:unique></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:key name="key"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclarationGroupMember"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:key></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> <xsd:keyref name="keyref" refer="PTS:key"></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="simpleRecursiveElementDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="simpleAttributeDeclaration"/></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿÿÿÿÿ
> </xsd:keyref></tt>
> <br><tt>ÿÿÿÿÿÿ //ÿÿÿÿ
> </xsd:element></tt>
> <br><tt>ÿÿÿÿ }</tt></blockquote>
> I noticed that if setReferenceKey is not called for a KEYREF, I get this
> exception:
> <blockquote>java.lang.NullPointerException
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
> <br>ÿÿÿÿÿÿÿ at java.lang.reflect.Method.invoke(Native
> Method)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> <br>ÿÿÿÿÿÿÿ at org.eclipse.core.launcher.Main.run(Main.java:747)
> <br>ÿÿÿÿÿÿÿ at
org.eclipse.core.launcher.Main.main(Main.java:583)</blockquote >
> So I'll also fix that bug like this:
> <blockquote>> etools-diff XSDIdentityConstraintDefinitionImpl.java
> <br>786c786
> <br><ÿÿÿÿÿÿÿÿÿÿ niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> <br>---
> <br>>ÿÿÿÿÿÿÿÿÿÿ niceSetAttributeURIValue(theElement,
> XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
theReferencedKey.getURI());
> <br>ÿ</blockquote>
> You can avoid this bug by making sure the referenced key is set for a
KEYREF...
> <br>ÿ
> <p>prabhakar wrote:
> <blockquote TYPE=CITE>I am using eclipse to automatically generate a new
> schema (ie without gui
> <br>and using java) - wanted to know how to implement the following
unique,key
> <br>and keyref constraints for a element as below
> <p><xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:uniqueÿ name="PERSON_PK">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="PERSON"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:unique>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:key name="PERSON_KEY">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="PERSON"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:key>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:field xpath="PERSON_ID"/>
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> </xsd:keyref>
> <br></xsd:element>
> <br><xsd:complexType name="ROOT_ELEMENT">
> <br>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
> <xsd:sequence>
> <br>....... and expanding the complex type mentioned above
> <p>I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> <br>etc and know that the functionality can be implemented.
> <br>Couldnt really add it to the schema (getting some exception or the
> <br>other)as there is no similar kind of example implementation
> <p>Request to help in this regard( with a code snippet may be!)
> <p>thanks & regards
> <br>prabhakar</blockquote>
> </html>

> --------------5F8431D81A59BA8EA87DDEF0--
Re: Help on Adding Identity Constraint to an element [message #573750 is a reply to message #24326] Fri, 04 July 2003 11:31 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
--------------3EB7D7AE192E58C2ADEE4E50
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Prabhakar,

When I open the sample schema you show below in the Sample XML Schema Editor, I don't get any diagnostics:

[Image]

I'm not sure what version of XSD you are using or what you did to obtain the diagnostic you mention.


prabhakar wrote:

> Ed,
> Thanks - worked like a charm - but i have another problem - when i was
> checking the schema using XSDDiagnostics iam getting the following error
>
> XSD: Key reference '#PERSON_K' is unresolved
>
> (The same schema is working well and was able to validate XML doc data
> successfully - when i was cross testing with XML SPY tool!)
>
> the schema generated is ------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <xsd:annotation>
> <xsd:documentation xml:lang="en">text</xsd:documentation>
> </xsd:annotation>
> <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> <xsd:unique name="PERSON_PK">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:unique>
> <xsd:key name="PERSON_K">
> <xsd:selector xpath="PERSON"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:key>
> <xsd:keyref name="PERSON_MAK" refer="PERSON_K">
> <xsd:selector xpath="MAILING_ADDRESS"/>
> <xsd:field xpath="PERSON_ID"/>
> </xsd:keyref>
> </xsd:element>
> <xsd:complexType name="ROOT_ELEMENT">
> ...................... and so on
>
> request help on the problem . (looks like its the last hurdle left before
> our program can deliver a clean error free schema!)
>
> Thanks once again for the solution given-
>
> regards
> prabhakar
>
> Ed Merks wrote:
>
> > --------------5F8431D81A59BA8EA87DDEF0
> > Content-Type: text/plain; charset=us-ascii
> > Content-Transfer-Encoding: 7bit
>
> > Prabhakar,
>
> > I'm updating XSDPrototypicalSchema to include this example:
>
> > public void initializeSimpleRecursiveElementDeclaration()
>
> > {
> > // Create an element declaration and name it
> > simpleRecursiveElementDeclaration.
> > //
> > XSDElementDeclaration simpleRecursiveElementDeclaration =
> > xsdFactory.createXSDElementDeclaration();
>
> >
> simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration ");
>
> > // Create an annotation placeholder and set it to the
> > element.
> > //
> > XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
> > simpleRecursiveElementDeclaration.setAnnotation(annotation);
>
> > // Set the element type definition to complex
> > SimpleRecursiveComplexTypeDefinition from this schema.
> > //
> > simpleRecursiveElementDeclaration.setTypeDefinition
>
> >
> (prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition "));
>
> > // Create a unique identity constraint called unique.
> > //
> > XSDIdentityConstraintDefinition unique =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> unique.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.UNIQUE_LITERAL);
>
> > unique.setName("unique");
> > XSDXPathDefinition uniqueSelector =
> > xsdFactory.createXSDXPathDefinition();
> > uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > uniqueSelector.setValue("simpleRecursiveElementDeclaration");
>
> > unique.setSelector(uniqueSelector);
> > XSDXPathDefinition uniqueField =
> > xsdFactory.createXSDXPathDefinition();
> > uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);
>
> > uniqueField.setValue("simpleAttributeDeclarationGroupMember ");
> > unique.getFields().add(uniqueField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(unique);
>
> > // Create a key identity constraint called key.
> > //
> > XSDIdentityConstraintDefinition key =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> key.setIdentityConstraintCategory(XSDIdentityConstraintCateg ory.KEY_LITERAL);
>
> > key.setName("key");
> > XSDXPathDefinition keySelector =
> > xsdFactory.createXSDXPathDefinition();
> > keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > keySelector.setValue("simpleRecursiveElementDeclaration");
> > key.setSelector(keySelector);
> > XSDXPathDefinition keyField =
> > xsdFactory.createXSDXPathDefinition();
> > keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> > keyField.setValue("simpleAttributeDeclarationGroupMember");
> > key.getFields().add(keyField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(key);
>
> > // Create a keyref identity constraint called keyref that
> > references key.
> > //
> > XSDIdentityConstraintDefinition keyref =
> > xsdFactory.createXSDIdentityConstraintDefinition();
>
> >
> keyref.setIdentityConstraintCategory(XSDIdentityConstraintCa tegory.KEYREF_LITERAL);
>
> > keyref.setName("keyref");
> > XSDXPathDefinition keyrefSelector =
> > xsdFactory.createXSDXPathDefinition();
> > keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
> > keyrefSelector.setValue("simpleRecursiveElementDeclaration");
>
> > keyref.setSelector(keyrefSelector);
> > XSDXPathDefinition keyrefField =
> > xsdFactory.createXSDXPathDefinition();
> > keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
> > keyrefField.setValue("simpleAttributeDeclaration");
> > keyref.setReferencedKey(key);
> > keyref.getFields().add(keyrefField);
>
> >
> simpleRecursiveElementDeclaration.getIdentityConstraintDefin itions().add(keyref);
>
> > // Add the element to the schema.
> > //
>
> > prototypeSchema.getContents().add(simpleRecursiveElementDecl aration);
>
> > // This is the result that is produced.
> > //
> > // <xsd:element name="simpleRecursiveElementDeclaration"
> > type="PTS:SimpleRecursiveComplexTypeDefinition">
> > // <xsd:annotation/>
> > // <xsd:unique name="unique">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclarationGroupMember"/>
> > // </xsd:unique>
> > // <xsd:key name="key">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclarationGroupMember"/>
> > // </xsd:key>
> > // <xsd:keyref name="keyref" refer="PTS:key">
> > // <xsd:selector
> > xpath="simpleRecursiveElementDeclaration"/>
> > // <xsd:field
> > xpath="simpleAttributeDeclaration"/>
> > // </xsd:keyref>
> > // </xsd:element>
> > }
>
> > I noticed that if setReferenceKey is not called for a KEYREF, I get this
> > exception:
>
> > java.lang.NullPointerException
> > at
> >
> org.eclipse.xsd.impl.XSDIdentityConstraintDefinitionImpl.cha ngeReference(XSDIdentityConstraintDefinitionImpl.java:786)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:342)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.updateElement( XSDConcreteComponentImpl.java:355)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.adoptContent(X SDConcreteComponentImpl.java:1363)
>
> > at
> > org.eclipse.xsd.impl.XSDSchemaImpl.adoptContent(XSDSchemaImp l.java:1725)
>
> > at
> >
> org.eclipse.xsd.impl.XSDConcreteComponentImpl.eNotify(XSDCon creteComponentImpl.java:1125)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:115)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotificationChainImpl.dis patch(NotificationChainImpl.java:103)
>
> > at
> >
> org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUniq ue(NotifyingListImpl.java:220)
>
> > at
> > org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:5 70)
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.initializeSimpleR ecursiveElementDeclaration(XSDPrototypicalSchema.java:1006)
>
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.initializePrototy peSchema(XSDPrototypicalSchema.java:375)
>
> > at
> >
> org.eclipse.xsd.util.XSDPrototypicalSchema.<init>(XSDPrototypicalSchema.java:184)
>
> > at
> > org.eclipse.xsd.test.XSDMainTest.run(XSDMainTest.java:132)
> > at
> >
> org.eclipse.core.internal.boot.InternalBootLoader.run(Intern alBootLoader.java:845)
>
> > at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
>
> > at java.lang.reflect.Method.invoke(Native Method)
> > at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
> > at org.eclipse.core.launcher.Main.run(Main.java:747)
> > at org.eclipse.core.launcher.Main.main(Main.java:583)
>
> > So I'll also fix that bug like this:
>
> > > etools-diff XSDIdentityConstraintDefinitionImpl.java
> > 786c786
> > < niceSetAttributeURIValue(theElement,
> > XSDConstants.REFER_ATTRIBUTE, theReferencedKey.getURI());
> > ---
> > > niceSetAttributeURIValue(theElement,
> > XSDConstants.REFER_ATTRIBUTE, theReferencedKey == null ? null :
> > theReferencedKey.getURI());
>
> > You can avoid this bug by making sure the referenced key is set for a
> > KEYREF...
>
> > prabhakar wrote:
>
> > > I am using eclipse to automatically generate a new schema (ie without gui
> > > and using java) - wanted to know how to implement the following unique,key
> > > and keyref constraints for a element as below
> > >
> > > <xsd:element name="ROOT_ELEMENT" type="ROOT_ELEMENT">
> > > <xsd:unique name="PERSON_PK">
> > > <xsd:selector xpath="PERSON"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:unique>
> > > <xsd:key name="PERSON_KEY">
> > > <xsd:selector xpath="PERSON"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:key>
> > > <xsd:keyref name="PERSON_FK" refer="PERSON_ID">
> > > <xsd:selector xpath="MAILING_ADDRESS"/>
> > > <xsd:field xpath="PERSON_ID"/>
> > > </xsd:keyref>
> > > </xsd:element>
> > > <xsd:complexType name="ROOT_ELEMENT">
> > > <xsd:sequence>
> > > ....... and expanding the complex type mentioned above
> > >
> > > I had seen the XSDIdentityConstraintDefinition class with xpath, selector
> > > etc and know that the functionality can be implemented.
> > > Couldnt really add it to the schema (getting some exception or the
> > > other)as there is no similar kind of example implementation
> > >
> > > Request to help in this regard( with a code snippet may be!)
> > >
> > > thanks & regards
> > > prabhakar
>
> > --------------5F8431D81A59BA8EA87DDEF0
> > Content-Type: text/html; charset=us-ascii
> > Content-Transfer-Encoding: 7bit
>
> > <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
> > <html>
> > Prabhakar,
> > <p>I'm updating XSDPrototypicalSchema to include this example:
> > <blockquote><tt>public void
> initializeSimpleRecursiveElementDeclaration()</tt><tt></tt >
> > <p><tt>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Loading Schema from JAR
Next Topic:Creating XSDSchema from DOM Element
Goto Forum:
  


Current Time: Thu Apr 25 06:48:20 GMT 2024

Powered by FUDForum. Page generated in 0.03953 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top