aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSD: Can I require a child element based on a parent attribute value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSD: Can I require a child element based on a parent attribute value?" Watch "XSD: Can I require a child element based on a parent attribute value?" New topic
Author

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

Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
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

Joined: Jan 18, 2011
Posts: 544
    
    3
The short answer is no, xsd is not expressive enough (like it or not) for that kind of constraint.
Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSD: Can I require a child element based on a parent attribute value?