[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-07
8
Medium Priority
?
422 Views
Last Modified: 2012-05-08
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>
0
Comment
Question by:sfun28
  • 5
  • 3
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26203119
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 26203241
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26203337
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:sfun28
ID: 26203550
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
 
LVL 1

Author Comment

by:sfun28
ID: 26306553
Gertone - want to close this out...just need a response to my last question.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26307881
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
 
LVL 1

Author Closing Comment

by:sfun28
ID: 31674109
thanks so much for your in-depth answer!!!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26307897
welcome
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

830 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