0

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

1 Answers1

0

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you for your response. But Its not working – ANUJI VARGHESE Aug 17 '21 at 09:33
  • @ANUJIVARGHESE, I don't have XML Spy but I tested in oXygen with Xerces and Saxon and XSD 1.1 support and the sample assert seemed to do what I understood you want to achieve, I will add the samples to the answer. Explain in more detail whether you get an error, which one, or what exactly happens. – Martin Honnen Aug 17 '21 at 09:39
  • Can you please refer the screenshots attached. Its not working in altova xmlspy – ANUJI VARGHESE Aug 17 '21 at 10:28
  • @ANUJIVARGHESE, while screenshots can help please also share minimal but complete XML and XSD samples as code snippets. I guess it might be a namespace problem but it is not possible to tell without seeing minimal but complete samples as code. Can you try whether the simple example based on your original question and used in my answer also fails in Altova? This example doesn't use namespaces. – Martin Honnen Aug 17 '21 at 10:46
  • @ANUJIVARGHESE, as an alternative, of course, ask the Altova support whether they can help you with the problem. – Martin Honnen Aug 17 '21 at 10:48
  • If the schema uses a target namespace and the elements your assertion refers to are in that namespace it might be necessary to add `xpathDefaultNamespace="##targetNamespace"` to the `xs:assert` element. – Martin Honnen Aug 17 '21 at 10:54