Posted on 2010-01-04
Last Modified: 2013-11-19
In my XSD,  i've got two elements, I want at most 1 of either, and at least 1 of them.
In other words I want : a, ab, abc, b,ba,bc, etc..  but not aa or bb, etc or nothing.
I tried:
    <xs:complexType name="xType">
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element ref="a"/>
            <xs:element ref="b"/>
            <xs:element ref="c"/>

but this doesnt allow ab, etc

<xs:complexType name="xType">
       <xs:choice minOccurs="1" maxOccurs="unbounded">
           <xs:element ref="a" maxOccurs="1"/>
           <xs:element ref="b" maxOccurs="1"/>
           <xs:element ref="c" maxOccurs="1"/>

but this allows aa,bb etc..

I also tried:
    <xs:complexType name="xType">
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="a" minOccurs="0" maxOccurs="1"/>
            <xs:element name="b" minOccurs="0" maxOccurs="1"/>
            <xs:element name="c" minOccurs="0" maxOccurs="1"/>

But this doesn't enforce the non-empty condtion.

How can this requirement be implemented in XSD?
Question by:blackfrancis75
    LVL 60

    Accepted Solution

    by implementing the different options, the risk of combining xs:all and xs:choice will make your model ambiguous soon,
    so your best bet is giving the options by combining sequences and choices and do that carefully

    (a, (b? & c?)) | (b, (a? & c?)) | (c, (b? & a?))

    is unambiguous, gives at least one element, up to three and no element is allowed twice
    You can vary on this theme to get what you want

    In my opinion it is better to model as an xs:choice multiple times
    (a | b | c)+
    makes sure that you have at least one element in the content model
    and use schematron for restricting (
    in the restricting assertions you can say something like
    "count(a) <= 1 and count(b) <= 1 and count(c) <= 1"
    that should cover your requirement
    LVL 60

    Expert Comment

    by:Geert Bormans
    in short:
    your only option, without bringing schematron in the picture, is listing all permutations one way or another
    I would go the schematron route by the way

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now