I have an XSD 1.0 sequence that defines a set of elements, some of which may be optional but none of which can occur more than once, and which also ends with an <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax"/>
tag in order to enable users to add their own data elements in the easiest possible way when programmatically exporting conforming XML (i.e. ideally without requiring them to define their own namespace/XSD).
When processing XML that users generate, I will ignore any custom elements that they add via the <any>
tag, but I want to to know if there is a way to enforce uniqueness of element names so that I can be sure that they're not putting in duplicates of elements defined in the sequence with maxOccurs=1
?
Here is a sample xsd (interactive version here):
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="testxsd" targetNamespace="testxsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="XSDSequence">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="itemA" type="xsd:string"
minOccurs="1" maxOccurs="1"/>
<xsd:element name="itemB" type="xsd:string"
minOccurs="0" maxOccurs="1"/>
<xsd:element name="itemC" type="xsd:string"
minOccurs="1" maxOccurs="1"/>
<xsd:any minOccurs="0" maxOccurs="unbounded"
namespace="##any" processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
and here is a sample XML that passes XSD validation but which I would like to error out on due to the duplicated <itemA>
at the end:
<XSDSequence xmlns="testxsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<itemA>foo</itemA>
<itemC>bar</itemC>
<randomElement>this is ok</randomElement>
<itemA>I want this second itemA to cause an error</itemA>
</XSDSequence>
(Note that capturing data items via attributes rather than elements is not something that I want to do.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…