Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 499
    
    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.

 
jQuery in Action, 2nd edition
 
subject: XSD: Can I require a child element based on a parent attribute value?
 
Similar Threads
XSD element validation
XmlException on calling WSDL2Java
A refactoring question.
Validating XML using XSD and with Oracle's DOMParser and XSDBuilder
generics and hibernate