Unique partial attribution violation

I wrote the following (simplified) schema to validate some of the XML files I receive:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="Param">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="RadioAddr" type="xs:string" />
                <xs:element name="DataToRead" type="xs:integer" minOccurs="0" maxOccurs="1" />
                <xs:choice minOccurs="0" maxOccurs="1">
                    <xs:group ref="Group1" />
                    <xs:group ref="Group2" />
                    <xs:group ref="Group3" />
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:group name="Group1">
        <xs:sequence>
            <xs:element ref="Password" />
            <xs:element name="RadioActivated" type="xs:integer" minOccurs="0" maxOccurs="1" />
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:group name="Group2">
        <xs:sequence>
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <xs:element ref="Password" />
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:group name="Group3">
        <xs:sequence>
            <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
            <!-- No password here -->
            <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
    </xs:group>

    <xs:element name="Password" type="xs:string" />
    <xs:element name="IdNumber" type="xs:integer" />
    <xs:element name="AdjustClock" type="xs:integer" />

</xs:schema>

      

When checking this schema, I get the following error message:

Not valid. Error - Line 5, 25: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 25; cos-nonambig: Password and password (or elements from their substitution group) violate "Unique Attribution Element". When tested against this scheme, ambiguity is created for the two particles.

I fully understand the ambiguity, but I can't seem to find a solution to make my schema valid.

A possible solution would be to do something like

<xs:element name="Param>
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <!-- put all the possible elements here -->
        </xs:choice>
    </xs:complexType>
</xs:element>

      

but my problem with this solution is that I lose one layer of abstraction (group) which is useful for me further (I am using this schema to generate Java classes with JAXB).

So, is there a way to make my schema valid with <xs:group>

or do I need to flatten my schema (like the solution mentioned above)?

Update

Here are examples that must be allowed by XSD:

Minimum allowed:

<Param>
    <RadioAddr>1</RadioAddr>
</Param>

      

Is also legal:

<Param>
    <RadioAddr>1</RadioAddr>
    <Password>1234</Password>
</Param>

<Param>
    <RadioAddr>1</RadioAddr>
    <Password>1234</Password>
    <RadioActivated>1</RadioActivated>
    <IdNumber>12345678</IdNumber>
</Param>

<Param>
    <RadioAddr>1</RadioAddr>
    <IdNumber>12345678</IdNumber>
    <Password>1234</Password>
</Param>

      

+3


source to share


1 answer


To defeat the Unique Particle Attribution violation, you need to let the parser know exactly where it is in the grammar without looking ahead more than one element.

The current error occurs because, when familiarizing with an element, Password

it is not possible to know if the parser is in Group1

or Group2

because it IdNumber

is optional. You can make it IdNumber

mandatory instead , but this will create ambiguity between Group2

and Group3

over IdNumber

. You can then try to use ordering to differentiate the groups xs:choice

, but then you will find that the optionality of the elements skews your efforts. You can remove the optionality, and if different ordering among the groups is acceptable, you can get an answer.



However, that would be a rather strange grammar. At this point, you'll probably be better at anti-aliasing, as you mentioned, but instead of using unlimited xs:choice

, which would allow arbitrary and unrestricted repetitions of its elements, you can keep some constraints on appearing through simple xs:sequence

elements instead:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:element name="Param">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="RadioAddr" type="xs:string" />
        <xs:element name="DataToRead" type="xs:integer" minOccurs="0" maxOccurs="1" />
        <xs:element name="RadioActivated" type="xs:integer" minOccurs="0" maxOccurs="1" />
        <xs:element ref="IdNumber" minOccurs="0" maxOccurs="1" />
        <xs:element ref="Password" minOccurs="0"/>
        <xs:element ref="AdjustClock" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Password" type="xs:string" />
  <xs:element name="IdNumber" type="xs:integer" />
  <xs:element name="AdjustClock" type="xs:integer" />

</xs:schema>

      

+4


source







All Articles