xsd schemas

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

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 on other sites
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 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 on other sites
Quote:
 Original post by ApochPiQA 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 on other sites
Quote:
 Original post by ApochPiQA 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 on other sites
XSD is verbose by nature. You will be much more effective if you are willing to create long schemas.

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 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 on other sites
If you have Visual Studio 2005 handy, it's xsd support is pretty useful and intuitive.

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]