File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Defining XSD for an XML file with constraint on the contents of a tag 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 "Defining XSD for an XML file with constraint on the contents of a tag" Watch "Defining XSD for an XML file with constraint on the contents of a tag" New topic
Author

Defining XSD for an XML file with constraint on the contents of a tag

Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Hi all,
i need to develop an XSD for a requirement where an element is recursively defined within itself, but we should specify a constraint that only the upper level element takes a particular value, which can't be taken by the same kind of element when defined as the child of something.
For ex:
=======================================================
<x>
<name>MAIN</name>
<y>
<x>
<name>Some value which should not be MAIN</name>
</x>
</y>
</x>
========================================================
Can somebody help me out as to how i can define such schema using XSD?

Thanks,
Naresh
Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
Hi Naresh,

It can be achieved easily in XSD.

1. Create a XSD type for your element 'x'
2. Create a new type extending the 'x' type created in step 1 and enforce your restrictions in this type.
3. In the root element of your xml, use the x type which is restricted (created in step 2)
4. In the element y use the x base type with no restrictions.

Sample XSD


Sample XML


Hope it helps..

Regards,
Srivatsa Katta
Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Hi Srivatsa,
thanks a lot that really helps me.
Thanks again.
-Naresh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Defining XSD for an XML file with constraint on the contents of a tag