• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSD: Can I require a child element based on a parent attribute value?

 
Paul Wam
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I want - if the parent attribute (attr) is true, then I want to require that child element <childB> be filled out (childA is always filled out). If the parent attribute is false, then I do not want <childB> to be filled out. How do I accomplish this conditional situation with XSD? Thanks.

sample xsd -

<xs:element name="parent" attr=true|false>
<xs:element name="childA"> </childA>
<xs:element name="childB"> </childB>
</xs:element parent>

 
g tsuji
Ranch Hand
Posts: 649
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is no, xsd is not expressive enough (like it or not) for that kind of constraint.
 
Paul Wam
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks g tsuji,

Rethinking the issue - I think the best way to solve the example I gave is to remove "attr=true|false". Then set the childB element to optional using minoccurs=0. ChildB's presence indicates true and absence indicates false.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic