'XSD Make a tag required if value

I want to create an XSD that will require tag2 when the val of tag1 is "x", which is basically make the tag "required" in specific value case. is that possible?

<T>
        <Level template = "a">
            <N>a</N>
        </Level>
        <Level template = "b">
            <N>b</N>
            <P>c</P>
        </Level>
</T>

In that XML example, I want to create 2 Levels, one of them with a template named "a", and the other with template "b". BUT, I want the XSD file to forbid the creation of template "b" without the tag P (which means the above example is valid). using:

  <T>
        <Level template = "b">
            <N>b</N>
        </Level>
</T>

will not be valid, since the template name is "b", but we have no "P" tag.

In my case, the name of the template should be specific - but if I can do it with a few options I'll be happy to hear.



Solution 1:[1]

This can't be done in XSD 1.0, it requires XSD 1.1.

XSD 1.1 is supported in Altova, Saxon, and Xerces schema processors, but not for example in libxml or Microsoft.

To achieve this in XSD 1.1, define element P as optional in the content model, and add an assertion:

<xs:assert test="if (@template = 'b') then exists(child::P) else true()"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1