Setting minOccurs and maxOccurs in XSD based on value of other XML field? -
i have xsd validate xml file. structure follows:
<root> <child> <size>2</size> <childelement>element 1</childelement> <childelement>element 2</childelement> </child> </root>
the number of childelement
s dependent on size provided i.e. if size
set 3, not more 3 childelement
s can added.
i have tried using xs:alternative
not seem work:
<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="root"> <xs:complextype> <xs:sequence> <xs:element name="child" minoccurs="1" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="size" type="xs:integer" maxoccurs="1"/> <xs:element name="childelement" type="xs:string" maxoccurs="1"> <xs:alternative test="@size>1" maxoccurs="@size"/> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
is there way of using xs:alternative
or tag achieve this, or outside realm of possibility xsd?
design recommendation: if xml design can still changed, eliminate size
element , convey information implicitly rather explicitly. eliminating duplication of information, you'll not need check duplication consistent.
if xml design cannot still changed, or if choose not change it...
xsd 1.0
not possible. have checked out-of-band wrt xsd.
xsd 1.1
possible using xs:assert
:
<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:vc="http://www.w3.org/2007/xmlschema-versioning" vc:minversion="1.1"> <xs:element name="root"> <xs:complextype> <xs:sequence> <xs:element name="child"> <xs:complextype> <xs:sequence> <xs:element name="size" type="xs:integer"/> <xs:element name="childelement" maxoccurs="unbounded"/> </xs:sequence> <xs:assert test="count(childelement) = size"/> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
Comments
Post a Comment