I am designing a new XSD to capture points information from a business partner. For each transaction the partner must provide a value of points for at least one points type. I have the following:
<xs:element name="Points">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="SKUPointsQty" type="xs:int" minOccurs="0"/>
         <xs:element name="WelcomePointsQty" type="xs:int" minOccurs="0"/>
         <xs:element nam开发者_JS百科e="ManualPointsQty" type="xs:int" minOccurs="0"/>
         <xs:element name="GreenPointQty" type="xs:int" minOccurs="0"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>
The business rules are:
- a transaction must provide points from one or more of the points type
- a transaction cannot provide more than one instance of the same points type
What I have so far is not ideal because it would be possible to provide an XML instance without any points. I can't use a choice element because it must be possible to provide an XML instance with more that one points type element. The same point type must not be repeated for a single transaction.
Is it possible to enforce this rule in the design of the XSD?
I have a copy of the excellent XML Schema Companion by Neil Bradley. I can't find the answer in there so I guess it's not possible but thought I'd share the puzzle!
Thanks
Rob.
I think this kind of constraint logic is beyond XSD. Here are three techniques for checking instance documents for constraints that are not expressable by XML Schemas.
* a transaction cannot provide more than one instance of the samepoints type
That's fairly easy - and you already have that, basically.
Since your "inner" elements like
 <xs:element name="ManualPointsQty" type="xs:int" minOccurs="0"/>
are defined as they are, you make them optional (minOccurs="0"), and by default since you didn't specify anything else, they also have a maxOccurs="1" setting. 
So that half of the requirements should be taken care of.
- a transaction must provide points from one or more of the points type
That's the part where XML schema is not helping you much - you cannot express requirements like this in XSD. XSD only lends itself to "structural" modelling - things like "include this", "include 1 through 5 of these" - but you cannot express limitations that "span" more than one element like "if A is present, then B cannot be present", or "if A is present, then the value of B must be between 10 and 100". The "at least one of the four types must be present" also falls into that category, unfortunately :-( No luck there.
Since its a sequence, could you have a choice of four forms, depending on the first element present?
<xs:element name="Points">
    <xs:complexType>
        <xs:choice>  
            <xs:sequence>
                <xs:element name="a" type="xs:int" />
                <xs:element name="b" type="xs:int" minOccurs="0"/>
                <xs:element name="c" type="xs:int" minOccurs="0"/>
                <xs:element name="d" type="xs:int" minOccurs="0"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="b" type="xs:int" />
                <xs:element name="c" type="xs:int" minOccurs="0"/>
                <xs:element name="d" type="xs:int" minOccurs="0"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="c" type="xs:int" />
                <xs:element name="d" type="xs:int" minOccurs="0"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element name="d" type="xs:int" />
            </xs:sequence>
        </xs:choice>
    </xs:complexType>
</xs:element>
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论