XSD author to allow expandable element
I want to authorize XSD to validate against XML files. An example XML file might look like this:
<person>
<fullname>John Doe</fullname>
<age>25</age>
<gender>male</gender>
</person>
One of the requirements is that the tag <person>
is extensible, which means that in addition to the 3 required children above, it can contain arbitrary elements with any name. Thus, this document will be valid when validating the XSD.
<person>
<fullname>John Doe</fullname>
<age>25</age>
<gender>male</gender>
<address>USA</address>
<profession>worker</profession>
</person>
I read about element <xs:any />
, but XSD doesn't let me put <xs:any />
inside element <xs:all />
. I want the elements <fullname>
, <gender>
and are <age>
required, and each of them should display exactly one. In addition, there may be zero or many optional elements.
Can this be achieved with supported XSD rules?
source to share
The combination of xs: all and xs: anyone can create ambiguous content, so it cannot be resolved. But you can do this if the content is in the xs: sequence.
Note. Make sure the namespace and processContent attributes on xs: any are set correctly for your requirements.
They support this flavor much better in XSD 1.1 using the xs: openContent tag , however, support for XSD 1.1. is still limited.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2016 Developer Bundle Edition 14.1.3.6618 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="fullname" type="xs:string" />
<xs:element name="age" type="xs:int" />
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male" />
<xs:enumeration value="female" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:any namespace="##any" processContents="skip" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
source to share