0

I need to put conditional enforcement on xml attributes using xsd.

For example

<Database>
    <Table Name="[Summary].[Aggregates]">
        <Columns>
            <Column Name="MyData" DataType="varbinary" Keys="Name,AddressLine1,AddressLine2,PostalCode,EmailAddress,Phone"></Column>
            <Column Name="User"></Column>
        </Columns>
    </Table>
    <Table Name="[Summary1].[Aggregates]">
        <Columns>
            <Column Name="MyData" DataType="varbinary" Keys="Name,AddressLine1,AddressLine2,PostalCode,EmailAddress,Phone"></Column>
            <Column Name="User"></Column>
        </Columns>
    </Table>
<Database>

I need to create xsd which will enforce validations such like if Column element has Name= "MyData" and DataType="varbinary" then it must have another attribute with named as Keys with atleast one value as Key="Name" else no attribute for that column.

Is this possible using xsd?

I am able to do child validations on attributes How to make type depend on attribute value using Conditional Type Assignment

Roshan
  • 873
  • 12
  • 33

1 Answers1

0

It might be easier to do using assertions rather than conditional type assignment, I don't think it's easy to judge from your description of the rules; but either way, it's certainly possible.

for example

<xs:assert test="if (@Name='MyData' and @DataType='varbinary')
                 then tokenize(@Keys, ',') = 'Name'
                 else empty(@Keys)"/>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164