You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Raimondas <ra...@mikrovisata.net> on 2005/02/09 16:51:10 UTC

unique constraint and validation

Hi,
I'm starting with XMLBeans and they seem to be really convienent wat to work
with XML. One thing I can't figure out and which is important for me is
unique constraint.
Say I have schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://some.domain.com/xmlbeans"
xmlns="http://some.domain.com/xmlbeans"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:complexType name="attribute">
  <xs:attribute name="name" type="xs:string" use="required"/>
  <xs:attribute name="type" type="xs:string" use="required"/>
 </xs:complexType>
 <xs:element name="custom_attributes">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="table" type="table" maxOccurs="unbounded">
     <xs:unique name="attrName">
      <xs:selector xpath="attribute"/>
      <xs:field xpath="@name"/>
     </xs:unique>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
  <xs:unique name="tableName">
   <xs:selector xpath="table"/>
   <xs:field xpath="@name"/>
  </xs:unique>
 </xs:element>
 <xs:complexType name="table">
  <xs:sequence>
   <xs:element name="attribute" type="attribute" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="name" type="xs:string" use="required"/>
 </xs:complexType>
</xs:schema>

I generated library from it and now combine document in a following way:
public void doTest() {
        System.out.println("Creating doc...");
        XmlOptions options = new XmlOptions();
        options = options.setValidateOnSet();
        List errors = new ArrayList();
        options.setErrorListener(errors);
        CustomAttributesDocument doc =
CustomAttributesDocument.Factory.newInstance(options);
        CustomAttributesDocument.CustomAttributes attrs =
doc.addNewCustomAttributes();

        Table table = attrs.addNewTable();
        table.setName("TABLE_1");
        Attribute attribute_1 = table.addNewAttribute();
        attribute_1.setName("name");
        attribute_1.setType("Object");

        Attribute attribute_2 = table.addNewAttribute();
        attribute_2.setName("name");//not valid
        attribute_2.setType("Object");

        Table table_2 = attrs.addNewTable();
        table_2.setName("TABLE_2");
        Attribute attribute_3 = table_2.addNewAttribute();
        attribute_3.setName("name");
        attribute_3.setType("Object");

        Table table_3 = attrs.addNewTable();
        table_3.setName("TABLE_2");//not valid
        Attribute attribute_4 = table_3.addNewAttribute();
        attribute_4.setName("name");
        attribute_4.setType("Object");

        boolean valid = doc.validate(options);
        System.out.println("Valid ? " + valid);
        if(!valid) {
            for(Iterator i = errors.iterator(); i.hasNext();) {
                XmlError error = (XmlError)i.next();
                System.out.println("Message: " + error.getMessage());
                System.out.println("Location of invalid XML: " +
                    error.getCursorLocation().xmlText() + "\n");
            }
        }

        System.out.println("Created XML document:");
        System.out.println(doc.toString());
}

and here's the output:
Creating doc...
Valid ? true
Created XML document:
<custom_attributes xmlns="http://bom.exigen.com/reverse/xmlbeans">
  <table name="TABLE_1">
    <attribute type="Object" name="name"/>
    <attribute type="Object" name="name"/>
  </table>
  <table name="TABLE_2">
    <attribute type="Object" name="name"/>
  </table>
  <table name="TABLE_2">
    <attribute type="Object" name="name"/>
  </table>
</custom_attributes>

I've tried different combinations of element creation (using newInstance()
with XmlOptions and without)  but no expected behaviour.
While XmlSpy validates such XML file as expected.

Is there something I'm missing or any other ways to achieve this behaviour ?

Thanks


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org