Because of limitations of certain systems, we need to use XMLs that are formatted a bit inconveniently. Those we need to transform into a convenient form.
The question: how do I define in an XSD schema an element that has the following properties:
Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.
<xsd:all> Element Allows the elements in the group to appear (or not appear) in any order in the containing element.
The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.
An element is an XML node - and it can contain other nodes, or attributes. It can be a simple type or a complex type. It is an XML entity. An attribute is a descriptor.
You can use the <xsd:any /> element together with the Xml Schema Instance type attribute.
Schema
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:any processContents="strict" namespace="##local"></xsd:any>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="st">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
Test Xml instance
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- valid -->
<one xsi:type="st">value one</one>
<emptyone xsi:type="st"/>
<!-- invalid -->
<two name="myname" xsi:type="st">value two</two>
<!-- invalid -->
<three xsi:type="st">
<four xsi:type="st">value four</four>
</three>
</root>
Conclusion
You cannot enforce a simple type in the xsd schema alone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With