views:

643

answers:

1

I am trying to extend an XML schema to change the number of times a child can occur under an element.

In the original schema, the parent type is defined as such:

<xsd:complexType name="CrimeLineBusiness_Type">
 <xsd:complexContent>
  <xsd:extension base="PCLINEBUSINESS">
   <xsd:sequence>
    <xsd:element ref="CrimeSchedule" minOccurs="0"/>
    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
   </xsd:sequence>
  </xsd:extension>
 </xsd:complexContent>
</xsd:complexType>

I want to make it so both CrimeSchedule and CrimeMoneyAndSecurities are repeating elements. I thought this might work (inside of xsd:redefine):

 <xsd:complexType name="CrimeLineBusiness_Type">
  <xsd:complexContent>
   <xsd:extension base="CrimeLineBusiness_Type">
    <xsd:sequence>
     <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0" maxOccurs="unbounded"/>
     <xsd:element ref="CrimeSchedule" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
   </xsd:extension>
  </xsd:complexContent>
 </xsd:complexType>

However, then I get the following error:

cos-nonambig: CrimeSchedule and CrimeSchedule (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

This makes sense, sine those children were indeed defined in the original schema. So, how can I accomplish what I need to do?

Thanks!

+5  A: 

When you make an extension the content model of the new type is a sequence of the content model of the base and whatever you add in your new type. For example in your case the content model for the redefined CrimeLineBusiness_Type type is

<xsd:sequence>
  <xsd:sequence>
    <xsd:element ref="CrimeSchedule" minOccurs="0"/>
    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
  </xsd:sequence>
  <xsd:sequence>
    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element ref="CrimeSchedule" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:sequence>

What you can try to achieve what you want is a double redefinition of that type. First you redefine it by restriction, removing the optional CrimeSchedule and CrimeMoneyAndSecurities elements and then by extension adding those elements again with the desired number of occurrences. See below a full working sample:

text.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;

  <xsd:element name="test" type="CrimeLineBusiness_Type"/>

  <xsd:complexType name="PCLINEBUSINESS"/>
  <xsd:element name="CrimeSchedule"/>
  <xsd:element name="CrimeMoneyAndSecurities"/>

  <xsd:complexType name="CrimeLineBusiness_Type">
    <xsd:complexContent>
      <xsd:extension base="PCLINEBUSINESS">
        <xsd:sequence>
          <xsd:element ref="CrimeSchedule" minOccurs="0"/>
          <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

</xsd:schema>

redef1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:redefine schemaLocation="test.xsd">

    <xs:complexType name="CrimeLineBusiness_Type">
      <xs:complexContent>
        <xs:restriction base="CrimeLineBusiness_Type">
          <xs:sequence/>
        </xs:restriction>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

redef2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:redefine schemaLocation="redef1.xsd">
    <xs:complexType name="CrimeLineBusiness_Type">
      <xs:complexContent>
        <xs:extension base="CrimeLineBusiness_Type">
          <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="CrimeSchedule"/>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="CrimeMoneyAndSecurities"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

A valid XML instance, test.xml

<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="redef2.xsd">

  <CrimeSchedule></CrimeSchedule>
  <CrimeSchedule></CrimeSchedule>
  <CrimeMoneyAndSecurities></CrimeMoneyAndSecurities>
  <CrimeMoneyAndSecurities></CrimeMoneyAndSecurities>

</test>

Regards, George

George Bina
Great explanation and solution George! I'd been beginning to lose faith in SO, after seeing a few incorrect answers about XSD being voted up and accepted by the asker.
13ren