Reputation: 11
I have an element 'ABC'. For that element there are 3 child elements. a,b and c and these are of type integer. and maximum occurrence of element c is infinity. if the value of element 'a' is 1 and 'b' is 2 then the occurrence of 'c' should be 3. Can i set the maximum occurrence of element c dynamically. or based on the value of elements 'a' and 'b' Eg:Refer the images Refer the screenshot of code Refer the screenshot of error message Refer the screenshot of value
Upvotes: 0
Views: 233
Reputation: 167401
XSD 1.1 schema with xs:assert
:
<xs:element name="ABC">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:integer"/>
<xs:element name="b" type="xs:integer"/>
<xs:element name="c" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
<xs:assert id="restrict-occurence" test="count(c) = a + b"/>
</xs:complexType>
</xs:element>
That way a sample like
<?xml version="1.0" encoding="UTF-8"?>
<ABC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="assert-example1.xsd">
<a>1</a>
<b>2</b>
<c>foo</c>
<c>bar</c>
<c>baz</c>
</ABC>
is considered valid while
<?xml version="1.0" encoding="UTF-8"?>
<ABC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="assert-example1.xsd">
<a>1</a>
<b>2</b>
<c>foo</c>
</ABC>
raises a validation error about the assertion failing: Assertion evaluation ('count(c) = a + b') for element 'ABC' on schema type '#AnonType_ABC' did not succeed.
.
Tested with Xerces in oXygen, I don't have XMLSpy here to test.
Make sure you use a version that supports/has enabled XSD schema 1.1 support.
Upvotes: 0