Jimbo
Jimbo

Reputation: 21

Difficulty successfully defining a unique constraint

I have the following XML document:

<?xml version="1.0"?>
<BillingRunParameters>
 <BusinessUnit BillFullBusinessUnit="false">XXXXXXXX</BusinessUnit>
 <Portal BillFullPortal="false">XXXXXXX</Portal>
 <BillingTargets>
  <CustomerIdList CustomerBilling="true">
   <CustomerId>1234567891</CustomerId>
   <CustomerId>1234567891</CustomerId>
  </CustomerIdList>
  <TransactionIdList TransactionBilling="true" ApplicationTransactions="true">
   <TransactionId>12345ABC</TransactionId>
   <TransactionId>1232K89C</TransactionId>
  </TransactionIdList>
 </BillingTargets>
</BillingRunParameters>

I have put the following XML Schema together to validate it. My XSD includeds a unique constraint on the CustomerId element. It is fine for multiple CustomerId elements to be present, but there cannot be more than one containing the same value (as there is above).

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb" version="1.0" xdb:storeVarrayAsTable = "true">
  <xs:element name="BillingRunParameters" xdb:defaultTable="XSD_BILLING_PARAMETERS">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="BusinessUnit" type="typeBusinessUnit" minOccurs="1" maxOccurs="1"/>
        <xs:element name="Portal" type="typePortal" minOccurs="1" maxOccurs="1"/>
        <xs:element name="BillingTargets" type="typeBillingTargets"  minOccurs="0" maxOccurs="1" xdb:defaultTable="XSD_BILLING_TARGETS"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="testCustomerIdUnique">
      <xs:selector xpath="BillingTargets/CustomerIdList"/>
      <xs:field xpath="CustomerId"/>
    </xs:unique>
  </xs:element>

  <xs:simpleType name="valiatorBusinessUnit">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="30"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="typeBusinessUnit">
    <xs:simpleContent>
      <xs:extension base="valiatorBusinessUnit">
        <xs:attribute name="BillFullBusinessUnit" type="xs:boolean" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType >

  <xs:simpleType name="valiatorPortal">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="30"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="typePortal">
    <xs:simpleContent>
      <xs:extension base="valiatorPortal">
        <xs:attribute name="BillFullPortal" type="xs:boolean" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:complexType name="typeBillingTargets">
    <xs:sequence>
      <xs:element name="CustomerIdList" type="typeCustomerIdList" minOccurs="1" maxOccurs="1"/>
      <xs:element name="TransactionIdList" type="typeTransactionIdList" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="typeCustomerIdList">
    <xs:sequence>
      <xs:element name="CustomerId" type="typeCustomerId" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="CustomerBilling" type="xs:boolean" use="required"/>
  </xs:complexType>
  <xs:simpleType name="typeCustomerId">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="20"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="typeTransactionIdList">
    <xs:sequence>
      <xs:element name="TransactionId" type="typeTransactionId" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="TransactionBilling" type="xs:boolean" use="required"/>
    <xs:attribute name="ApplicationTransactions" type="xs:boolean" use="required"/>
  </xs:complexType>
  <xs:simpleType name="typeTransactionId">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="20"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

I am using the following online validation tool:

XML-XSD Validation Tool

Unfortunately, all the unique constraint directive seems to do is restrict me to a single CustomerId element. I am getting the following error message:

**The field 'CustomerId' is expecting at the most one value. Line: 1 Column:271 **

It is worth mentioning here that if I change the second element's value, I still get this error message. It is most vexing.....

I am pretty certain it is not the online validation tool at fault. It works very well for the following example I found on Stackoverflow:

How to ensure unique values in an XML Schema-validated document

This entire problem is driving me mental. I have spent hours trying various syntax all to no avail. The error message above is as close as I can come to actually have unique work for me in my schema.....

Does anybody have any ideas......?

Upvotes: 2

Views: 467

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

All you have to do is change this constraint:

enter image description here

To this:

enter image description here

The relevant XSD:

<xs:unique name="testCustomerIdUnique"> 
    <xs:selector xpath="BillingTargets/CustomerIdList/CustomerId"/> 
    <xs:field xpath="."/> 
</xs:unique> 

The idea is to have uniqueness of the value, not of the element (that is what the error message is telling you).

Upvotes: 2

Related Questions