How to validate that child elements exist with out specifying the order in XML Schema?

StackOverflow https://stackoverflow.com/questions/21461284

  •  05-10-2022
  •  | 
  •  

Question

I am trying to develop an XML Schema file to validate a Person element. Our application requires that a person have a FirstName and LastName but does not care what order they come in. It also allows other elements to be put under the Person element. So the following are valid:

<person>
    <firstName>Jon</firstName>
    <lastName>Smith</lastName>
</person>

<person>
    <lastName>Smith</lastName>
    <firstName>Jon</firstName>
</person>

<person>
    <title>Mr</title>
    <firstName>Jon</firstName>
    <lastName>Smith</lastName>
</person>

<person>
    <title>Mr</title>
    <lastName>Smith</lastName>
    <firstName>Jon</firstName>
    <suffix>CEng</suffix>
</person>

<person>
    <title>Mr</title>
    <lastName>Smith</lastName>
    <middleInitial>G</middleInitial>
    <firstName>Jon</firstName>
    <suffix>CEng</suffix>
</person>

The following however is not valid because it has no firstName:

<person>
    <title>Mr</title>
    <lastName>Smith</lastName>
    <suffix>CEng</suffix>
</person>

I tried creating a complex type like this:

<xsd:element name="person">
    <xsd:complexType>
        <xsd:all>
            <xsd:element minOccurs="1" maxOccurs="1" name="firstName" />
            <xsd:element minOccurs="1" maxOccurs="1" name="lastName"/>
            <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
        </xsd:all>
    </xsd:complexType>
</xsd:element>

But apparently any is not allowed inside all. Is it possible to get XML Schema to do this validation? If so how?

Was it helpful?

Solution

If you have an XML Schema 1.1 validator:

<xsd:element name="person">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:assert test="firstName and lastName"/>
</xsd:element>

In xsd 1.1, it is valid to have xsd:any inside an xsd:all but if your unspecified elements are in the same namespace as firstName and lastName, it may still cause an error because the content model would be ambiguous. But the xsd:assert would work.

In xsd 1.0, you can specify all orders that your elements can have as a sequence with choices. However, your elements other than firstName and lastName need to be in a different namespace, or you get this "ambiguous content model" error on your schema. I'm not sure if you could work with this "other namespace" constraint in your data, but I think that is the only way to model this in Xml Schema version 1.0.

XSD:

<?xml version="1.0" encoding="utf-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="persons">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="person" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="firstName" />
    <xsd:element name="lastName" />
    <xsd:element name="person">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
                <xsd:choice>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
                        <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
                        <xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
                    </xsd:sequence>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
                        <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
                        <xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
                    </xsd:sequence>
                </xsd:choice>
                <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Input XML:

<?xml version="1.0"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd" xmlns:o="urn:foobar">
    <person>
        <firstName>Jon</firstName>
        <lastName>Smith</lastName>
    </person>

    <person>
        <lastName>Smith</lastName>
        <firstName>Jon</firstName>
    </person>

    <person>
        <o:title>Mr</o:title>
        <firstName>Jon</firstName>
        <lastName>Smith</lastName>
    </person>

    <person>
        <o:title>Mr</o:title>
        <lastName>Smith</lastName>
        <firstName>Jon</firstName>
        <o:suffix>CEng</o:suffix>
    </person>

    <person>
        <o:title>Mr</o:title>
        <lastName>Smith</lastName>
        <o:middleInitial>G</o:middleInitial>
        <firstName>Jon</firstName>
        <o:suffix>CEng</o:suffix>
    </person>
</persons>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top