xml - XSD unique element XPath limitations -
in order allow element appear multiple times restrict element values being allowed once, i'm applying unique
element. i've got working in inefficient manner due xpath limitations within xsd 1.0 though. xsd 1.1 unfortunately not option me.
below simplified version of xsd:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified"> <xs:simpletype name="deliverymethod"> <xs:restriction base="xs:string"> <xs:enumeration value="post"/> <xs:enumeration value="digitalarchive"/> </xs:restriction> </xs:simpletype> <xs:complextype name="mailpack"> <xs:sequence> <xs:element maxoccurs="unbounded" name="deliverymethod" type="deliverymethod"/> </xs:sequence> </xs:complextype> <xs:complextype name="letter"> <xs:complexcontent> <xs:extension base="mailpack"> <xs:sequence> <xs:element name="content" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexcontent> </xs:complextype> <xs:element name="batch"> <xs:complextype> <xs:sequence> <xs:element minoccurs="0" name="id" type="xs:string"/> <xs:choice> <xs:element maxoccurs="unbounded" name="letter" type="letter"> <xs:unique name="oneofeachdeliverymethodtype"> <xs:selector xpath="deliverymethod"/> <xs:field xpath="." /> </xs:unique> </xs:element> </xs:choice> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
with following xml:
<?xml version="1.0" encoding="utf-8"?> <batch> <id>123</id> <letter> <deliverymethod>post</deliverymethod> <deliverymethod>digitalarchive</deliverymethod> <deliverymethod>digitalarchive</deliverymethod> <content>narf!</content> </letter> </batch>
this works expected , gives me error "digitalarchive" included twice. problem method forces me include unique
element in each possible extended mailpack
type. if e.g. introduce parcel
type extension of mailpack
have duplicate same unique
element batch/letter
element in new batch/parcel
element.
making unique
element part of deliverymethod
element below not option because ..
parent selector not allowed in xsd xpath.
<xs:complextype name="mailpack"> <xs:sequence> <xs:element maxoccurs="unbounded" name="deliverymethod" type="deliverymethod"> <xs:unique name="oneofeachdeliverymethodtype"> <xs:selector xpath="deliverymethod"/> <xs:field xpath=".." /> </xs:unique> </xs:element> </xs:sequence> </xs:complextype>
making part of batch
element directly resulted in same issue.
how can include unique
element once make count extensions of mailpack
? (if @ all)
by wrapping <deliverymethod>
elements in (for example) <deliverymethodslist>
element can move unique
element have write once.
as example need replace mailpack
complextype following:
<xs:complextype name="mailpack"> <xs:sequence> <xs:element name="deliverymethodslist"> <xs:complextype> <xs:sequence> <xs:element maxoccurs="unbounded" name="deliverymethod" type="deliverymethod"/> </xs:sequence> </xs:complextype> <!-- unique placed here --> <xs:unique name="oneofeachdeliverymethodtype"> <xs:selector xpath="deliverymethod"/> <xs:field xpath="." /> </xs:unique> </xs:element> </xs:sequence> </xs:complextype>
and don't need unique
element in letters list:
<xs:element maxoccurs="unbounded" name="letter" type="letter" />
now following example not valid <deliverymethod>digitalarchive</deliverymethod>
appears twice:
<?xml version="1.0" encoding="utf-8"?> <batch> <id>123</id> <letter> <deliverymethodslist> <deliverymethod>post</deliverymethod> <deliverymethod>digitalarchive</deliverymethod> <deliverymethod>digitalarchive</deliverymethod> </deliverymethodslist> <content>narf!</content> </letter> </batch>
Comments
Post a Comment