Jump to content
  • Advertisement
Sign in to follow this  
_Madman_

xsd schemas

This topic is 4262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to learn how to make an xsd schema. Unfortunately the topic seems to be extremely confusing and from googling around doesn't seem to be well documented at all. What currently seems to puzzle me the most is how to make a schema with recursive tags. For example:
<bold>
<italic>
<bold>
...
</bold>
<italic>
</bold>
The task seems to be impossible to accomplish from what I have seen so far... Is that realy so? Ok, maybe there is a way by using global declarations, but what if there is a tag say <newtag> that cant include any of the mentioned ones?

Share this post


Link to post
Share on other sites
Advertisement
Something like this should work.


<xs:complexType name="text" mixed="true">
<xs:choice maxOccurs="unbounded">
<xs:element name="bold" nillable="true" type="text" />
<xs:element name="italic" nillable="true" type="text" />
</xs:choice>
</xs:complexType>

Share this post


Link to post
Share on other sites
A slightly more flexible alternative that I use quite a bit:

<xs:group name="elementgroup">
<xs:choice>
<xs:element name="bold" type="BoldTag" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="italic" type="ItalicTag" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
</xs:group>

<xs:complexType name="BoldTag">
<xs:sequence>
<xs:group ref="elementgroup" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="MoreComplexTag">
<xs:sequence>
<xs:group ref="elementgroup" />
</xs:sequence>
<xs:attribute name="SomeAttrib" type="xs:string" />
</xs:complexType>



I personally find it invaluable to have an editor like VS2005 that shows the valid elements/attributes at each point. Just mucking around with such an editor alongside your work-in-progress schema can be very illuminating when you need to figure out how various parts of the schema work.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
A slightly more flexible alternative that I use quite a bit:

*** Source Snippet Removed ***

I personally find it invaluable to have an editor like VS2005 that shows the valid elements/attributes at each point. Just mucking around with such an editor alongside your work-in-progress schema can be very illuminating when you need to figure out how various parts of the schema work.


I like it! I'm just starting XSD and didn't know you could use group elements like that. That example fixes a number of "limitations" I've found in XSD. I wish I could rate you up more. [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
A slightly more flexible alternative that I use quite a bit:

*** Source Snippet Removed ***

I personally find it invaluable to have an editor like VS2005 that shows the valid elements/attributes at each point. Just mucking around with such an editor alongside your work-in-progress schema can be very illuminating when you need to figure out how various parts of the schema work.


Still it seems to be real pain when you want to get a recursive structure, it allmost seems that kicking <any> tag is the only way you can do that without pages of xsd...

But thanks for the tip, I find it really usefull.

Share this post


Link to post
Share on other sites
Well I spent a whole day researching and in the end I'm more confused that I was before :(

The ideas offered here look realy nice, but I can't adjust them in the way I need.

The problem is that user might write a text like:

<text>
...<b>...</b>...<i>...</i>...<b>...</b>...
</text>





So I have a nonsequential order of tags. As a bonus, I also can't afford to include a new group tag. "moreComplexTag" just wont cut it :( I can only have <b>, <i> and <text> everything else goes outside the allowed set...

After some researching I stumbled upon:

<xs:element name="bi" type="xs:string" abstract="true" />
<xs:element name="b" type="xs:string" substitutionGroup="bi" />
<xs:element name="i" type="xs:string" substitutionGroup="bi" />





Which seems to fix the extra tag case, but validator swears that the tag can't substitute <bi> tag at which is expected at that position...

Share this post


Link to post
Share on other sites
Here's a complete schema that fully handles the recursion and arbitrary ordering:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="text" type="TextNode" />

<xs:complexType name="TextNode">
<xs:sequence>
<xs:group ref="TagGroup" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:group name="TagGroup">
<xs:choice>
<xs:element name="b" type="GenericTag" />
<xs:element name="i" type="GenericTag" />
</xs:choice>
</xs:group>

<xs:complexType name="GenericTag">
<xs:sequence>
<xs:group ref="TagGroup" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

</xs:schema>



One important thing to note: if you're looking to set up something like HTML, where you can freely mix markup tags with "raw text" data, you are using the wrong tool. XML is not designed to blend tags and text data.

If you genuinely need that capability, you need to look into SGML, which is the sort of "parent language" that both HTML and XML are derived from.

Share this post


Link to post
Share on other sites
Thanks a lot for the replies, they are really helpfull, but unfortunately, I am trying to use XML for the HTML like language modelling, so it's a dead end :(

Concerning Visual Studio, I have one and I used example XML file to generate base schema and edit parts of it via designer, but I'd say it's far from perfect if you need to include xs:choice, xs:all and other things like that... Bsically it's easier to genereate xsd from xml from my point of view.

Off to SGML then...

Thanks again!

EDIT: Hmmm... But what about DTDs?

[Edited by - _Madman_ on September 22, 2006 4:12:19 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!