This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Ensure child element cannot exist without parent Using Xsd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Ensure child element cannot exist without parent Using Xsd" Watch "Ensure child element cannot exist without parent Using Xsd" New topic
Author

Ensure child element cannot exist without parent Using Xsd

rohit chawla
Greenhorn

Joined: Apr 05, 2011
Posts: 8
I am facing issues in trying to get this schema validation inside xsd.

Valid case

<root>
<groups>
<group/>
</groups>
</root>

Valid case
<root>
<groups/>
</root>

valid case

<groups>
<class>
<group/>
</class>
</groups>


Invalid case

<root>
<group/>
</root>


How to ensure that a particular child element can only exist under some parent, not alone in xml through xsd?

In this e.g.
group cannot exist alone, but can exist when groups is parent...

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
This is relatively easy to implement in an XML Schema or XML DTD. You just need to start to think about the elements differently. XML languages are hierarchal structures. Every single element has only one parent. The fact that elements may use the same name in a particular design, does not mean that they are the same element. This is something that could easily be avoided in language design, but only if markup language concepts are clearly understood.

A group element when it is a child of a groups parent is different than a group element when it is a child of a root element.

The following are two different elements because they come from different parents. The fact that they share the same name does not mean that they are the same element.

root/group

root/groups/group

In your XML Schema or XML DTD you would simply include only groups as a valid child element. If an XML document violates this rule, then it will be reported when the document is validated against the XML Schema or XML DTD.

How to ensure that a particular child element can only exist under some parent, not alone in xml through xsd?


Do not specifiy that the particular child element is a valid child element. If you specify an element as a valid child the the parser will consider it a valid child. If you do not specify it as a valid child, then the parser will throw an error when it encounters an invalid content model.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ensure child element cannot exist without parent Using Xsd