Hello,
I have spent last 2 days working on a XSD specification and an example of XML code that this XSD would validate. However, the XSD I have created does not validate the XML and thus I have information from the command line about the error, I was unable to identify the exact cause.
If anyone could help me with this, I would be thankful.
The XML looks like this:
<?xml version="1.0" encoding="UTF-8"?> <peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd"> <person role="boss" typeOfContact="humanBeing"> <firstName>Anton</firstName> <lastName>Mlaskal</lastName> <email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email> <email typeOfEmail="professional">mlaskal.anton@gmail.com</email> <phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber> <phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber> <web>www.mlaskal.com</web> <adress> <streetName>Lubovnikova</streetName> <houseNumber>7</houseNumber> <postalCode>84107</postalCode> <cityName>Bratislava</cityName> <country>Slovakia</country> </adress> <note typeOfNote="action">Please, kill him as soon as possible.</note> <note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note> </person> <person role="boss" typeOfContact="IDServicePair"> <service>Skype</service> <ID>Antonius</ID> </person> </peopleCatalogue>
The XSD looks like this:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="peopleCatalogue"> <xsd:complexType> <xsd:all> <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="role" type="xsd:string" default="unknown"/> <xsd:attribute name="typeOfContact"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="humanBeing|IDServicePair"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element> </xsd:all> </xsd:complexType> </xsd:element> <xsd:complexType name="IDServicePair"> <xsd:all> <xsd:element name="ID" minOccurs="1" maxOccurs="1"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z0-9 ])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="service" minOccurs="1" maxOccurs="1"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:all> </xsd:complexType> <xsd:element name="humanBeing"> <xsd:complexType> <xsd:all> <xsd:element name="firstName" minOccurs="1" maxOccurs="1"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="lastName" minOccurs="1" maxOccurs="1"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="email"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="emailPattern"> <xsd:attribute name="typeOfEmail" default="personal"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <xsd:element name="phoneNumber"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="phonePattern"> <xsd:attribute name="typeOfPhone" default="cell"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <xsd:element name="web"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="(https?://[a-zA-Z]+(\.[a-zA-Z]+)*\.[a-zA-Z]{2,9})|(www(\.[a-zA-Z]+)+((\.[a-zA-Z]{2,9}){1}))"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="adress"> <xsd:complexType> <xsd:all> <xsd:element name="streetName"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z ])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="houseNumber"> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:pattern value="([0-9])*"/> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="99999"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="postalCode"> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:pattern value="([0-9])*"/> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="99999"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="cityName"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z ])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="country"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-zA-Z ])*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:all> </xsd:complexType> </xsd:element> <xsd:element name="note"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="noteType"> <xsd:attribute name="typeOfNote"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:all> </xsd:complexType> </xsd:element>
The error message I get, when I try to validate the xml file with xsd through
xmllint --noout --schema zoznam-osoby.xsd OnePerson.xml in command line is this: OnePerson.xml:3: element person: Schemas validity error : Element 'person': Character content is not allowed, because the content type is empty. OnePerson.xml:3: element person: Schemas validity error : Element 'person': Element content is not allowed, because the content type is empty. OnePerson.xml:22: element person: Schemas validity error : Element 'person': This element is not expected. OnePerson.xml fails to validate
It tells me, that the content type is empty, but I have no idea, what kind of content this is about. What to do to make it functional?

- 59
- 4
-
Your schema and your question showcase considerable gaps in your understanding of XML Schema. I suggest that you first study relevant learning material, for example [Definitive XML Schema](http://www.datypic.com/books/defxmlschema/), or the W3C [XML Schema Primer](http://www.w3.org/TR/xmlschema-0/). – Meyer Dec 10 '16 at 15:06
-
Could you please briefly elaborate this? – Jakub Hančin Dec 10 '16 at 15:09
-
Well, the error message already tells you everything you need to know. In the schema, `person` is declared as an element with empty content, which means that it cannot contain other elements. – Meyer Dec 10 '16 at 15:26
-
I am looking for the line where I have declared it as an element with empty content, but I cannot find the answer. That is the reason why I am asking anyone willing to point me in the right direction. – Jakub Hančin Dec 10 '16 at 15:41
-
2As I said, you should study relevant learning material. Then you would know that an element with empty content simply is declared [by not including any content in its declaration](http://www.w3.org/TR/xmlschema-0/#emptyContent). – Meyer Dec 10 '16 at 15:58
2 Answers
Except for type definition problems (fixed in the examples below), you are generally looking for Conditional Type Assignment defined starting with version 1.1 of the XML Schema standard (https://www.w3.org/TR/xmlschema11-1/#cTypeAlternative), see https://stackoverflow.com/a/27880329.
XML Schema v1.1 data:
<?xml version="1.0" encoding="UTF-8"?>
<peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd">
<person role="boss" typeOfContact="humanBeing">
<firstName>Anton</firstName>
<lastName>Mlaskal</lastName>
<email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email>
<email typeOfEmail="professional">mlaskal.anton@gmail.com</email>
<phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber>
<phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber>
<web>www.mlaskal.com</web>
<adress>
<streetName>Lubovnikova</streetName>
<houseNumber>7</houseNumber>
<postalCode>84107</postalCode>
<cityName>Bratislava</cityName>
<country>Slovakia</country>
</adress>
<note typeOfNote="action">Please, kill him as soon as possible.</note>
<note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note>
</person>
<person role="boss" typeOfContact="IDServicePair">
<service>Skype</service>
<ID>Antonius</ID>
</person>
</peopleCatalogue>
XML Schema v1.1 definition:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xsd:element name="peopleCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
<xsd:alternative test="@typeOfContact = humanBeing" type="humanBeing"/>
<xsd:alternative test="@typeOfContact = IDServicePair" type="IDServicePair"/>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="IDServicePair">
<xsd:all>
<xsd:element name="ID" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z0-9 ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="service" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:all>
<xsd:attribute name="role" type="xsd:string" default="unknown"/>
<xsd:attribute name="typeOfContact" type="xsd:string" fixed="IDServicePair"/>
</xsd:complexType>
<xsd:complexType name="humanBeing">
<xsd:sequence>
<xsd:element name="firstName" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="lastName" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="email" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="emailPattern">
<xsd:attribute name="typeOfEmail" default="personal"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="phoneNumber" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="phonePattern">
<xsd:attribute name="typeOfPhone" default="cell"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="web">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="(https?://[a-zA-Z]+(\.[a-zA-Z]+)*\.[a-zA-Z]{2,9})|(www(\.[a-zA-Z]+)+((\.[a-zA-Z]{2,9}){1}))"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="adress">
<xsd:complexType>
<xsd:all>
<xsd:element name="streetName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="houseNumber">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:pattern value="([0-9])*"/>
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="postalCode">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:pattern value="([0-9])*"/>
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="cityName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="country">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name="note" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="noteType">
<xsd:attribute name="typeOfNote"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="role" type="xsd:string" default="unknown"/>
<xsd:attribute name="typeOfContact" type="xsd:string" fixed="humanBeing"/>
</xsd:complexType>
<xsd:simpleType name="emailPattern">
<xsd:restriction base="xsd:string">
<xsd:pattern value=".+@.+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="phonePattern">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\+[\d\s]+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="noteType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
However, I am not sure how widespread support of v1.1 of XML Schema is (at least NetBeans 8.2 failed to understand it). So I suggest to use XML Schema v1.0 definition distinguishing element type by the element name itself rather then by the value of an required attribute. I do not see much sense in doing so – why to use the same element name when the required attribute totally changes its allowed contents?
XML Schema v1.0 data:
<?xml version="1.0" encoding="UTF-8"?>
<peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd">
<humanBeing role="boss">
<firstName>Anton</firstName>
<lastName>Mlaskal</lastName>
<email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email>
<email typeOfEmail="professional">mlaskal.anton@gmail.com</email>
<phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber>
<phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber>
<web>www.mlaskal.com</web>
<adress>
<streetName>Lubovnikova</streetName>
<houseNumber>7</houseNumber>
<postalCode>84107</postalCode>
<cityName>Bratislava</cityName>
<country>Slovakia</country>
</adress>
<note typeOfNote="action">Please, kill him as soon as possible.</note>
<note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note>
</humanBeing>
<IDServicePair role="boss">
<service>Skype</service>
<ID>Antonius</ID>
</IDServicePair>
</peopleCatalogue>
XML Schema v1.0 definition:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="peopleCatalogue">
<xsd:complexType>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="humanBeing" type="humanBeing"/>
<xsd:element name="IDServicePair" type="IDServicePair"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="IDServicePair">
<xsd:all>
<xsd:element name="ID" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z0-9 ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="service" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:all>
<xsd:attribute name="role" type="xsd:string" default="unknown"/>
</xsd:complexType>
<xsd:complexType name="humanBeing">
<xsd:sequence>
<xsd:element name="firstName" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="lastName" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="email" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="emailPattern">
<xsd:attribute name="typeOfEmail" default="personal"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="phoneNumber" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="phonePattern">
<xsd:attribute name="typeOfPhone" default="cell"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="web">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="(https?://[a-zA-Z]+(\.[a-zA-Z]+)*\.[a-zA-Z]{2,9})|(www(\.[a-zA-Z]+)+((\.[a-zA-Z]{2,9}){1}))"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="adress">
<xsd:complexType>
<xsd:all>
<xsd:element name="streetName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="houseNumber">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:pattern value="([0-9])*"/>
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="postalCode">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:pattern value="([0-9])*"/>
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="cityName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="country">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-zA-Z ])*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name="note" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="noteType">
<xsd:attribute name="typeOfNote"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="role" type="xsd:string" default="unknown"/>
</xsd:complexType>
<xsd:simpleType name="emailPattern">
<xsd:restriction base="xsd:string">
<xsd:pattern value=".+@.+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="phonePattern">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\+[\d\s]+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="noteType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
Ok, after a lot of effort, I solved it.
Basically, what I tried to do, was to specify some kind of an IF statement inside an XML schema. If a person has a typeOfContact = humanBeing, I wanted it to have a set of elements, if it is of typeOfContact = IDServicePair, I wanted it to have just two elements (ID + service).
I thought that if I specify the attribute as a complexType, this would be possible. However, it is not and after hours of effort, you will end up with the same error I have ended up with, with people seemingly answering, but basically just talking to you like a child.
So I just deleted a lot of my work and just dealt with the fact, that I cannot do that.

- 59
- 4