Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vasudhaiv Naresh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srivatsa,
thanks a lot that really helps me.
Thanks again.
-Naresh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic