Krishna Kumar
Krishna Kumar

Reputation: 63

XML schema-binding existence of an element with an attribute of specific value

i have the following xml structure-

<Recipients>
 <SwapswireRecipient Role="Counterparty">
 ...
 </SwapswireRecipient>
 <SwapswireRecipient Role="PrimeBroker">
 ...
 </SwapswireRecipient>
</Recipients>

Recipients can have 0 or more SwapswireRecipient and each SwapswireRecipient should have Role attribute with unique value. Role attribute is optional.

I have implemented above in xsd schema and it is working fine. Now i have a new constraint to implement in schema which i am finding difficult to implement.

Constraint- If SwapswireRecipient is present then exactly one SwapswireRecipient should exist with Role="Counterparty" value

Any help is appreciated.

Upvotes: 1

Views: 201

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

It cannot be done in XML Schema, one part due to the limitations in the XPath syntax supported by selectors, and the other related to cross-field validation.

If such a rule must be captured in your XSD, I would rewrite the XML Schema so that the content of a <Recipients> element would include an optional sequence, made from a mandatory <CounterPartyRecipient> element, folllowed by zero or more <SwapswireRecipient>. I would keep the xsd:unique clause for <SwapswireRecipient> elements and ensure that the @Role's type is restricted so that it does not include the Counterparty value.

Upvotes: 1

Related Questions