File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ensure child element cannot exist without parent Using Xsd

 
rohit chawla
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic