1

This is the XML:

<employee>
  <name>John Doe</name> <!-- exactly one mandatory element -->
  <age>35</age> <!-- exactly one mandatory element -->
  <manager/> <!-- optional element, can be absent -->
</employee>

I'm trying to define it in XSD:

<xs:complexType name="employee">
  <xs:all>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="age" type="xs:string"/>
    <xs:element name="manager" minOccurs="0"/>
  </xs:all>
</xsl:complexType>

Validator complains when manager element is absent. What am I doing wrong?

Maybe related to Middle way between XSD all and XSD sequence

This is how I validate: https://github.com/jcabi/jcabi-xml/blob/jcabi-0.7.5/src/main/java/com/jcabi/xml/XSDDocument.java#L161-L207

Community
  • 1
  • 1
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 1
    manager element should not exist if you do not like to send it across. I meant in the XML. Also check if you need the type of the element in the xsd. – Zeus Jan 14 '14 at 18:58
  • I didn't get you at all :( can you explain again please? – yegor256 Jan 14 '14 at 19:00
  • What is type of your manager element? – herry Jan 14 '14 at 19:25
  • The top xml that you have has manager element (3rd). If you do not intend to send manager data, do not use this element in there. Also, the type of hte manager should be mentioned in the XSD, i see that no type is mentiond in the xsd file. so, it should be like – Zeus Jan 14 '14 at 19:36
  • @Zeus, not really... you don't need to define a type for an element (in which case the default kicks in, which is anyType. – Petru Gardea Jan 14 '14 at 19:38
  • The problem with the linked post [Middle way...](http://stackoverflow.com/questions/839079/middle-way-between-xsd-all-and-xsd-sequence) is different: the author was trying to make one of the elements occur more than once, and that is not permitted in an xsd:all compositor in the XSD 1.0 spec. – Petru Gardea Jan 14 '14 at 19:43

1 Answers1

2

The key might be what validator you're using, and/or what is your exact error you're getting.

If I use this XSD (which is much like yours, except for some minor cleaning and use of a global element to match your instance XML):

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="employee">
        <xsd:complexType>
            <xsd:all>
                <xsd:element name="name" type="xsd:string"/>
                <xsd:element name="age" type="xsd:string"/>
                <xsd:element name="manager" minOccurs="0"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Then an XML like this is perfectly valid (notice that I've commented out the manager element):

<employee>
  <name>John Doe</name> <!-- exactly one mandatory element -->
  <age>35</age> <!-- exactly one mandatory element -->

    <!--
  <manager/> 
    -->
    <!-- optional element, can be absent -->
</employee>

Add the manager element back in and the XML is still valid (as per minOccurs=0).

Going back to my opening comments... you have to say what validator you're using... The above works on .NET and Java (stock Xerces) - as they should.

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • I think you're wrong, this is what I'm getting: `The content of element 'employee' is not complete. One of '{manager}' is expected.` (when `manager` is absent). I'm using Java Xerces – yegor256 Jan 14 '14 at 19:48
  • Could you please give a link to that part of the spec? (I'm using 2.11 as well) – yegor256 Jan 14 '14 at 19:59
  • Section [3.8.6](http://www.w3.org/TR/xmlschema-1/#coss-modelGroup), scroll to Schema Component Constraint: All Group Limited. Your error message implies that one cannot use minOccurs on an element particle; the spec clearly states: `The {max occurs} of all the particles in the {particles} of the group must be 0 or 1.` The minOccurs is defined as 0 or 1. – Petru Gardea Jan 14 '14 at 20:07
  • Hm.. I also found this http://stackoverflow.com/questions/5677154 (which confirms what you're saying and showing).. So why my schema doesn't work?... (I have a more complex example than the above, but similar in syntax and semantic) – yegor256 Jan 14 '14 at 20:13
  • I posted a link to the Java code I'm using for validation (see the question updated) – yegor256 Jan 14 '14 at 20:37
  • I can't figure it out from a cursory look... you're doing things differently... have a look at [this](https://www.paschidev.com/downloads/xsd_1_1.zip) - I've uploaded the test project on our website; go straight to the source folder and look for Sample.java (fyi, the project is NetBeans) - that should help. Don't let the XSD 1.1 designation fool you, it works as XSD 1.0 as well. – Petru Gardea Jan 14 '14 at 20:53