views:

527

answers:

2

Hello,

I'm trying to do some specific stuff in XSD XML Schema: I want to set an element as use="required", only if there a specific value to some attribute of other element.

Example:

<udi att1="all">

<name att2="dsd"/>

<phone att3="dsd"/>

</udi>

I want the that the "phone" element will be a required one, if the "att1" value is "all".

I was told that XPath could work for me, does anyone knows how? is there a better way then XPath?

Thanks,

Udi

+1  A: 

XML is not relational, it's hierarchical. Schemas are designed to enforce rules on nesting, not on rules between siblings. It simply is not something you can do with XSD.

You can do it when you're parsing or creating documents conforming to the format, however. In this case, while you're parsing through the XML document, you enforce constraints like the one you have stated in your question.

Welbog
A: 

+1 on Welbog's comment.

If it is a one-off, you can try creating two elements, say, Employee and EmployeeWithPhone and make it as a choice. Obviously, it is going to be pretty difficult if you have too many combinations..