XSD: element has at least one child element, but none is repeated

Folks,

Is it possible to specify an element that requires at least 1 child element, but that no child is repeated.  for example, lets say parent A has children B, C, D:

invalid (needs one of B, C, D)
<A></A>

valid:
<A><B /></A>

valid:
<A><B /><C /></A>

invalid (two "B"s)
<A><B /><B /><C /></A>
LVL 1
sfun28Asked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
Personally I would not go that way but use a simpler schema and pick one of the following
- use the workingdraft schema 1.1 and assertions (Saxon EE and Xerces-J allready support that)
- use schematron in combination with the xsd schema (www.schematron.com)

the assertions in xsd 1.1 or schematron would make sure each element is only used once
with an XPath like this "count(b) <= 1 and count(c) <= 1 and count(d) <= 1" in the context of a
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema   xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="a">
        <xs:complexType>
            <xs:choice minOccurs="1" maxOccurs="3">
                <xs:element ref="b"></xs:element>
                <xs:element ref="c"></xs:element>
                <xs:element ref="d"></xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="b"></xs:element>
    <xs:element name="c"></xs:element>
    <xs:element name="d"></xs:element>
</xs:schema>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
Well, for that you need to add all the permutations.
All other solutions would push your schema in the ambiguous zone
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="a">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element ref="b"/>
                    <xs:choice>
                        <xs:sequence>
                            <xs:element ref="c"/>
                            <xs:element ref="d" minOccurs="0"/>
                        </xs:sequence>
                        <xs:element ref="d" minOccurs="0"/>
                    </xs:choice>
                </xs:sequence>
                <xs:sequence>
                    <xs:element ref="c"/>
                    <xs:choice>
                        <xs:sequence>
                            <xs:element ref="b"/>
                            <xs:element ref="d" minOccurs="0"/>
                        </xs:sequence>
                        <xs:element ref="d" minOccurs="0"/>
                    </xs:choice>
                </xs:sequence>
                <xs:sequence>
                    <xs:element ref="d"/>
                    <xs:choice>
                        <xs:sequence>
                            <xs:element ref="c"/>
                            <xs:element ref="b" minOccurs="0"/>
                        </xs:sequence>
                        <xs:element ref="b" minOccurs="0"/>
                    </xs:choice>
                </xs:sequence>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="b"></xs:element>
    <xs:element name="c"></xs:element>
    <xs:element name="d"></xs:element>
</xs:schema>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
A W3C schema 1.1 would be as simple as this
<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema   xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="a"> 
        <xs:complexType> 
            <xs:choice minOccurs="1" maxOccurs="3"> 
                <xs:element ref="b"></xs:element> 
                <xs:element ref="c"></xs:element> 
                <xs:element ref="d"></xs:element> 
            </xs:choice>
        </xs:complexType> 
        <xs:assert test="count(b) <= 1 and count(c) <= 1 and count(d) <= 1"/>
    </xs:element> 
    <xs:element name="b"></xs:element> 
    <xs:element name="c"></xs:element> 
    <xs:element name="d"></xs:element> 
</xs:schema>

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
sfun28Author Commented:
Gertone - thanks so much for responding!  Excuse my ignorance here, XML has never been my forte:
If I go the workingdraft schema 1.1, then I need a validator such as Saxon EE or Xerces-J that supports the assert?  Right now I validate my Xml docs using .Net LINQ technology.  As is, my schema already uses w3 1.1.  I'm trying to figure out if I get the assert for free or if .NET LINQ would need to support it?
0
 
sfun28Author Commented:
Gertone - want to close this out...just need a response to my last question.
0
 
Geert BormansInformation ArchitectCommented:
Hey, sorry, I completely missed your follow up...

Yes if you are going with working draft, you need a parser that allready supports assert. I know for certain Saxon EE does, you do't need full Saxon EE to have a validating parser by teh way, cheaper versions of Saxon EE also do validating parsing. I have never tested with Xerces-J, but literature says it does support assertions allready.
I don't know at all how much of 1.1 is supported by .net msxml. If the schema allready is 1.1, there is a big chance assertions are in there. Why don't you simply test the schema I gave and see what happens. Maybe the LINQ knowledge base can shine a light on that

Sorry for not answering earlier, I completely missed your follow up
0
 
sfun28Author Commented:
thanks so much for your in-depth answer!!!
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.